This module is an introduction to the PHP programming language. It will also serve as a programming reference that you can use throughout the course.
This module will cover variable data types, boolean branching structures, looping structures and variable scope.
Upon completion of this module, you should be able to:
echo.All source code in the notes will be syntax highlighted like so:
<?php
$beers_on_the_wall = 99;
$beers_on_the_wall--; // Take one down, pass it around.
echo $beers_on_the_wall . " bottles of beer on the wall.";
?>
The output obtained by running the source through an interpreter will be displayed in a monospace font on a grey background:
98 bottles of beer on the wall.
Side note: PHP 6 never launched, although some of that work (like namespaces and traits) was back-ported into PHP 5.3/5.4 in 2009/2010.
Here’s a short PHP program:
<?php
for ($i = 0; $i < 4; $i++) {
echo "Wello Horld!\n";
}
?>
The code looks a little bit like Java, Javascript or even C.
Note the <?php and ?> which respectively indicate the beginning and end of a section of PHP code.
Here is the same program written in Java.
class WelloHorld {
public static void main(String[] args) {
for (int i = 0; i < 4; i++) {
System.out.println("Wello Horld!");
}
}
}
When compared to PHP, the Java version has some extra baggage:
public static void main(String[] args) method required to wrap the mainline.System.out.prinln vs. echoAll PHP statements must end in a semicolon. The rules for semicolon usage are identical to those in Java.
Single-line and multi-line comments are also identical to Java comments.
<?php
// Single line-comments are fun times!
echo "Grinning ghosts ";
/* A multi-line
comment if I ever saw one. */
echo "boast the most roast!";
?>
Output:
Grinning ghosts boast the most roast!
You can define constants in PHP. A constant holds a value that you do not expect to change.
Constant names should be written in all capital letters, with underscores as spaces.
<?php
define("CLOSE_TO_PIE",3.1415926);
define("THE_ANSWER",42);
define("INSTRUCTOR_FULL_NAME","Kyle Andrew McGrath Geske");
echo INSTRUCTOR_FULL_NAME . " knows the answer: " . THE_ANSWER;
?>
Output:
Kyle Andrew McGrath Geske knows the answer: 42
PHP can store your data in variables. Variables are named placeholders that can store numbers, strings, and other data.
Variables in PHP begin with a dollar sign followed by the name of the variable. The variable name is case-sensitive.
<?php
$goats_in_the_kitchen = 13;
$goat_feet = $goats_in_the_kitchen * 4;
$feet_story = "Once there were " . $goat_feet . " goat feet in our kitchen.";
echo $feet_story;
?>
A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
PHP variables are dynamically typed, which means that they are not assigned types by the programmer. They are assigned a type by the interpreter based on their context.
Types in PHP:
unset().)The type of a variable can be changed at any time by reassignment or by casting.
There are a number of is_*() functions available (example is_bool(), is_int(), &c) and isset() to check for NULL.
<?php
$my_int = 12;
$my_float = (float)$my_int; // Casting int to a float.
unset($my_int); // Equivalent to: $my_int = NULL;
if (!isset($my_int) && is_float($my_float)) {
echo "All is well.";
}
?>
A string is series of characters. The most common ways to specify strings is by wrapping characters in single or double quotes.
If a string is specified using double quotes then any variables within curly-braces or escape characters it contains will be evaluated.
<?php
$name = 'Bobby McGee';
$fancy_string = "My name is {$name}.\n"; // The \n represents a newline.
$plain_string = 'My name is {$name}.\n';
echo $fancy_string;
echo $plain_string;
?>
Output:
My name is Bobby McGee.
My name is {$name}.\n
Strings can be concatenated together using the dot operator.
We can use strlen() to determine the length of a string.
<?php
$old_shopping_list = 'bacon,chickpeas,gasoline,grapes';
$new_shopping_list = $old_shopping_list . ',corn,tricycle';
$output = "Our list is " . strlen($new_shopping_list);
$output .= " characters long." // Add to an existing string by using .=
echo $output;
?>
Output:
Our list is 45 characters long.
When working with Boolean expressions PHP contains the constants TRUE and FALSE. However, most other variable types can be coerced into Booleans.
<?php
function boolean_test($var) {
if ($var) {
echo "{$var} is true\n";
} else {
echo "{$var} is false\n";
}
}
boolean_test(TRUE); boolean_test(FALSE);
boolean_test(0); boolean_test(1);
boolean_test(2); boolean_test(3);
boolean_test('string'); boolean_test('');
boolean_test([]); boolean_test([34]);
boolean_test(NULL);
?>
Output:
1 is true
is false
0 is false
1 is true
2 is true
3 is true
string is true
is false
Array is false
Array is true
is false
Some things to note here:
TRUE we get a 1.FALSE or NULL a empty string is printer.A boolean expression is a mathematical expression that results in either true or false.
Boolean expressions can contain the following common operators (and more):
| Symbol | Meaning |
|---|---|
| == | equal |
| != | not equal |
| > | greater than |
| < | less than |
| >= | greater than or equal |
| <= | less than or equal |
| && | Boolean ‘and’ |
| ¦¦ | Boolean ‘or’ |
Because PHP will auto-cast your variables when you compare them with different types, the double equals operator doesn’t always act in an expected manner.
For example, the following comparisons will evaluate to true.
42 == "42"
42 == true
0 == false
0 == "a"
"a" == true
0 != true
To avoid these WAT!? moments, use the triple equals operator === when making comparisons.
If statements work as expected. Note that elseif is one word when chaining statements.
<?php
if (($wobblypops > 4) || ($wobblypops === 0)) {
echo 'I pity da fool.';
} elseif ($wobblypops < 4) {
echo 'Gulp gulp.';
$wobblypops++;
}
?>
It’s sometimes nice to express long if/else chains as switch statements:
<?php
$i = 1;
switch ($i) {
case 0:
echo 'i equals 0' . $i;
break;
case 1: // Note: Fall-through
echo "falling\n";
case 2: // Note: Fall-through
echo "falling\n";
case 3:
echo 'i equals 1-3';
break;
default:
echo 'Nothing special about the default.';
}
?>
Note the “fall-through” cases for 1 and 2. Execution does not stop until we reach a break or the end of the switch block.
Output:
falling
falling
i equals 1-3
Strings can also be used as the target of a switch:
<?php
switch($beer)
{
case 'trad':
case 'fort garry dark':
case 'hoegaarden':
echo 'Good choice';
break;
default:
echo 'Please make a new selection...';
}
?>
A for loop is a counting loops that executes a block of code until a test becomes false. In PHP a for loop works pretty much as you would expect it to.
<?php
for($i = 100; $i > 0; $i--) {
echo $i;
}
?>
A while loop repeatedly executes a block of code while the supplied boolean expression is true.
<?php
$i = 1;
while ($i <= 10) {
echo $i++;
}
echo 'Oh my, a humdrum example if I ever saw one.';
?>
Arrays are created using square braces. The data stored in an array can be retrieved using a zero-based index.
<?php
$numbers = [1,2,3];
$to_do_list = ['finish homework','bathe','cook dinner'];
$to_do_list[] = 'practice taxidermy'; // Appended the array.
echo "Kyle {$to_do_list[0]} now!";
?>
Output:
Kyle finish homework now!
The length of an array can be determined using the count() function.
<?php
$bookshelf = ['Catcher in the Rye',
'Anathem',
'The Glassbead Game',
'The Manticore'];
echo "I own " . count($bookshelf) ." books.";
?>
Output:
I own 4 books.
The implode() function joins array elements into a string, while the explode() function splits a string into an array.
Both functions need to be supplied with a delimiter.
<?php
$poem = ['Mares eat oats','goat eat oats','little lambs eat ivy.'];
echo implode(' and ', $poem) . "\n";
$the_numbers = '4,8,15,16,23,42';
$dharma_hatch = explode(',',$the_numbers);
?>
Output:
Mares eat oats and goat eat oats and little lambs eat ivy.
The print_r() function prints human-readable information about an array.
<?php
$the_numbers = '4,8,15,16,23,42';
$dharma_hatch = explode(',',$the_numbers);
print_r($dharma_hatch);
?>
Output:
Array
(
[0] => 4
[1] => 8
[2] => 15
[3] => 16
[4] => 23
[5] => 42
)
For even more details use var_dump() instead of print_r.
A hash (sometimes called a map or a dictionary) is similar to an array. However, instead of storing and retrieving data using an index, hashes use keys to retrieve values. Each key is unique, and corresponds to a single value within the array. We call these key-value pairs.
In PHP, arrays are actually just hashes where the keys are zero-based integers.
Hash keys can also be strings.
<?php
$french_fruit = ['apple' => 'une pomme',
'pineapple' => 'un ananas',
'grapefruit' => 'un pamplemousse'];
echo "Aujourd'hui, je vais manger {$french_fruit['grapefruit']}.";
?>
Output:
Aujourd'hui, je vais manger un pamplemousse.
Hashes can contain hashes. Arrays can contain arrays. Hashes can contain Arrays. Arrays can contain Hashes.
<?php
// An Array of Hashes
$employees = [
['name' => 'Wally Glutton',
'position' => 'Ninja'],
['name' => 'Kyle Geske',
'position' => 'Instructor']
];
echo "{$employees[0]['name']} is a {$employees[0]['position']}.";
?>
Output:
Wally Glutton is a Ninja.
Here’s a hash where the values are arrays:
<?php
// A Hash of Arrays
$favourite_foods = [ 'jane' => ['cabbage','pizza'],
'john' => ['rainbow trout','chalk']];
$john_eats = $favourite_foods['john'];
echo "John eats {$john_eats[0]} and {$john_eats[1]}.";
?>
Output:
John eats rainbow trout and chalk.
Two things that should also be noted:
print_r() function also works for hashes.For example:
<?php
$mixed_bag = ['one',2,'three',4,[5 => 'five','six' => 6]];
print_r($mixed_bag);
?>
Output:
Array
(
[0] => one
[1] => 2
[2] => three
[3] => 4
[4] => Array
(
[5] => five
[six] => 6
)
)
A variable is considered NULL if:
NULL.unset().We can test for null using isset().
<?php
if (isset($new_variable)) {
echo "How very odd!?!";
}
$new_variable = 'I am no longer NULL.';
if (isset($new_variable)) {
echo $new_variable;
}
?>
Output:
I am no longer NULL.
We can also use isset() to test if particular hash keys have been assigned values.
<?php
function toy_box_contains($toy, $toy_box) {
if (isset($toy_box[$toy])) {
echo "You have {$toy_box[$toy]} {$toy}.\n";
} else {
echo "You have no $toy.";
}
}
$toy_box = ['tin soldiers' => 52,
'pick-up sticks' => 35,
'dice' => 6,
'transformers' => 4];
toy_box_contains('dice', $toy_box);
toy_box_contains('robots', $toy_box);
?>
Output:
You have 6 dice.
You have no robots.
Functions do things, they are the verbs of computer programming.
PHP functions can be defined to take zero of more parameters and can return any type of variable.
<?php
function square($x) {
return $x * $x;
}
echo "16 squared = " . square(16);
?>
If your function has nothing to return you need not use the return keyword. A function will automatically return when there are no more statements to execute.
<?php
function say_good_day($name) {
echo "A fine day indeed {$name}!";
}
say_good_day('Bobby McGee');
?>
The most common task performed on arrays is a traversal, the incrementally “visiting” of each array element.
<?php
$words = ['one','two','buckle','my','shoe'];
foreach ($words as $word) {
echo $word . ' ';
}
?>
Here’s the same code in Java:
String[] words = {"one", "two", "buckle", "my", "shoe"};
for (int i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
The foreach loop can also be used to traverse the key/value pairs of a hash.
<?php
$names = ['Wally' => 'Glutton',
'Sammy' => 'McGoo',
'Joni' => 'Mitchell'];
foreach($names as $firstname => $lastname) {
echo "{$lastname}, {$firstname}";
}
?>
Output:
Glutton, Wally
McGoo, Sammy
Mitchell, Joni
Sometimes we need to traverse complex nested structures of arrays and hashes.
When traversing such structures it is important to determine which portion of the structure is comprised of arrays and his portion is comprised of hashes.
We can then use nested foreach loops to traverse all of these hashes and arrays.
Here the $football_teams variable is an array of hashes detailing British football teams:
<?php
$football_teams = [
[ // This is the 0th hash in the $football_teams array.
'coach' => 'Bobby McGoo',
'teamname' => 'Strike Attack',
'players' => [ // 'players' is an array of hashes.
['name'=>'Jill Scott', 'position'=>'goalie'],
['name'=>'Willard Bee', 'position'=>'defense'],
['name'=>'Ace Batord', 'position'=>'centre']
],
'wins' => 5,
'loses' => 2
],
[ // This is the 1st hash in the $football_teams array.
'coach' => 'Sam Smith',
'teamname' => 'Running Scared',
'players' => [
['name'=>'Warren Lost', 'position'=>'goalie'],
['name'=>'Jane Mayor', 'position'=>'defense'],
['name'=>'Tim MacLand', 'position'=>'centre']
],
'wins' => 0,
'loses' => 4
]];
foreach($football_teams as $current_team) {
echo "{$current_team['teamname']} is coached by {$current_team['coach']}.\n";
echo "The team has {$current_team['wins']} wins and {$current_team['loses']} loses.\n";
echo "The team roster:\n";
foreach ($current_team['players'] as $current_player) {
echo "* {$current_player['name']} plays {$current_player['position']}\n";
}
echo "\n";
}
?>
Output:
Strike Attack is coached by Bobby McGoo.
The team has 5 wins and 2 loses.
The team roster:
* Jill Scott plays goalie
* Willard Bee plays defense
* Ace Batord plays centre
Running Scared is coached by Sam Smith.
The team has 0 wins and 4 loses.
The team roster:
* Warren Lost plays goalie
* Jane Mayor plays defense
* Tim MacLand plays centre
All variables in PHP (created outside of functions) exist in a global scope. Function have a separate local scope. Variables used inside a function leave scope when the function exits.
<?php
$magic_words = 'abracadabra';
function scope_scoop() {
echo "Inside Function 1: {$magic_words}\n";
$magic_words = 'Bibbidi-bobbidi-boo';
echo "Inside Function 2: {$magic_words}\n" ;
}
scope_scoop();
echo "Outside Function : {$magic_words}" ;
?>
Output:
Notice: Undefined variable: magic_words in C:\xampp\htdocs\php\text.php on line 4
Inside Function 1:
Inside Function 2: Bibbidi-bobbidi-boo
Outside Function : abracadabra
Scope defines where in a program a variable is accessible. Where ever a variable is not accessible by your program it is said to be “out of scope” or “outside the current scope.”
Up to this point when we’ve wanted to print from PHP we’ve been using echo:
<?php
$world = "Hello World";
echo $world;
?>
But we can also use what is called a short echo:
<?= $world ?>
Note that a trailing semicolon is not required.
When mixing PHP and HTML we’re going to try to keep the two as separate as possible.
A typical PHP + HTML script should look like:
<?php
/* Most of your PHP code should go here.
Including all variable initialization,
calculations, user input processing,
database queries, etc. */
$world = "Hello World";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Page Title</title>
</head>
<body>
<!-- To inject data from PHP variables into
your HTML you should use a short echo. -->
<p>Woot: <?= $world ?></p>
</body>
</html>
In the HTML portion of your code you should use as little PHP as possible. At most you should use PHP within your HTML to:
The HTML portion of your page should be considered a template only, not a place to code. Because spaghetti code.
When embedding PHP within your HTML you should be using what is called the “alternative syntax for control structures.”
<ul>
<?php foreach($todo as $item): ?>
<li><?=$item?></li>
<?php endforeach ?>
</ul>
<?php if ($logged_in): ?>
<h3>You Are Logged In</h3>
<?php elseif ($username === 'admin'): ?>
<h3>You Are the Admin</h3>
<?php else: ?>
<a href="login.php">Log In</a>
<?php endif ?>
Notice that there are no curly braces. Instead blocks begin with a colon and end with an endif, endfor, endforeach or endwhile.
…write HTML inside of your PHP inside of your HTML.*
This is what not to do:
<?php
$href = "http://example.com";
$title = "Snafu";
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Make Your Instructor Cry</title>
</head>
<body>
<!-- The following two lines are what I want you to avoid doing. -->
<p><?= "<span>WAT?!?</span>" ?></p>
<p><?= "<a href='" . $href ."'>" . $title . "</a>" ?></p>
</body>
</html>
There is always a way around embedding HTML in your PHP strings. For example:
<p><a href="<?= $href ?>"><?= $title ?></a></p>
############^^^^^^^^^^^^##^^^^^^^^^^^^^########
Code above hashes (#) is HTML. Code above carets (^) is PHP.
When processed this line would generate:
<p><a href="http://example.com">Snafu</a></p>
* Rules, of course, were meant to be broken. So try your best to keep this one, but if you really can’t find a way around it, go ahead and live dangerously. ;)
Another way to manage the complexity of the PHP embedded in your markup is to use a templating library like Twig or Blade. To use these libraries you’ll have to look into Composer, the PHP dependency manager.