Cover
Boot teaches everything we need creating effective web applications - starting with big picture - diving into language syntax and programming techniques - become a top-notch PHP programmer
Foreword
by Michael P. Bourque
Preface
...
Contents of This Book
Read from start or jump around - ...
CHAPTER 1 - Introduction to PHP
What Does PHP Do?
p1 - Used in 3 ways:
• Server-side scripting - PHP was designed to create dynamic web content - generating XML documents, graphics, FLASH animations, PDF files and much more
• Command-side scripting - used for system administration tasks, backup and log parsing - even some CRON job type scripts can be done this way (nonvisual PHP tasks)
• Client-side GUI applications - using PHP-GTK, you can write full-blown, cross-platform GUI applications in PHP
This book concentrates on dynamic web content
PHP runs on all major operating systems - used with leading web servers Flexible - not limited outputting just HTML or other text files - any document format can be generated - built-in support for generating PDF files, GIF, JPEG, and PNG images, and Flash movies
Wide-ranging support for databases - with PHP creating web pages with dynamic content from a database is remarkably simple Library of PHP code to perform common tasks - with the PHP Extension and Application Repository (PEAR) - PEAR is a framework and distribution system for reusable PHP components
A Brief History of PHP
p2 - Version PHP 1.0 since June 1995 - PHP 2.0 April 96 - PHP 3.0 June 97 - project changed from one-person effort with a few contributors to a true open source project with many developers around the world - PHP 4.0 May 22, 2000 - now PHP 5.4 etc.
The Widespread Use of PHP
p6 - 77.8% of websites use PHP server-side
Installing PHP
p7 - Go php.net/manual/install.php
php.ini - PHP’s configuration settings - go de3.php.net/manual/en/configuration.file.php
To change PHP configuration file and restart Apache server - each time you make a change to PHP’s environment, you have to restart the Apache server in order for those changes to take effect - settings are maintained in a file called php.ini - in general the code in this book does not require a customized configuration - change php.ini.default: command+shift G > /etc/php.ini.default - open with Sublime text 2 or other editor
A Walk Through PHP
PHP pages are generally HTML pages with PHP commands embedded - in contrast to many other dynamic web page solutions, which are scripts that generate HTML
<?php echo "Hello, world!"; ?> // echo command produces output of string
Example 1-1 - hello_world.php
Configuration Page
p8 - The PHP function phpinfo() creates an HTML page full of information on how PHP was installed and is currently configured. You can use it to see whether you have particular extensions installed, or whether the php.ini file has been customized
PHP programs access form values primarily through the $_POST and $_GET array variables - more Chapter 7 - (for now be sure that you are processing your pages with the REGISTER_GLOBALS value set to off (the default) in the php.ini file) - REGISTER_GLOBALS feature has been removed as of PHP 5.4
Databases
PHP supports all the popular database systems, including MySQL, PostgreSQL, Oracle, Sybase, SQLite, and ODBC-compliant databases.
Example 1-4 shows MySQL database query - book title, year, ISBN - droplibrary.sql into MySQL after you create the library database, and have the sample database at your disposal for testing out the following code sample as well as the related samples in Chapter 8 - code in Example 1-4 connects to database, issues a query to retrieve all available books (with the WHERE clause), and produces a table as output for all returned results through a while loop - database-provided dynamic content drives the news, blog, and ecommerce sites at the heart of the Web - more in Chapter 8
Graphics
p13 - Easily create and manipulate images using GD extension. Example 1-5 provides text-entry field that lets the user specify the text for a button. It takes an empty button image file, and on it centers the text passed as the GET parameter 'message'. The result is then sent back to the browser as a PNG image.
You can use GD to dynamically resize images, produce graphs, and much more. PHP also has several extensions to generate documents in Adobe’s popular PDF format - Chapter 9 covers dynamic image generation in depth - Chapter 10 provides instruction on how to create Adobe PDF files
CHAPTER 2 - Language Basics
p15 - Chapter provides whirlwind tour of core PHP language - covering data types, variables, operators, and flow control statements - PHP strongly influenced by other programming languages, such as Perl and C - should be easy to pick up - if PHP first language don't panic - we build up from basics
Lexical Structure
Case Sensitivity
Statements and Semicolons
Whitespace and Line Breaks
Comments
Identifiers
An identifier is simply a name used to name variables, functions, constants, and classes - first character of an identifier must be an ASCII letter or underscore character (_) or between ASCII 0x7F and ASCII 0xFF - after the initial character, these characters and the digits 0–9 are valid - variable names always begin with a dollar sign ($) and are case-sensitive - function and class names are not case-sensitive - class name stdClass is reserved - a constant is an identifier for a simple value; only scalar values—Boolean, integer, double, and string can be constants - Once set, the value of a constant cannot change - referred to by their identifiers and are set using the define() function
define('PUBLISHER', "O'Reilly & Associates");
echo PUBLISHER;
Keywords
p21 - A keyword (or reserved word) is a word set aside by the language for its core functionality - you cannot give a variable, function, class, or constant the same name as a keyword - we cannot use an identifier that is the same as a built-in PHP function - complete list see APPENDIX - List of Keywords php.net
PHP Keywords - case-insensitive
__halt_compiler() abstract and array() as - break callable (as of PHP 5.4) case catch class
clone const continue declare default - die() do echo else elseif
empty() enddeclare endfor endforeach endif - endswitch endwhile eval() exit() extends
final finally (as of PHP 5.5) for foreach function - global goto (as of PHP 5.3) if implements include
include_once instanceof insteadof (as of PHP 5.4) interface isset() - list() namespace (as of PHP 5.3) new or print
private protected public require require_once - return static switch throw trait (as of PHP 5.4)
try unset() use var while - xor yield (as of PHP 5.5) = 67 keywords :-)
Compile-time constants - case-insensitive
__CLASS__ __DIR__ (as of PHP 5.3) __FILE__ __FUNCTION__ __LINE__
__METHOD__ __NAMESPACE__ (as of PHP 5.3) __TRAIT__ (as of PHP 5.4) = 8 compile-time constants :-)
Data Types
Integers
p22 - whole numbers - typically extends from −2,147,483,648 to +2,147,483,647 - can be written in decimal, octal, or hexadecimal - octal numbers consist of a leading 0 and a sequence of digits from 0 to 7
0755 // decimal 493
+010 // decimal 8
Hexadecimal values begin with 0x, followed by a sequence of digits (0–9) or letters (A–F)
0xFF // decimal 255
0x10 // decimal 16
-0xDAD1 // decimal −56017
If you try to store a variable that is too large to be stored as an integer or is not a whole number, it will automatically be turned into a floating-point number. Use the is_int() function (or its is_integer() alias) to test whether a value is an integer:
if (is_int($x)) {
// $x is an integer
}
Example 7-1 - The chunkify form - chunkify.html Example 7-3 - A self-processing temperature-conversion page - temp.php Example 7-4 - Temperature conversion using the GET method - temp2.php Example 7-5 - Temperature conversion with a sticky form - sticky_form.php Example 7-6 - Multiple selection values with a select box - select_array.php Example 7-7 - Multiple selection values in checkboxes - checkbox_array.php Example 7-8 - Sticky multivalued checkboxes - checkbox_array2.php Example 7-9 - Form validation - data_validation.php Example 7-10 - Preference selection - colors.php Example 7-11 - Setting preferences with cookies - prefs.php Example 7-12 - Using the color preferences with cookies - prefs_demo.php Example 7-13 - Setting preferences with sessions - prefs_session.php Example 7-14 - Using preferences from sessions - prefs_session_demo.php Example 7-15 - Saving state across visits - save_state.php