Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

Reference to PHP, Second Edition
Reference to PHP, Second Edition
Reference to PHP, Second Edition
Ebook347 pages4 hours

Reference to PHP, Second Edition

Rating: 0 out of 5 stars

()

Read preview

About this ebook

As computer architectures and languages have evolved over the years, the ways in which computer users communicate with applications also has altered. We need new tools and new techniques. But we have sometimes a few days only to understand how to use such new tools. If it is the case, this e-book allows you to understand PHP and to use PHP effectively in one weekend. You will find here many simple examples how to use contemporary, modern PHP syntax. It makes sense to have this e-book on your private disk (or in your smartphone). It is easier to work with the right solutions available at once. This e-book uses PHP-dedicated online IDEs available and accessible for you always and everywhere. So, based on this e-book you can use and run your PHP scripts using your smartphone.

LanguageEnglish
PublisherAdam Majczak
Release dateMar 8, 2015
ISBN9781310933165
Reference to PHP, Second Edition

Read more from Adam Majczak

Related to Reference to PHP, Second Edition

Related ebooks

Programming For You

View More

Related articles

Reviews for Reference to PHP, Second Edition

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    Reference to PHP, Second Edition - Adam Majczak

    Reference to PHP

    Second Edition

    Adam Majczak

    C. Adam Majczak, 2015, All Rights Reserved

    Smashwords, English Edition

    (The Second Edition, improved and updated)

    E-Edition, License Notes

    This e-book is licensed for your personal use only. This e-book may not be re-sold or given away to others. To share this book with another person, please purchase an additional copy for each recipient.

    While every precaution has been taken in the preparation of this book, the publisher and the author assume no responsibility for errors or omissions, or for damages resulting from any use of the information contained herein. Information contained herein is offered and should be considered as as is only.

    As computer architectures and languages have evolved over the years, the ways in which computer users communicate with applications also has altered. We need new tools and new techniques. But we have sometimes a few days only to understand how to use such new tools. If it is the case, this e-book allows you to understand PHP and to use PHP effectively in one weekend. You will find here many simple examples how to use contemporary, modern PHP syntax. It makes sense to have this e-book on your private disk (or in your smartphone). It is easier to work with the right solutions available at once. This e-book uses PHP-dedicated online IDEs available and accessible for you always and everywhere. So, based on this e-book you can use and run your PHP scripts using your smartphone.

    ENGLISH EDITION

    CONTENTS:

    PART I:

    CHAPTER 1: Introduction

    What for is PHP?

    What is a PHP File?

    How to install a PHP interpreter?

    How to test local configuration?

    How to use PHP based on Command Line Interface (CLI)?

    How to run PHP on a remote server?

    Compileonline.com: PHP Page vs. PHP Web View Page

    Text output – codepad.org

    Embedding PHP

    Comments in PHP and including files

    CHAPTER 2: PHP variables and constants

    Variable names and values

    Case sensitivity

    Error messages and warnings

    Data types in PHP

    Using constants in PHP

    Integer data type

    Floating-point numbers

    Bool type (Boolean values)

    Null type variable and type checking in PHP

    Dynamic variables

    CHAPTER 3: Operators

    Arithmetic operators

    Assignment operator and combined operators

    Comparison operators

    Logical operators

    Type cast forcing

    Conditional statements (preview)

    Base converting functions and bitwise operators

    Operator precedence

    The ternary operator

    CHAPTER 4: PHP Arrays at a glance

    Numeric indexed arrays, runtime array size change

    Associative arrays

    How to add new key => value pairs to associative arrays?

    How to format output data?

    PART 2:

    CHAPTER 5: Array processing

    Character strings as 1D arrays

    Mixed arrays

    Multi-dimensional arrays

    Internal pointer and some useful functions

    CHAPTER 6: When we need conditional statements and loops

    If statement (if-elseif-else) and switch-case statement

    The while and do-while loops

    The for and foreach loops

    The break, continue and goto keywords

    Filtering mask with binary operators

    CHAPTER 7: PHP functions

    Argument passing by value and using default values

    Variable argument list

    Anonymous functions

    Arrays as arguments and return values

    Argument passing by reference

    Returning an array or modifying values by reference

    Locals vs. globals

    Variable scope in PHP vs. JavaScript

    Math functions

    Searching and sorting

    Quick binary search

    PHP runtime-created functions

    Generators

    PART 3:

    CHAPTER 8: Date and time

    Introduction: date() and time() functions

    Timezone

    The getdate() function

    Date validation: checkdate()

    Simple timezone geolocation

    CHAPTER 9: Introduction to OOP

    Encapsulation, abstraction, inheritance and so on…

    What is a class?

    Properties and methods

    Creating an object (a class new instance)

    Visibility and scope

    Constructor

    Destructor

    Included and required file(s)

    Simple object cloning

    CHAPTER 10: A little more of OOP

    PHP does not support constructor overloading

    Static local variables

    Static class members and methods

    Declaring constants within classes

    Magic constants

    Static properties in use

    Recommended OOP practice and The Gold Rule

    BC Math functions vs. PHP operators

    Inheritance – first glance

    Overriding class members

    Final keyword and instanceof operator

    Access levels before and after inheritance

    Generating HTML page using PHP functions

    PART 4:

    Let’s take a look again (without HEREDOC)

    CHAPTER 11: Classes, objects and traits in use

    Generating HTML page using PHP classes

    A few words about PHP scope

    Interfaces

    Abstract data types (ADT) and the abstract keyword

    Polymorphism

    Traits

    Constructor overloading

    Randomized numbers and strings

    CHAPTER 12: Intermixing PHP and HTML, JavaScript, OS commands

    Intermixing PHP and HTML

    Using intermixed PHP and JavaScript codes

    How to transfer data from PHP variable to JavaScript variable?

    How to transfer data from JavaScript variable to PHP variable?

    PHP intermixing with JavaScript, how it works?

    Transferring data from PHP variables to JavaScript again

    PHP interaction with operating system and file system

    Pointing to an external process

    Basic file-related functions

    CHAPTER 13: Effective programming using functions and classes

    Algorithm complexity and recursive functions

    Fibonacci sequence and factorials

    Programming errors

    The is_datatype() group

    Better encapsulation: using __set() and __get() methods

    Property and function overloading

    More about lambda functions and closures

    Namespaces

    Namespace aliases

    CHAPTER 14: A few advanced topics simply explained

    A few words about error handling

    Function call - time measuring

    The NaN and Infinity errors

    Exception handling

    The finally keyword

    Shared operating memory in PHP

    A few words about data encryption in PHP

    Tuples in PHP

    PART 5:

    CHAPTER 15: What’s new in PHP

    How to check PHP version?

    The "use" operator extended

    The foreach() supports list()

    New(er) password hashing

    The empty() native function accepts expressions

    Constant (scalar) expressions

    Constant arrays and variadic functions

    The ** operator instead of the pow() function in PHP 5.6

    Dynamic Properties in PHP

    CHAPTER 16: PHP graphics

    How to use HTML Canvas with PHP?

    How to grab pictures from other web-sites?

    A simple way to place an image in PHP

    How to create graphs and function plots using The PHP GD Library?

    Drawing y = cos(x) plot in PHP

    Using SVG graphics

    Drawing rotation using SVG

    PHP graphics using CSS

    PART 1:

    This part describes the following PHP programming aspects:

    What and what for is PHP?

    How to install a PHP interpreter?

    How to use PHP based on Command Line Interface (CLI)?

    How to run PHP on a remote server?

    Embedding PHP

    Variable names and values

    Error messages and warnings

    Data types in PHP

    Using constants in PHP

    Integer data type

    Floating-point numbers

    Bool type (Boolean values)

    Null type variable and type checking in PHP

    Dynamic variables

    Arithmetic operators

    Assignment operator and combined operators

    Comparison operators

    Logical operators

    Type casting

    Conditional statements

    Base converting functions and bitwise operators

    Operator precedence

    Numeric indexed arrays, runtime array size change

    Associative arrays

    How to add new key => value pairs to associative arrays?

    How to format output data?

    CHAPTER 1: Introduction

    What for is PHP?

    When we are using PHP to create websites, at the most basic level, the request/response process consists of a web browser asking the web server to send it a web page and the server is sending back the page. Each step in the request / response sequence is as follows:

    1. You enter, for example, http://www.site.com into your browser’s address bar.

    2. Your browser looks up the IP address for www.site.com.

    3. Your browser issues a request (so named HTTP request) to that address for the web server’s home page.

    4. The request crosses the Internet and arrives at the www.site.com web server.

    5. The web server, having received the request, fetches the home page from its hard disk (i.e. the index.html or index.php file).

    (index.html can contain PHP script inside its HTML code.)

    6. With the home page now in memory, the web server notices that it is a file containing

    PHP script and passes the page to the PHP interpreter.

    7. The PHP interpreter executes the PHP code.

    8. The PHP interpreter returns the results of the executed PHP code, along with the results from the MySQL database (if any in use), to the web server.

    9. The web server returns the page to the requesting client, where client’s browser displays it.

    It is the most typical use of PHP. So, let’s begin. First, I will show you a few tools, and then we will analyze basic PHP syntax. Later I will show you how to use PHP effectively intermixing PHP codes witch CSS, HTML, JavaScript. This e-book contains examples using Canvas, SVG and other popular techniques.

    What is a PHP File?

    PHP is like C and C++, but interpreted (not compiled) server-side universal programming language (named also the server scripting language) used for dynamic websites and interactive web applications. PHP codes as input produce HTML as output. The PHP script is executed on the server, and the plain HTML result is sent back to the browser. Users do not need to install any additional software to be able to view PHP generated web pages. All being required is that the web server has PHP installed in order to interpret PHP scripts.

    * PHP files can contain text, HTML, CSS, JavaScript, and PHP code.

    * PHP codes are executed on the server, and the result is returned to the client’s browser as a plain HTML.

    * PHP files have as default extension *.php (* = file name, php = extension).

    * So named pure PHP files contain PHP codes only (without HTML, JavaScript, nor CSS).

    In contrast with static HTML sites, PHP sites are dynamically generated. Instead of the site containing a large number of static HTML files, a PHP-based site may consist of only a few template files (even Single PHP pages are in use). With PHP we are not limited to output HTML. We can output images, PDF files and Flash movies or any text, such as XHTML or XML. Even all HTML code can be dynamically generated by pure PHP files.

    Besides of numerous PHP examples this e-book contains also some simple JavaScript code snippets to show how to perform data processing on the client side by JavaScript and on the server side by PHP. I have compared hereby some popular programming techniques being frequently used and providing complementary solutions.

    How to install a PHP interpreter?

    It is free. First we should choose a www page to download. I will show here an example startup procedure for the most popular Windows environment (however there are numerous other www pages available to use and there are appropriate versions for all popular operating systems). You can use Google or any other application to localize the most convenient source. For example, you can first find a page to download using:

    XAMPP download + phrase.

    Pic. 1. You can download free the XAMPP tool (including PHP interpreter) to install it on your local computer.

    To view PHP code effects in a browser the code first has to be parsed on a web server having the PHP module installed. A simple way to set up a PHP environment is to download and install a distribution of the Apache web server called XAMPP, which comes pre-installed with PHP, Perl and MySQL. This will allow you to experiment with PHP on your own computer.

    Pic. 2. Default setup configuration in MS Windows environment.

    After installing the web server point your browser to "http://localhost" to make sure that the server is on and ready. It should display the index.php file, which by default is located under "C:\xampp\htdocs\index.php" on Windows machines. ..\htdocs is the folder that the Apache Web Server looks to for files to serve on your domain.

    After download and installation the default configuration of the XAMPP in the Windows system is shown below.

    Pic. 3. The C:\xampp\htdocs\ path in Windows.

    The Apache (www server) and the PHP (interpreter) should be started up first (manually) using the XAMPP Control Panel.

    Pic. 4. Control Panel.

    Now you can create PHP files and HTML files containing PHP codes. To view your local www pages created you should place INDEX.PHP or INDEX.HTML file (with your PHP code) in the

    ..\HTDOCS

    directory and set your browser to show HTTP://LOCALHOST page. The simplest procedure is the following:

    1. Create PHP / HTML file using Notepad (or any other ASCII text editor). Do not forget to change extension (default .TXT should be replaced by .PHP or .HTM / .HTML).

    You can also use Windows | Edit command or even the simplest sequence:

    COPY CON ...[^Z] command.

    2. Copy your file to the ..\HTDOCS directory as INDEX.PHP or INDEX.HTML.

    3. Use HTTP://LOCALHOST in your browser.

    How to test local configuration?

    You can test your configuration using the TEST.HTML file. Such a file should contain a HTML content (always visible for your browser) and a PHP content (visible only when PHP interpreter is working properly). So I propose you to use the following simple test file. Follow the procedure below:

    1. Write in (or copy from this ebook file) the following code:

    (use any ASCII editor, for example Notepad in Windows or Vi in Linux.)

    Your Browser is working.

    That is all you can see without PHP.

    echo My PHP is working properly. ;

    echo This message is visible only when PHP is configured properly.;

    ?>

    2. Save it as the INDEX.HTML file.

    3. Copy the INDEX.HTML file to the ..\HTDOCS directory.

    4. Point your browser to the HTTP://LOCALHOST.

    And that is all. If you see

    Enjoying the preview?
    Page 1 of 1