🎁GET 20% OFF Using Code 👉SCHOOL👈 Until End 2024🎁

Function Declaration

A function is a unit of code that represents a sequence of statements. Functions can accept arguments or values and return a single value (or not).

Sample Code<?php
/* Function That Returns Age Based On Provided Birth Year
	The return type is string.
	The name of the function is myAge
	The number of arguments to the function is 1:
		The first argument is of type int.
 */
function myAge(int $birthYear): string
{
    // calculate the age by subtracting the birth year from the current year.
    $yearsOld = date('Y') - $birthYear;

    // return the age in a descriptive string.
    return $yearsOld . ($yearsOld == 1 ? ' year' : ' years');
}

echo 'I am currently ' . myAge(1995) . ' old.';
?>
<?php // Type Some Code ?>

Do while Loop

Checks if condition is true after executing a block of code in the loop. The block of code inside the loop will be executed at least once.

Sample Code<?php
$counter = 0;
do {
    $counter += 1;
    echo "Executing - counter is $counter.\n";
} while ($counter < 10);
?>
Sample Code<?php
$even_numbers = [2,4,6,8,10];
$counter = 0;
do {
	echo $even_numbers[$counter] . "\n";
	$counter +=1;
} while ($counter < count($even_numbers))
?>
<?php // Type Some Code ?>

While Loop

While loop executes a set of statements as long as a condition is true. It requires relevant variables to be ready such as an indexing variable to iterate through a loop.

Sample Code<?php
$counter = 0;

while ($counter < 10) {
    $counter += 1;
    echo "Executing - counter is $counter.\n";
}
?>
Sample Code<?php
$even_numbers = [2,4,6,8,10];
$counter = 0;
while ($counter < count($even_numbers)) {
	echo $even_numbers[$counter] . "\n";
	$counter +=1;
}
?>
<?php // Type Some Code ?>

Foreach Loop

Foreach works only on arrays and objects iterating and providing a specific variable one at a time.

Sample Code<?php
$even_numbers = [2,4,6,8,10];
foreach ($even_numbers as $even_number) {
  echo $even_number . "\n";
}
?>
Sample Code<?php
$array2D = [["Ford","F-Series",1984],["GMC","C-Series",1986]];
foreach ($array2D as $key => $data) {
  echo $data . “\n”;
}
?>
<?php // Type Some Code ?>

For Loop

The for loop is C-style in that it requires three parts, the initialization or loop variant, the condition and advancement to next iteration. It is used to iterate over data when referring to a changing index.

Sample Code<?php
$even_numbers = [2,4,6,8,10];
for ($i = 0; $i < count($even_numbers); $i=$i+1) {
    $even_number = $even_numbers[$i];
    echo $even_number . "\n";
}
?>

The initialization is the iterator variable $i which begins at zero.

The condition is evaluated in every loop and stops when the $i is larger than the length of the array.

The advancement to next iteration is the incrementalism of $i by one.

<?php // Type Some Code ?>

Multidimensional Arrays

Multidimensional arrays are two or more levels deep containing one or more arrays.

Sample Code<?php
$students = [
    "john_doe" => [
        “name” => “John”,
        “surname” => “Doe”,
        “age” => 25,
    ],
    “jane_doe” => [
        “name” => “Jane”,
        “surname” => “Doe”,
        “age” => 25,
    ]
];
print_r($people);
echo $people[“john_doe”][‘surname’];
?>
<?php // Type Some Code ?>

Two-dimensional Arrays

A two-dimensional array is an array of arrays requiring two indices to select an element.

Sample Code<?php
$int_array2D = [ [11,22,33], [111,222,333] ];
$array2D = [ ["Ford","F-Series",1984], ["GMC","C-Series",1986] ];
var_dump($int_array2D);
var_dump($array2D);
echo $int_array2D[0][1] . " ";
echo $array2D[0][1];
?>
<?php // Type Some Code ?>

Associative Arrays

Associate array stores a collection of named key and value pairs.

Sample Code<?php
$pickup_truck = ["brand"=>“Ford”, “model”=>”F-Series”, “year”=>1984];
echo $pickup_truck[“model”];
?>
<?php // Type Some Code ?>

References

Supported versions https://www.php.net/supported-versions.php

Language Reference https://www.php.net/manual/en/langref.php

Exceptions https://www.php.net/manual/en/language.exceptions.php

Chapter 7: Exceptions

During the execution of a program, it may be required to process special conditions called exceptions. An exception breaks the normal execution flow and executes a specified exception handler.

An exceptional event could occur from an error and the PHP will try to find a matching exception block. PHP uses try with at least one corresponding catch or finally block. An exception can be thrown or caught.

Table : Exceptions

FunctionDescriptionExample
tryExecutes only if exception triggers try { echo 2/0; } finally { return TRUE; }
throwTriggers an exception echo 2/0 or throw new Exception(‘Fails’);
catchRetrieves an exception try { echo 2/0; } catch (Exception $e) { echo $e->getMessage(); }