Reference to PHP, Second Edition
By Adam Majczak
()
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.
Read more from Adam Majczak
Introduction to PHP Rating: 3 out of 5 stars3/5Introduction to PHP, Part 2, Second Edition Rating: 0 out of 5 stars0 ratingsIntroduction to PHP, Part 1, Second Edition Rating: 0 out of 5 stars0 ratingsIntroduction to PHP, Part 4, Second Edition Rating: 0 out of 5 stars0 ratingsIntroduction to PHP, Part 3, Second Edition Rating: 0 out of 5 stars0 ratingsIntroduction to PHP, Part 5, Second Edition Rating: 0 out of 5 stars0 ratings
Related to Reference to PHP, Second Edition
Related ebooks
Persistence in PHP with Doctrine ORM Rating: 0 out of 5 stars0 ratingsPHP for Beginners Rating: 0 out of 5 stars0 ratingsPHP Oracle Web Development: Data processing, Security, Caching, XML, Web Services, and Ajax Rating: 0 out of 5 stars0 ratingsPHP Examples, Part 2 Rating: 1 out of 5 stars1/5PHP Reactive Programming Rating: 0 out of 5 stars0 ratingsLearn PHP in 24 Hours Rating: 0 out of 5 stars0 ratingsPHP 5 CMS Framework Development - 2nd Edition Rating: 0 out of 5 stars0 ratingsThe PHP Workshop: Learn to build interactive applications and kickstart your career as a web developer Rating: 0 out of 5 stars0 ratingsMastering PHP Design Patterns Rating: 0 out of 5 stars0 ratingsPHP Examples Part 4 Rating: 0 out of 5 stars0 ratingsLearning PHP 7 High Performance Rating: 0 out of 5 stars0 ratingsPHP Error Reporting: How To Do It Right Rating: 0 out of 5 stars0 ratingsLearning PHP Data Objects Rating: 5 out of 5 stars5/5Html5 for Beginners: A Step-By-Step Guide Rating: 0 out of 5 stars0 ratingsA concise guide to PHP MySQL and Apache Rating: 4 out of 5 stars4/5Expert PHP 5 Tools Rating: 4 out of 5 stars4/5HTML, CSS, Bootstrap, Php, Javascript and MySql: All you need to know to create a dynamic site Rating: 4 out of 5 stars4/5Introduction to PHP Web Services: PHP, JavaScript, MySQL, SOAP, RESTful, JSON, XML, WSDL Rating: 0 out of 5 stars0 ratingsP.H.P Simple C.R.U.D Design Rating: 4 out of 5 stars4/5jQuery 1.3 with PHP Rating: 0 out of 5 stars0 ratingsLaravel 5 Essentials Rating: 0 out of 5 stars0 ratingsLearn PHP Programming in 7Days: Ultimate PHP Crash Course For Beginners Rating: 3 out of 5 stars3/5PHP Examples Part 3 Rating: 5 out of 5 stars5/5Intermediate Load Runner With Oracle/Apex Concepts. Rating: 0 out of 5 stars0 ratingsPHP 7 Programming Blueprints Rating: 0 out of 5 stars0 ratingsLearn PHP 8: Using MySQL, JavaScript, CSS3, and HTML5 Rating: 0 out of 5 stars0 ratingsModular Programming with PHP 7 Rating: 0 out of 5 stars0 ratingsRapid Application Development With CakePHP Rating: 0 out of 5 stars0 ratingsLearn PHP: Learn PHP Programming in 4 hours! PHP for Beginners - Smart and Easy Ways to learn PHP & MySQL Rating: 4 out of 5 stars4/5Multi-Tier Application Programming with PHP: Practical Guide for Architects and Programmers Rating: 0 out of 5 stars0 ratings
Programming For You
Excel : The Ultimate Comprehensive Step-By-Step Guide to the Basics of Excel Programming: 1 Rating: 5 out of 5 stars5/5Python: For Beginners A Crash Course Guide To Learn Python in 1 Week Rating: 4 out of 5 stars4/5Java for Beginners: A Crash Course to Learn Java Programming in 1 Week Rating: 5 out of 5 stars5/5Python: Learn Python in 24 Hours Rating: 4 out of 5 stars4/5HTML & CSS: Learn the Fundaments in 7 Days Rating: 4 out of 5 stars4/5Learn SQL in 24 Hours Rating: 5 out of 5 stars5/5Grokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5SQL All-in-One For Dummies Rating: 3 out of 5 stars3/5Coding All-in-One For Dummies Rating: 4 out of 5 stars4/5Learn to Code. Get a Job. The Ultimate Guide to Learning and Getting Hired as a Developer. Rating: 5 out of 5 stars5/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Python Machine Learning By Example Rating: 4 out of 5 stars4/5Python Programming : How to Code Python Fast In Just 24 Hours With 7 Simple Steps Rating: 4 out of 5 stars4/5Unreal Engine from Zero to Proficiency (Foundations): Unreal Engine from Zero to Proficiency, #1 Rating: 0 out of 5 stars0 ratingsPython Projects for Beginners: A Ten-Week Bootcamp Approach to Python Programming Rating: 0 out of 5 stars0 ratingsHTML in 30 Pages Rating: 5 out of 5 stars5/5C++ Learn in 24 Hours Rating: 0 out of 5 stars0 ratingsProfessional C++ Rating: 2 out of 5 stars2/5OneNote: The Ultimate Guide on How to Use Microsoft OneNote for Getting Things Done Rating: 1 out of 5 stars1/5Beginning Programming with Python For Dummies Rating: 3 out of 5 stars3/5Linux: Learn in 24 Hours Rating: 5 out of 5 stars5/5
Reviews for Reference to PHP, Second Edition
0 ratings0 reviews
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
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