Learning PHP: volume 1
You can use any style of programming with PHP, but some things work better than others - I will try to hit all the quirks in this section.
Syntax
You can insert PHP in to a HTML file by typing the first line of code shown here. The second line shows a one-line PHP script that includes a print statement; lines three through six show a multiple-line PHP script:
<?php starts PHP code. You can start it anywhere in a HTML page and the code is XML compatible, so always use the syntax <?php. You can also use things like <?, but that does not work with XML. ?> ends the PHP code.
When you include a PHP file within a PHP file, PHP starts reading the included file as HTML, so be sure to start the included file with <?php to get straight into PHP.
Every statement ends with a semicolon, ;, and statement blocks are surrounded with braces, { }, as in:
You can leave out the semicolon and braces in certain circumstances, but it makes code hard to understand and leads to mistakes when you modify code.
You can add a commend to the end of a line with // as in:
You can add blocks of comments with /* and */ as in:
Variables are created by adding $ to the front of a name, and a variable name is case-sensitive. When you add the following two lines to your code, you can print out $a and get the value assigned to $a, not the value assigned to $A:
Numeric variables do not require quotes. If you use quotes, the variable will be stored as a string, but will be converted to a number when needed as a number.
You can also use names defining logical values, including the following:
If, Then, Else
PHP automatically converts between data types, so testing data become more important. Consider the following sections of code:
In a language with strong typing and a binary field, the code will work perfectly because $a can contain only 0 or 1, and the code has an action for each value. The following code also works for that type of language:
What if another programmer comes along and defines the type as integer? The test using if else will most likely work as intended, because any value over 1 will work as 1. The code using the two if statements will not work, because it will ignore any value greater than 1. What if the same programmer uses a negative number; should -1 be treated like +1 or as a special case? Of there is no logical else, --- a warning message, like this:
PHP is more liberal with data types, and PHP 4 add the === comparison to let you check values by type; but you still cannot lock in data types, so you have to be careful when interpreting data. Here is an attempt at defining all the possibilities for what you might think is a simple binary field:
First, you have to check that a field exists, because PHP replaces missing fields with a variable of the same name. The next test is to check if a firls is true or false. You have to replace == with ===, because == lets PHP convert numeric and string fields to true or false, whereas === checks that the field is the correct type in the first place.
You can use any style of programming with PHP, but some things work better than others - I will try to hit all the quirks in this section.
Syntax
You can insert PHP in to a HTML file by typing the first line of code shown here. The second line shows a one-line PHP script that includes a print statement; lines three through six show a multiple-line PHP script:
PHP:
<?php ?>
<?php print("Sup"); ?>
<?php
$message = "Sup";
print($message);
?>
<?php starts PHP code. You can start it anywhere in a HTML page and the code is XML compatible, so always use the syntax <?php. You can also use things like <?, but that does not work with XML. ?> ends the PHP code.
When you include a PHP file within a PHP file, PHP starts reading the included file as HTML, so be sure to start the included file with <?php to get straight into PHP.
Every statement ends with a semicolon, ;, and statement blocks are surrounded with braces, { }, as in:
PHP:
if("a" == "a") {
print("true");
}
You can leave out the semicolon and braces in certain circumstances, but it makes code hard to understand and leads to mistakes when you modify code.
You can add a commend to the end of a line with // as in:
PHP:
print("true"); // This is a comment.
You can add blocks of comments with /* and */ as in:
PHP:
/* This is a pretty long
comment don't you think?
This can go as far as you want
til you add this -> */
Variables are created by adding $ to the front of a name, and a variable name is case-sensitive. When you add the following two lines to your code, you can print out $a and get the value assigned to $a, not the value assigned to $A:
PHP:
$a = "the contents here";
$A = "different contents here";
Numeric variables do not require quotes. If you use quotes, the variable will be stored as a string, but will be converted to a number when needed as a number.
PHP:
$a = 20;
You can also use names defining logical values, including the following:
PHP:
$a = null;
$b = true;
$c = false;
If, Then, Else
PHP automatically converts between data types, so testing data become more important. Consider the following sections of code:
PHP:
if($a == 0) {
//do this
}
if($a == 1) {
//do this
}
In a language with strong typing and a binary field, the code will work perfectly because $a can contain only 0 or 1, and the code has an action for each value. The following code also works for that type of language:
PHP:
if($a == 0) {
// do this
} else {
// do this
}
What if another programmer comes along and defines the type as integer? The test using if else will most likely work as intended, because any value over 1 will work as 1. The code using the two if statements will not work, because it will ignore any value greater than 1. What if the same programmer uses a negative number; should -1 be treated like +1 or as a special case? Of there is no logical else, --- a warning message, like this:
PHP:
if($a == 0) {
// do this
} elseif($a == 1) {
// do this
} else {
print("Warning message");;
}
PHP is more liberal with data types, and PHP 4 add the === comparison to let you check values by type; but you still cannot lock in data types, so you have to be careful when interpreting data. Here is an attempt at defining all the possibilities for what you might think is a simple binary field:
PHP:
if(!isset($a)) {
// some sort of warning
} elseif($a === false) {
// do this
} elseif($a === true) {
// do this
} elseif($a == 0) {
// do this
} elseif($a < 0) {
// do this
} else {
// do this
}
First, you have to check that a field exists, because PHP replaces missing fields with a variable of the same name. The next test is to check if a firls is true or false. You have to replace == with ===, because == lets PHP convert numeric and string fields to true or false, whereas === checks that the field is the correct type in the first place.