Learning PHP 5 Book Review
- Sklar, David. Learning PHP 5. Beijing: O’Reilly, 2004.
Learning PHP 5 is an entry level book on PHP. PHP is a server side scripting language that allows one to add dynamic content to websites. In contrast from other scripting languages, such as JavaScript, PHP executes all of its operations on a server rather than on the computer of the person who is using a web page. PHP can be easily embedded into HTML. It is a very popular language because it is free, open source, cross-platform compatible, has a rather easy learning curve, and allows you to hide your code. When a client requests a PHP page from a server, the server uses a PHP interpreter to parse the request and convert the PHP program embedded in the HTML into HTML.
Sklar offers several basic salient points in his first chapter. They include the following points:
- PHP uses <?php as its start tag.
- PHP uses ?> as its end tag. The PHP interpreter ignores everything outside these two tags.
- PHP ignores white space.
- PHP separates statements by a semicolon. Best practice for coding puts each statement on a different line for readability.
- PHP can use several different types of comments, including // and ##. // is prefered. Multi-line comments begin with /* and end with */.
Chapter two addresses how to work with text and numbers. It introduces several important concepts.
- A piece of text is called a string.
- Strings can be surrounded by either the single or double quote marks. The single mark outputs the string exactly as it occurs. The double allows one to introduce variables.
- The backward slash is a delimeter that allows one to escape a string.
- A period can be used to combine strings
- PHP can be used to validate (with trim or strlen) or format (with printf) text. It can make all letters in a string uppercase, lowercase, or title-case.
- PHP can replace text within a string.
- Variables cannot begin with a number. They can include numbers, letters, and the underscore.
- PHP can be used to accomplish mathematical tasks. It distinguishes between intergers and floating point numbers (ie. numbers with decimals.)
In the third chapter, Sklar lays out the ways various statements and operators are used in PHP to allow you to make decisions and repeat yourself. Every PHP statement has a value of either true or false. Most statements are true, but there are five types of false statements.
- Intergers equal to 0
- Floating point numbers equal to 0.0
- Empty strings
- Strings that only contain 0
- An empty array
Sklar then discusses in detail the function of several constructs and operators.
- The
if ()construct tests for the truth of a statement - The
else ()construct tests for the falsity of a statement - The
elseif ()construct tests for the falsity of a prior statement and then executes the code in the next statement if it is true - The operator
==tests for the equality of two values. This is to be distinguished from the operator=, which sets the value of a variable. The less than and greater than operators can be used in a similar fashion, and they can also be used with the equal operator. All of these can be used with numbers and strings. - The
absoperator produces the absolute value of an argument - When comparing strings, the interpreter determines whether the first character each string is an interger or a letter. If either string begins with a letter, the interpreter uses dictionary ordering. If both begin with a number, the interpreter uses number ordering.
- The
strcmp ()construct forces dictionary order comparisons. Thestrcasecmpdoes the same thing, except it ignores case. - The operater
!serves to negate a statement. - The AND and OR operators are
&&and||respectively. - The
while ()construct executes a block of code until it meets with a false condition. This can provide a never ending loop. - The
for ()construct expects initialization, test, and iteration expressions. - Two constructs are used to repeat or loop. The
while ()construct repeatedly tests for the truth of a statement and repeats the code connected to the construct until it finds the statement false. - The
for ()construct is a bit more complex than the while construct, although it does something similar. It requires 3 parameters, initialization, test, and iteration. It too repeatedly tests for the truth of a statement and repeats the code connected to the construct until it finds the statement false. Both or these, especially thewhile ()construct, need something to end them.
