Chapter 2 Notes
-
Comments in PHP:
Below are examples of comments in PHP:
//This is a comment
/*This is a comment using
mutiple line.
*/ -
Arrays in PHP:
Arrays example:
$cars = array("Toyota", "BMW", "Honda", "Ford", "Jaguar");
To display Honda, which is the 2nd item, type: echo $cars[2]; -
Constants:
Constants do not change.
For example:
define(PI, 3.1415927);
$pitimes2 = PI * 2;
print ($pitimes2);
Once contants are set, they are used throughout the program.
They do not require $ sign.
I had to use the print function to print the statement on the screen. The echo statement did not work. Example 03.12 -
Functions:
Functions separate out and encapsulate sections of code that perform a particular task more than once.
Very simple function:
function myMessage() {
echo "Hello world!";
}
Then we call the function:
myMessage();
Example 03.14Another example of a function:
Example 03.15
Variable Typing:
The substr() function returns part of a string.
For example the code below:
$number = 12345 * 67890;
echo substr($number, 3, 3);
$number = 12345 * 67890;
echo substr($number, 3, 3);
will return the values "838102050 102".
First, it multiplies 12345 by 67890 to get the $number value.
Then, the substr() function will grap the 3 value (i.e. the count starts from 0)
and then grabs the 3 numbers.
Example 03.11