Week 5 - Term Test 1 and PHP

PHP

PHP was changed later on to stand for: Hypertext PreProcessor

PHP is a server-side scripting language that has been one of the most popular languages used in millions of websites.

What can PHP Do?

What can PHP do?

PHP In-Line Tags

The web browser, when it comes across files with a *.php extension looks for special tags:

Processes what is inside the tags, then sends html (i.e. text) to the requesting browse

PHP Syntax

Variable Types

Variables can be of three (3) basic types:

Variables are declared always in the format:

$variable_name = "initial_value"; // quotes only for strings

Notice: No data type required

Variable Naming Rules:

Variable Naming Style Guide

PHP Arithmetic Operators

PHP Assignment Operators

PHP Comparison Operators

a.k.a. Relational Operators

PHP Logical Operators

PHP Provided Functions

PHP comes with a vast collection of pre-defined functions: http://ca3.php.net/manual/en/funcref.php

One that we will use extensively is echo() which will output text to a page (similar to cout in a C++ or Java console application: http://ca3.php.net/manual/en/function.echo.php

A simple PHP example

<html>
    <head>
        <title>First PHP Page</title>
    </head>
    <body>
        <h1><?php echo “Hello World!”; ?></h1>
    </body>
</html>

Some Built-In Function Examples:

Date and Time:

Arrays

Using arrays in PHP has lots of variations, for now we will stick to the basics.

Defining a basic array:

<?php 
    $names = array("Clint", "Sarah", "Raj", "David", "Ruba", "Stephen");
    $months = array("January", 2 => "March", "February", "April");
    $countries = array("CA" => "Canada", "US" => "United States", "MX" => "Mexico");
    print_r($names);
    echo "<br>";
    print_r($months);
    echo "<br>";
    print_r($countries);
    echo "<br>";
    print_r($names[2]);    
    echo "<br>";
    print_r($months[2]);    
    echo "<br>";
    print_r($countries["US"]);
?>

would output

Array ( [0] => Clint [1] => Sarah [2] => Raj [3] => David [4] => Ruba [5] => Stephen )
Array ( [0] => January [3] => March [4] => February [5] => April )
Array ( [CA] => Canada [US] => United States [MX] => Mexico )
Raj
March
United States

And iterating through the array:

<?php 
    echo <ul>
    for ($row =0; $row < 6; $row++) {
        echo <li>.$names[$row].</li>
    }
    echo </ul>
?>

would output

You can even mix data types within an array:

<?php 
    $vars = Array(0, 1, 2, "Clint", "test");

    print_r($vars[2]);
    echo "<br>";
    
    print_r($vars[3]);
    echo "<br>";
    
    print_r($vars[4]);
    echo "<br>";
    
    print_r($vars);
?>

outputs

2
Clint
test
Array ( [0] => 0 [1] => 1 [2] => 2 [3] => Clint [4] => test )

2 Dimensional Arrays

<?php 
$countryList = array(
    'CA'  => array(
        'AB'        => 'Alberta',
        'BC'        => 'British Columbia',
        'AB'        => 'Alberta',
        'BC'        => 'British Columbia',
        'MB'        => 'Manitoba',
        'NB'        => 'New Brunswick',
        'NL'        => 'Newfoundland/Labrador',
        'NS'        => 'Nova Scotia',
        'NT'        => 'Northwest Territories',
        'NU'        => 'Nunavut',
        'ON'        => 'Ontario',
        'PE'        => 'Prince Edward Island',
        'QC'        => 'Quebec',
        'SK'        => 'Saskatchewan',
        'YT'        => 'Yukon'),
    'US'  => array(
        'AL'        => 'Alabama',
        'AK'        => 'Alaska',
        'AZ'        => 'Arizona',
        'AR'        => 'Arkansas',
        'CA'        => 'California',
        'CO'        => 'Colorado',
        'CT'        => 'Connecticut',
        'DE'        => 'Delaware',
        'DC'        => 'District of Columbia',
        'FL'        => 'Florida',
        'GA'        => 'Georgia',
        'HI'        => 'Hawaii',
        'ID'        => 'Idaho',
        'IL'        => 'Illinois',
        'IN'        => 'Indiana',
        'IA'        => 'Iowa',
        'KS'        => 'Kansas',
        'KY'        => 'Kentucky',
        'LA'        => 'Louisiana',
        'ME'        => 'Maine',
        'MD'        => 'Maryland',
        'MA'        => 'Massachusetts',
        'MI'        => 'Michigan',
        'MN'        => 'Minnesota',
        'MS'        => 'Mississippi',
        'MO'        => 'Missouri',
        'MT'        => 'Montana',
        'NE'        => 'Nebraska',
        'NV'        => 'Nevada',
        'NH'        => 'New Hampshire',
        'NJ'        => 'New Jersey',
        'NM'        => 'New Mexico',
        'NY'        => 'New York',
        'NC'        => 'North Carolina',
        'ND'        => 'North Dakota',
        'OH'        => 'Ohio',
        'OK'        => 'Oklahoma',
        'OR'        => 'Oregon',
        'PA'        => 'Pennsylvania',
        'RI'        => 'Rhode Island',
        'SC'        => 'South Carolina',
        'SD'        => 'South Dakota',
        'TN'        => 'Tennessee',
        'TX'        => 'Texas',
        'UT'        => 'Utah',
        'VT'        => 'Vermont',
        'VA'        => 'Virginia',
        'WA'        => 'Washington',
        'WV'        => 'West Virginia',
        'WI'        => 'Wisconsin',
        'WY'        => 'Wyoming')
    );

print_r($countryList["CA"]["ON"]);
?>

outputs:

Ontario

2D Array with Looping Through

<?php
    $cars = array (
      array("Volvo",22,18),
      array("BMW",15,13),
      array("Saab",5,2),
      array("Land Rover",17,15)
    );
        
    for ($row = 0; $row < 4; $row++) {
      echo "<p><b>Row number $row</b></p>";
      echo "<ul>";
      for ($col = 0; $col < 3; $col++) {
        echo "<li>".$cars[$row][$col]."</li>";
      }
      echo "</ul>";
    }
    ?>