PHP01 - Building Blocks, Flow

eg0001.php The first PHP script ("view - source" eg0001.txt) (P32)
Edit PHP with any text editor. Save php scripts with .php extension. Some servers support .phtml or .php4.

Beginning and ending a block of PHP statement

Tag Style Start Tag End Tag  
Standard tags <?php ?> Can work on any configuration
Short tags <? ?> Must be enabled in php.ini
ASP tags <% %> Must be enabled in php.ini
Script tags <SCRIPT LANGUAGE="PHP"> </SCRIPT> Can work on any configuration

To activate recognition for short tags and asp tags, change php.ini:
short_open_tag = On;
asp_tags = On;

eg0002.php Combing HTML and PHP (eg0002.txt) (P35)

Adding comments to PHP code
// this is a comment
# this is a comment
/*Multiline comments
Multiline comments*/


The Building Blocks

Variables
A variables consists of a name that you can choose, preceded by a dollar ($) sign. The variable name can include letters, numbers and the underscore character (_). Variable names cannot include spaces or characters that are not alphanumeric. They should also begin with a letter or an underscore.

Variables are case-sensitive. ("$variable" and "$Variable" are two different constructs)
PHP is not case-sensitive although variables are case-sensitive.

PHP 4 is loosely typed, which means that it will calculate data types as data is assigned to each variable.
Refer to Pg 41 for datatypes table. (Integer, Double, String, Boolean, Object, Array + Resource, NULL)

eg0003.php Testing the type of a variable gettype() (eg0003.txt) (P44)

eg0003a.php Changing type with settype() (eg0003a.txt) (P47)


Using Numbers
eg0005.php Adding, Subtracting, Multiplying and Dividing (eg0005.txt)

printf ("%01.2f", $Amount) => print $Amount using 0 to pad extra spaces, with a least 1 digit to the left of the decimal and with 2 digits to the right of the decimal.

eg0006.php Mathematical functions round(); ceil(); floor();abs(); srand(); rand(); (eg0006.txt)

Assignment Operator
=

Arithmetic Operators Table, Pg 51 Table
+ + Addition
- - Subtraction
/ / Division
* * Multiplication
% % Modulus

Combined Arithmetic Operators, Pg 52 Table
+= $x += 5
-= $x -= 5
/= $x /=5
*= $x *= 5
%= $x %= 5
.= $x .= " test"

Comparison Operators
= = = = Equivalence
!= != Non-Equivalence
= = = = = = Identical
> > Greater than
>= >= Greater than or equal to
< < Less than
<= <= Less than or equal to

Logical Operators
|| Or
or Or
xor Xor
&& And
and And
! Not


Constants Pg57
define ("CONSTANT_NAME", 58);
eg0007.php Define a contant eg0007.txt

 

Going with the Flow

The if Statement Pg64
if (expression) {
// code to execute if expression is true
}
eg0008.php An if statement eg0008.txt

if (expression) {
// code to execute if expression is true
} else {
// code to execute in all other cases
}
eg0009.php An if statement that uses else eg0009.txt

The switch Statement Pg67
switch (expression) {
case result1: //execute this if expression results in result1
break;
case result2: //execute this if expression results in result2
break;
default: //execute this if no break statement has been encountered hitherto
}
eg0010.php A switch statement, eg0010.txt

Using the ? Operator Pg69
The ? operator is similar to the if statement but returns a value derived from one of two expressions seperated by a colon.
(expression)?returned_if_expression_is_true:returned_if_expression_is_false;
eg0011.php the ? operator, eg0011.txt

Loops

The while Statement Pg70
while (expression) {
// do something
}
eg0012.php the while statement, eg0012.txt

The do..while Statement Pg71
do {
// code to be executed
} while (expression);
eg0013.php the do..while statement, eg0013.txt

The for Statement Pg72
for (initialization expression; test expression; modification expression) {
// code to be executed
}
eg0014.php the for statement, eg0014.txt

eg0015.php the Nesting Two for Loops, eg0015.txt