PHP Reference Book Blog

PHP Reference: Beginner to Intermediate PHP5

Archive for February, 2009

Feb-28-09

PHP & Ampersand: Passing by Reference

posted by Mario Lurig

The following PHP Reference excerpt is from pages 20-21.

& – Pass by Reference

References allow two variables to refer to the same content. In other words, a variable points to its content (rather than becoming that content). Passing by reference allows two variables to point to the same content under different names. The ampersand ( & ) is placed before the variable to be referenced.

Examples:

$a = 1;
$b = &$a; // $b references the same value as $a, currently 1
$b = $b + 1; // 1 is added to $b, which effects $a the same way
echo "b is equal to $b, and a is equal to $a";
b is equal to 2, and a is equal to 2



Use this for functions when you wish to simply alter the original variable and return it again to the same variable name with its new value assigned.

function add(&$var){ // The & is before the argument $var
$var++;
}
$a = 1;
$b = 10;
add($a);
echo "a is $a,";
add($b);
echo " a is $a, and b is $b"; // Note: $a and $b are NOT referenced
a is 2, a is 2, and b is 11

You can also do this to alter an array with foreach:

$array = array(1,2,3,4);
foreach ($array as &$value){
$value = $value + 10;
}
unset ($value); // Must be included, $value remains after foreach loop
print_r($array);
Array ( [0] => 11 [1] => 12 [2] => 13 [3] => 14 )


What tricks do you have for using the ampersand in PHP to pass by reference?
Leave them in the comments below!

Feb-25-09

Equal Sign in PHP: Equality and Not Equals

posted by Mario Lurig

The PHP equal sign can be used to assign the value of variable as well as evaluate a variable as part of an if-else statement or other conditional statement. However, there are subtle differences that are important for ensuring your PHP code is as accurate as possible. The following samples are from page 22.

The Equal Sign

Assignment ( = ): Assigns the value on the right to the variable on the left
Equality ( == ): Checks if the left and right values are equal
Identical ( === ): Checks if the left and right values are equal AND identical

Example:

$a = 1; // Sets the value of $a as 1 by assignment
$b = TRUE; // Sets the value of $b to the boolean TRUE
if ($a == $b){
echo 'a is equal to b.';
}
if ($a === $b){
echo 'a is identical and equal to b.';
}
a is equal to b. 

Not ( ! ), Not Equal to ( != ), Not Identical to ( !== )

Used in conditional statements to evaluate as true a FALSE result of an expression or if a value is NOT equal to the second value.

Example:

$a = 1;
if (!isset($a)){ // If the variable $a is NOT set then...
echo '$a is not set'; // The expression is TRUE if it is NOT set
// Since there is no ELSE statement, nothing is displayed
}
if ($a != 0){
echo '$a does not equal zero';
}
$a does not equal zero

Feb-16-09

Seven Downloads for Young Web Developers

posted by Mario Lurig

There are tons of books and tutorials on PHP, MySQL, Zend, and SEO on store shelves. There are even more resources found only on the internet. Sometimes you need something in the middle, content you can download legally and keep handy when you are offline. Below are a few completely free resources that most beginner or intermediate web developers (even if you do it just for yourself) will find useful.

PHP

1) PHP Reference: Beginner to Intermediate PHP5

Shameless self-promotion. A reference for many of the functions within PHP that serves as a quick go-to resource for checking syntax and remembering the nuances of many of the functions. It is available as a PHP book you can purchase in print, however the entire book is released under creative commons and available as a PHP reference PDF.

2) Object Oriented PHP Tutorial in PDF

Provided by killerphp.com and Stefan Mischook, this is a PDF version of his article on the topic of object orientated programming in PHP. It gives a conversational explanation to the basics. More information and the OOP PHP PDF is available over on killerphp.com.

3) Zend Framework: Surviving the Deep End

The Zend Framework can help developers organize and write more efficient PHP code for large projects and has become one of the top frameworks used online today. From the page…

“The book was written to guide readers through the metaphorical ‘Deep End’. It’s the place you find yourself in when you complete a few tutorials and scan through the Reference Guide, where you are buried in knowledge up to your neck but without a clue about how to bind it all together effectively into an application.”

While available online, there is a link to downloading the PDF version in the bottom right. Check out the Zend Framework survival guide.

HTML, CSS, AJAX

4) The Woork Handbook

Another compilation of online articles compiled and organized as an offline document. From the page…

“The Woork Handbook is a free eBook about CSS, HTML, Ajax, web programming, Mootools, Scriptaculous and other topics about web design… directly from Woork!”

This isn’t a full study of any single topic, but is filled with tidbits. Grab the Woork Handbook.

5) Added Bytes Cheat Sheets (formerly ILoveJackDaniels)

While the site’s name has changed, the great resources have not. Cheat sheets are designed to cram (in a useful way) tons of information into the front and back of an 8.5″ x 11″ sheet of paper. There is little excuse for not keeping these handy. Grab the cheat sheets for HTML, CSS, RegEx, Mod_Rewrite, and more.

MySQL

6) MySQL Manual

All MySQL documentation is available as a downloadable file. Choose from various options for the MySQL documentation.
Editor’s Note: I had trouble finding a good, free, legal resource for MySQL that wasn’t hyper specific and was user friendly. I hope others have suggestions they can leave in the comments.

SEO (Search Engine Optimization)

7) Beginner’s Guide to Search Engine Optimization

SEOMoz.org is an amazing website for learning the complex world of SEO with a very clear, user-friendly tone. Besides that, they offer a bunch of tools for helping your new website get better in the eyes of search engines (aka Google). Available as HTML, MS Word, or an OpenOffice document, you can get a copy of the Beginner’s Guide to Search Engine Optimization over on SEOMoz.org.

Tags: , ,
Feb-4-09

Regular Expression Syntax (RegEx) Sample

posted by Mario Lurig

The handy Regular Expression Syntax from the PHP book (pages 149-150). Check out the free PDF if you don’t have it already!

Regular Expression Syntax

^ – Start of string
$ – End of string
. – Any single character
( ) – Group of expressions
[] – Item range ( e.g. [afg] means a, f, or g )
[^] – Items not in range ( e.g. [^cde] means not c, d, or e )
- (dash) – character range within an item range ( e.g. [a-z] means a through z )
| (pipe) – Logical or ( e.g. (a|b) means a or b )
? – Zero or one of preceding character/item range
* – Zero or more of preceding character/item range
+ – One or more of preceding character/item range
{integer} – Exactly integer of preceding character/item range ( e.g. a{2} )
{integer,} – Integer or more of preceding character/item range ( e.g. a{2,} )
{integer,integer} – From integer to integer (e.g. a{2,4} means 2 to four of a )
– Escape character
[:punct:] – Any punctuation
[:space:] – Any space character
[:blank:] – Any space or tab
[:digit:] – Any digit: 0 through 9
[:alpha:] – All letters: a-z and A-Z
[:alnum:] – All digits and letters: 0-9, a-z, and A-Z
[:xdigit:] – Hexadecimal digit
[:print:] – Any printable character
[:upper:] – All uppercase letters: A-Z
[:lower:] – All lowercase letters: a-z

PERL Compatible (PCRE) only ( preg_*() )

/ – delimiter before and after the expression

Character classes:

\c – Control character
\s – Whitespace
\S – Not whitespace
\d – Digit (0-9)
\D – Not a digit
\w – Letter, Digit, Underscore [a-zA-Z0-9_]
\W – Not a letter
\x – Hexadecimal digit
\O – Octal digit

Modifiers:

i – Case-insensitive
s – Period matches newline
m – ^ and $ match lines
U – Ungreedy matching
e – Evaluate replacement
x – Pattern over several lines

Feb-2-09

array_key_exists() Function Index Correction

posted by Mario Lurig

Thanks to John for submitting this correction!

In the Function Index (pg. 161), array_key_exists() is listed as page 82. However, it is actually on page 79.

Feel free to submit a correction yourself if you find any!