PHP Language

Goals

  • Be able to use the basic building blocks of PHP code

  • Use Psysh to run PHP code

  • Do simple calculations

  • Use and understand variables

  • Use and understand arrays

  • Use built-in functions

  • Use loops and conditional statements

  • Create and use a custom function

Steps

Step 1

Start by connecting to the virtual machine:

vagrant ssh

Step 2

Type this in the terminal to start Psy Shell, a program which lets you try out PHP code:

psysh

Yours might look different, but it should look something like this:

Psy Shell v0.1.4 (PHP 5.5.3 — cli) by Justin Hileman
>>>

Step 3

Next try some simple math that's built into PHP. Type these lines into psysh:

3 + 3;
7 * 6;

Step 4

Variables are names with values assigned to them. Each name starts with the $ symbol.

$my_variable = 5;

This assigns the value 5 to the name $my_variable.

Step 5

You can also do math with variables:

$my_variable + 2;
$my_variable * 3;

Step 6

To display the value of a variable, or any other value, you use echo. Try the following

echo $my_variable;
echo "Programming is easy!";
echo "13 * 8";
echo 13 * 8;
echo "My variable is: $my_variable";

Step 7

Variables can also hold more than one value. This is called an array.

$fruits = ["kiwi", "strawberry", "plum"];

Here we're using the variable $fruits to hold a collection of fruit names.

Step 8

Each value in an array has a key. These can be numbers or words:

$fruits = [2 => "kiwi", "berry" => "strawberry", 6 => "plum"];

If you do not specify a key, they will be numbers starting from zero, or the last number.

Step 9

You can easily add a new value to an array:

$fruits[] = "orange";

Step 10

You can also remove items from an array:

unset($fruits["berry"]);

Or remove the last item added:

array_pop($fruits);

Step 11

To inspect your array, you can use a built-in function

print_r($fruits);

For more detailed information about the variable, you can use var_dump()

var_dump($fruits);

Step 12

A conditional runs code only when a statement evaluates to true.

if ($my_variable > 1) {
  echo "YAY!";
}

This prints YAY! if the value stored in $my_variable is greater than 1.

Try changing the > in the conditional to a <.

< and > are called operators. There are many kinds of operators such as the +, -, * and / arithmetic operators.

Step 13

To perform an action if the conditional statement does not evaluate to true, we use else:

if ($my_variable < 1) {
    echo "YAY!";
} else {
    echo "OH NOES! 😢";
}

Step 14

You can combine these two, to add more conditionals using elseif:

if ($my_variable < 1) {
  echo "YAY!";
} elseif ($my_variable > 4) {
  echo "YIPPEE! 😍";
} else {
  echo "OH NOES! 😢";
}

Step 15

A common operation is to perform the same task on each value in an array. We call this looping, or iteration.

The simplest loop, is known as a while loop which will iterate until a given condition returns false:

$fruits = ["kiwi", "plum", "orange", "banana"];
$i = 0;
while ($i < sizeof($fruits)) {
  echo $fruits[$i] . PHP_EOL;
  $i++;
}

Step 16

Another common loop, is known as the for loop. This loop has three conditions:

  • The first condition is evaluated at the beginning no matter what
  • The second is evaluated at the beginning of each iteration, and will cause the loop to end when it returns false (just like a while loop!)
  • The third is evaluated at the end of each iteration
for ($i = 0; $i < sizeof($fruits); $i++) {
  echo $fruits[$i] . PHP_EOL;
}

Step 17

Another common loop is foreach which is the most common and easiest way to loop through an array:

foreach ($fruits as $fruit) {
  echo $fruit . PHP_EOL;
}

Step 18

While PHP features thousands of built-in functions, you can also make your own:

function happy()
{
  echo "YAY!";
}

You run the function just like a built-in function:

happy();

Step 19

function pluralize($word)
{
  return $word . "s";
}
pluralize("kiwi");

Functions can take parameters, which are the variables they work on. In this case, we made a function called pluralize that takes one parameter, a word.

Functions can also return data. In this case, pluralize returns the word with an 's' added to the end of it. In PHP, we use the return keyword to do this.

Step 20

When using PHP outside of Psysh, you should put it inside <?php and ?> tags:

<?php
// Code goes Here
?>

You may notice the two-slashes at the start of the middle line above. This is known as a comment and is ignored by PHP.

Step 21

As a shortcut, you can use what is known as "short echo tags" to echo simple variables:

<?=$variable;?>

Explanation

With this small amount of code, you've learned a lot of the syntax you will use every day!

Next Step: