2 minute read

Introduction to PHP.

Overview

PHP enables scripting dynamic server-side actions.

Why PHP?

Without a dynamic scripting language, a web page is entirely static, where the time is exactly as typed:

<!DOCTYPE html>
<html lang="en>

<head>
    <meta charset="UTF-8">
    <title>Static vs Dynamic</title>
</head>

<body>
    <h1>This is Static</h1>
    <p>
        The time is 
        9:52 am
    </p>
</body>

</html>

With PHP we get a dynamic web page, where the time updates continuously:

<!DOCTYPE html>
<html lang="en>

<head>
    <meta charset="UTF-8">
    <title>Static vs Dynamic</title>
</head>

<body>
    <h1>This is Static</h1>
    <p>
        The time is 
        <?php>
            echo date(DATE_RSS) 
        ?>
    </p>
</body>

</html>

PHP Basics

Comments

Use slash star for multi-line comments and double-slash for single-line comments:

/* 
 * A multi-line comment 
 * this is
 */

// A single-line comment this is 

Variables

Define a php variable with the dollar symbol $:

<?php
    $myVariable = 0; 
?>

Insert a variable in a text string:

<?php
    $galaxy = "Andromeda"; 
    $atmosphere = "habitable"; 
    echo "The atmosphere in the {$galaxy} star system is {$atmosphere}.";
?>

Casting

This expression returns a floating point value:

<?php
    $alpha = 33;
    $beta = 12; 
    $charlie = $alpha / $beta ; 
    echo $charlie; 
?>

Prefix the desired format to cast the expression to return an integer:

<?php
    $alpha = 33;
    $beta = 12; 
    $charlie = $alpha / $beta ; 
    echo (int) $charlie; 
?>

Functions

Define a php function with the keyword function:

<?php
    function myFunction($myParam) {
        $counter = $myParam + 1; 
        return $counter; 
    }
?>

Conditionals

Use the conditional operators:

||    // -- OR  
&&    // -- AND  
!     // -- NOT 

Control of Flow

Use if, else and elseif conditional statements:

<?php
    $myStr = "This is a String"; 
    if (strtoupper($myStr) == $myStr) {
        echo "UPPER CASE"; 
    }
    elseif (strtolower($myStr) == $myStr) {
        echo "lower case"; 
    }
    else {
        echo "Mixed Case"; 
    }
?>

Use switch statements:

<?php
    $faveColour = "purple"; 
    switch ($faveColour) {
        case "red": 
            echo "You like Red!"; 
            break; 
        case "green": 
            echo "You like Green!"; 
            break; 
        case "blue": 
            echo "You like Blue!"; 
            break; 
        default: 
            echo "You don't like RGB!";
    } 
?>

Use the ternary operator as shorthand for if and else statements:

<?php
    $celcius = 10;
    echo $celcius <= 0 ? "stay indoors" : "go outside"; 
?>

Loops

A basic while loop:

<?php
    $gallons = 15; 
    while ($gallons < 20) {
        echo "Petrol is low - keep filling!";
        ++$gallons; 
    } 
    
?>

Alternative syntax:

<?php
    $gallons = 15; 
    while (++$gallons < 20) {
        echo "Petrol is low - keep filling! <br>";
    } 
?>

do…while syntax:

<?php
    $gallons = 15; 
    do {
        echo "Petrol is low - keep filling! <br>";
    } while (++$gallons < 20); 
?>

for loop:

<?php
    // times tables 
    $multiplier = 12; 
    for ($count = 1 ; $count <= 12; ++$count)
    echo "$count times $multiplier is " . $count * $multiplier . "<br>"; 
?>

QED

© Adam Heinz

17 June 2025

Categories:

Updated: