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?
The web browser, when it comes across files with a *.php
extension looks for special tags:
<? ?>
<php ?> //use these in this course
<script language="php"> </script>
Processes what is inside the tags, then sends html (i.e. text) to the requesting browse
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
myVariableName
MyMethodName
my_Variable_Name
+
$b = $a + 3;
adds values-
$b = $a - 3;
subtracts values*
$b = $a * 3;
multiplies values/
$b = $a / 3;
divides values%
$b = $a % 3;
modulus operator - modulus determines the remainder=
assignment operator, place the value of the right-hand operand into the memory spot of the left-hand operand+=
addition assignment operator-=
subtraction assignment operator.=
string concatenation (the period will append the right-hand operand to the end of the left-hand operand)a.k.a. Relational Operators
==
logical equal to!=
not equal to>
greater than<
less than>=
greater than or equal to<=
less than or equal to&&
Logical AND - Implies true if and only if both operands are true||
Logical OR - Implies true if either operand is truePHP 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>
Date and Time:
date
Function <?php echo date("F d, Y") ?> - June 05, 2023
<?php echo date("Y") ?> - 2023
<?php echo date("m.d.y") ?> - 06.05.23
<?php echo date("Y-m-d H:i:s") ?> - 2023-06-05 11:34:23
Using arrays in PHP has lots of variations, for now we will stick to the basics.
<?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 )
<?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
<?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>";
}
?>