PHP Reference: Beginner to Intermediate PHP5
Mario Lurig
Beginner to Intermediate PHP5
ISBN: 978-1-4357-1590-5
Creative Commons Attribution-NonCommercial-ShareAlike 2.0
You are free:
to Share to copy, distribute and transmit the work to Remix to adapt the work
Under the following conditions:
Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).
Noncommercial. You may not use this work for commercial purposes.
Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.
For any reuse or distribution, you must make clear to others the license terms of this work.
Any of the above conditions can be waived if you get permission from the copyright holder.
Nothing in this license impairs or restricts the author's moral rights.
FIRST EDITION
http://www.phpreferencebook.com
Cover art credit (PHP REFERENCE:), included with permission:
Leo Reynolds ( www.flickr.com/lwr/ ) - 7 images Patrick Goor ( www.labworks.eu ) - 3 images
Eva the Weaver ( www.flickr.com/evaekeblad/ ) - 2 images Duncan Cumming ( www.flickr.com/duncan/ ) - 1 image
Contents
Preface
I taught myself PHP and MySQL and found myself, at times, without internet access and thus without search access to the PHP.net manual ( http://www.php.net/manual/en/ ). Since coding was not my primary job, I needed a refresher on syntax, usage, and most of all, a reminder of how to code PHP without spending an hour debugging a silly mistake. I printed out reference sheets, cards, cheat sheets and tried to work off of them exclusively. However, I still found myself needing more than would fit on one page of 8.5" x 11" paper front and back. So, off I went to the web and the local bookstore. After spending some time with a few books, giving them a trial run, I ran into two major problems:
- I spent most of the time weeding through extensive tutorials to find the keyword and answer I was looking for, sometimes fruitlessly.
- Information was biased or surrounded by irrelevant and often confusing code that did little to explain the what of the function.
I figured I couldn't be the only one with this problem, and quickly found out that I wasn't alone thanks to a chance run-in at a local bookstore. Casual PHP programmers, sometimes away from the internet, wanting a quick reference book that assumes they have some experience with PHP and understood the basics while still needing a little clarification sometimes on the details. Therefore, this book was born.
For this edition, I decided to eliminate some of the more advanced aspects of PHP programming: object oriented programming, image manipulation/creation, secondary modules, and a few others. Secondarily, items such as mail handling, file manipulation, regular expressions, MySQL, sessions, and cookies were balanced for complexity and usability, usually excluding the more advanced uses, such as streams . Finally, this book is not an exhaustive collection of every PHP function, but a majority selection of those appropriate for beginner to intermediate programmers. The most common or effective functions are included and some aliases are left out to reduce confusion, such as including is_int() and not is_long().
A few bits of nomenclature should be addressed and provided, to better understand the word/code used inside this book. In other words, here are some assumptions made inside this book that you should understand:
expr An expression ( e.g. $x == 1), including boolean
$variable A string, integer, float, array or boolean
$scalar - A string, integer, float, or boolean
$string A string variable or its equivalent ( e.g. "string" or 'string' )
$array An array variable or its equivalent ( e.g. array( 'one' , 'two' , 'three' ) ) key Represents the key (integer or string) of an array ( e.g. $array[ key ] ) value In relation to an array, represents the $variable value ( e.g.
array( ' value ') )
This book also shows all code using procedural PHP and standard syntax. However, you will find many tips will include the alternative syntax for control structures as to better allow you, the reader, to choose whichever you would prefer. Here is an example of both:
// Standard syntax if ($x == 1) {
echo 'Hello World!';
} else {
echo 'Goodbye World!';
}
// Alternative syntax if ($x == 1):
echo 'Hello World!'; else:
echo 'Goodbye World!'; endif;
Furthermore, the use of whitespace and indenting is for clarity and is completely up to your preference. Styles vary greatly in the community, so please be aware that the use of spaces or whitespace does not directly affect the PHP code.
The majority of the book is a collection of functions, their descriptions, example code, maybe an extra tip, and some related functions that may be of interest. All sample code will be accompanied by the sample output, and the output will have a gray background. The definition and example section is separated from the extraneous tip section by the use of three black clovers, centered on the line. It is meant as a simple visual clue to keep one from getting distracted or lost and confusing the next bit of information as required reading. All functions will be presented using the following formatting:
Boolean is usually used within an expression. While it is also evaluated as a variable, output results may vary and are noted within specific functions whenever possible
function name(input, [optional input]) Description/definition
Example:
Code with // comments
Output of code as seen through a web browser's output
See Also:
function simplified and relevant definition
function simplified and relevant definition
{Optional Section} Tip to help with usage or trick on using it Extra code related to the tip |
Output { [0] => Of [1] => Code } |
Thanks, and enjoy the show!
Miscellaneous Things You Should Know
Not everything fits into a neat little category, nor does everything in PHP belong in this reference book. However, sometimes they deserve a quick note and a little attention and shall be included here.
For portability and compatibility, always use the long form.
Long form:
" " (double quotes) Variables inside double quotes are evaluated for their values.
Example:
$string = 'Double Quotes'; echo "$string";
Escapes characters that should be evaluated literally when inside double quotations.