PHP
PHP
<?php
?>
A PHP file normally contains HTML tags, and some PHP scripting code.
A simple .php file with both HTML code and PHP code:
<!DOCTYPE html>
<html>
<body>
<?php
?>
</body>
</html>
In PHP, keywords (e.g. if, else, while, echo, etc.), classes, functions, and user-defined functions are
not case-sensitive.
<html>
<body>
<?php
?>
</body>
</html>
<html>
<body>
<?php
$color = "red";
?>
</body>
</html>
Comments
/* This is a
multi-line comment */
Variables
$x = 5;
$y = "John";
PHP if Statements
if...else statement - executes some code if a condition is true and another code if that
condition is false
if...elseif...else statement - executes different codes for more than two conditions
<html>
<body>
<?php
if (5 > 3) {
echo "Have a good day!";
}
?>
</body>
</html>
Ex.2
<html>
<body>
<?php
$t = 14;
?>
</body>
</html>
Ex.3
<html>
<body>
<?php
$t = date("H");
} else {
?>
</body>
</html>
Short Hand If
<html>
<body>
<?php
$a = 5;
if ($a < 10) $b = "Hello";
echo $b
?>
</body>
</html>
Nested if
<html>
<body>
<?php
$a = 13;
if ($a > 10) {
echo "Above 10";
if ($a > 20) {
echo " and also above 20";
} else {
echo " but not above 20";
}
}
?>
</body>
</html>
The switch statement is used to perform different actions based on different conditions.
<html>
<body>
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
echo "Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
</body>
</html>
PHP Loops
while - loops through a block of code as long as the specified condition is true
do...while - loops through a block of code once, and then repeats the loop as long as the
specified condition is true
While loop
<html>
<body>
<?php
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
?>
</body>
</html>
The PHP do...while Loop
The do...while loop will always execute the block of code at least once, it will then check the
condition, and repeat the loop while the specified condition is true.
<html>
<body>
<?php
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);
?>
</body>
</html>
The for loop is used when you know how many times the script should run.
Syntax
// code block
<html>
<body>
<?php
?>
</body>
</html>
Break statement
<html>
<body>
<?php
if ($x == 3) break;
?>
</body>
</html>
The foreach loop - Loops through a block of code for each element in an array or each property in an
object.
The most common use of the foreach loop, is to loop through the items of an array.
<html>
<body>
<?php
?>
</body>
</html>
What is an Array?
An array is a special variable that can hold many values under a single name, and you can access the
values by referring to an index number or name.
<html>
<body>
<pre>
<?php
var_dump($cars);
?>
</pre>
</body>
</html>
Output
array(3) {
[0]=>
string(5) "Volvo"
[1]=>
string(3) "BMW"
[2]=>
string(6) "Toyota"
The var_dump() function in PHP is used to display structured information about one or more
variables, including its type and value. Arrays and objects are explored recursively with values
indented to show structure
Array Items
Array items can be of any data type.
The most common are strings and numbers (int, float), but array items
can also be objects, functions or even arrays.
You can have different data types in the same array
Indexed Array
<html>
<body>
<?php
echo $cars[0];
?>
</body>
</html>
Change Value
<html>
<body>
<pre>
<?php
$cars[1] = "Ford";
var_dump($cars);
?>
</pre>
</body>
</html>
To loop through and print all the values of an indexed array, you could use a foreach loop
<html>
<body>
<?php
?>
</body>
</html>
Index Number
The key of an indexed array is a number, by default the first item is 0 and the second is 1 etc., but
there are exceptions.
<html>
<body>
<pre>
<?php
$cars[0] = "Volvo";
$cars[1] = "BMW";
$cars[2] = "Toyota";
array_push($cars, "Ford");
var_dump($cars);
?>
</pre>
</body>
</html>
Ex.2
<html>
<body>
<?php
$cars[5] = "Volvo";
$cars[7] = "BMW";
$cars[14] = "Toyota";
array_push($cars, "Ford");
var_dump($cars);
?>
</pre>
</body>
</html>
Associative arrays are arrays that use named keys that you assign to them.
<html>
<body>
<pre>
<?php
var_dump($car);
?>
</pre>
</body>
</html>
Output
array(3) {
["brand"]=>
string(4) "Ford"
["model"]=>
string(7) "Mustang"
["year"]=>
int(1964)
<html>
<body>
<?php
echo $car["model"];
?>
</body>
</html>
Change Value
<html>
<body>
<pre>
<?php
$car["year"] = 2024;
var_dump($car);
?>
</pre>
</body>
</html>
To loop through and print all the values of an associative array, you could use a foreach loop
<html>
<body>
<?php
?>
</body>
</html>
<html>
<body>
<pre>
<?php
$fruits[] = "Orange";
//Output the array:
var_dump($fruits);
?>
</pre>
</body>
</html>
To remove an existing item from an array, you can use the array_splice() function.
With the array_splice() function you specify the index (where to start) and how many items you want
to delete
<html>
<body>
<pre>
<?php
array_splice($cars, 1, 1);
var_dump($cars);
?>
</pre>
</body>
</html>
You can also use the unset() function to delete existing array items.
Note: The unset() function does not re-arrange the indexes, meaning that after deletion the array
will no longer contain the missing indexes.
array_splice($cars, 1, 2);
The unset() function takes a unlimited number of arguments, and can therefore be used to delete
multiple array items
unset($cars[0], $cars[1]);
To remove items from an associative array, you can use the unset() function
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
unset($cars["model"]);
You can also use the array_diff() function to remove items from an associative array.
<html>
<body>
<pre>
<?php
$cars = array("brand" => "Ford", "model" => "Mustang", "year" => 1964);
var_dump($newarray);
?>
</pre>
</body>
</html>
array_pop($cars);
array_shift($cars);
PHP - Sort Functions For Arrays
sort($cars);
PHP supports multidimensional arrays that are two, three, four, five, or more levels deep.
The dimension of an array indicates the number of indices you need to select an element.
Volvo 22 18
BMW 15 13
Saab 5 2
Land Rover 17 15
$cars = array (
array("Volvo",22,18),
array("BMW",15,13),
array("Saab",5,2),
array("Land Rover",17,15)
);
echo "<ul>";
echo "<li>".$cars[$row][$col]."</li>";
echo "</ul>";
PHP Strings
$x = "John";
$x = "John";
Upper Case
$x = "Hello World!";
echo strtoupper($x);
Lower Case
$x = "Hello World!";
echo strtolower($x);
Replace String
$x = "Hello World!";
Reverse a String
$x = "Hello World!";
echo strrev($x);
Remove Whitespace
echo trim($x);
print_r($y);
/*
Result:
*/