Learning PHP 101 – Introduction

PHP is one of the most powerful server side programing languages in use to date. It powers most of the sites you see on the web and is a great way to kick start your developer career off.

We all learn to code in our own way and writing it down is the best way for me to solidify information. But of course it also means I have to start from the basics.

You can distinguish a PHP file by its extension .php, which tells the interpreter there is PHP to be processed. Below is a typical index.php file.

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
          <?php
            echo "My first line of PHP"; 
          ?>
        </p>
    </body>
</html>

When you run the PHP script the compiler will interpret anything that resides within the opening and closing PHP tags <?php?>.

Echo

One of the first statements to learn is echo. This simple statement lets you output one or more strings. All strings of text must written within quotes " " and end with a semicolon.

When talking about echo the term function is sometimes loosely applied.  echo is not a function but a language construct.
<?php
     echo "This is a string of text"; 
?>
Tip: Dont forget to terminate your strings with a semi-color (;) its a very common mistake.

Strings

A string is a sequence of characters such as A-Z, 0-9 as well as symbols.

A string is written between quotes and can be written as a single string (as seen above) or you can concatenate multiple strings together using the concatenation operator (.).

<?php
     echo "This is a" . " " . "string of text"; 
?>

Strings can also cross over multiple lines which helps to make code more readable.

<?php
    echo "This is a" . " " .
        "string of text";
?>

Even though this string is on two lines, it still lacks formatting and the output will still be on a single line.

Arithmetic

Like most programming languages, PHP can also do math.

<?php
    echo 10 + 4; // 14
    echo 10 - 4; // 6
    echo 10 * 4; // 40
    echo 10 / 4; // 2.5
    echo 10 % 4; // 2
?>

Arithmetic operators as of PHP 5.5 are as follows.

Operator Name Example Result
+ Addition $x + $y Sum of $x and $y
Subtraction $x – $y Difference between $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x and $y

Variables

Variables are a way for us to “save” values for later use. Each variable can have a specific case-sensitive name which helps us identify its purpose.

All variable names start with a dollar sign ($).

<?php
     $name = "Matthew";
     $age = 27;
?>

Above I have two variables $name and $age which represent a string and a number.

Putting them together

Now that we know some basics we can put them all together.

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <p>
        <?php
            $name = "Matthew";
            $age = 27;

            echo "My name is " . $name;
            echo "I am " . $age . " years old";
        ?>
        </p>
    </body>
</html>

Which would output My name is MatthewI am 27 years old.

Notice the lack of formatting. We will correct that later.

Comparisions

Comparison operators are used to compare two values. The values can be numbers or strings.

Operator Name Example
> Greater than $x > $y
< Less than $x < $y
<= Less than or equal to $x <= $y
>= Greater than or equal to $x >= $y
== Equal to $x == $y
!= Not equal to $x != $y
=== Identical to $x === $y
!== Not identical to $x !== $y

Comparisons are either true or false.

<?php
    $a = 16;
    $b = 4;

    $a > $b;   // true
    $a < $b;   // false
    $a <= $b;  // false
    $a >= $b;  // true
    $a == $b;  // false
    $a != $b;  // true
    $a === $b; // false
    $a !== $b; // true
?>

So far you have learned how to echo out the output, assign variables, do some simple math and make comparisons.

Next: we will look at Conditionals and Control Flows

In Category: PHP tutorials

Matthew Horne

I am a web developer from the United kingdom who taught himself PHP and JavaScript and continues to build on those skills as well as learn new skills.

Show 2 Comments
  • Eoin O'Siluria 15th July 2015, 6:56 am

    Very neat and simple explanations Matt, thanks for sharing.

    • Matthew Horne 15th July 2015, 7:17 am

      Thanks Eoin.

      I have wanted to do this for some time and now I feel the time is right.

Leave a Comment