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

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(); }

Chapter 6: Objects

An object is a compound data type. Values of more than one type can be stored together in a single variable. An object is an instance of either a built-in or user defined class. An object is initialized by creating a new statement to instantiate a class.

Multiple objects can be created from a class. Each object has all the properties and methods defined in a class but will have different property values.

Table : Common Functions To Manipulate Objects

FunctionDescriptionExample
is_object()Check if a variable is an objectis_object($obj);
isset()Check if non-null variable declaredisset($obj);
var_dump()Dumps information about variablevar_dump($obj);



Sample Code<?php
$obj = (object)["name" => “John”];
var_dump( isset($obj->name) );
echo $obj->name;
?>
<?php // Type Some Code ?>

Chapter 5: Functions

A function is a subprogram that can be called by code externally or internally to the function. It is composed of a sequence of statements called the function body. Values can be passed to a function as parameters or arguments and the function can return a value if applicable.

PHP comes with built-in functions that can be called directly to perform a specific task. It is also possible to create custom functions also called user defined functions.

A function is therefore a block of statements that can be used repeatedly in a program. It will not execute automatically and must be executed by a call to the function.

Table : Common Functions Used To Manipulate Custom Functions

FunctionDescriptionExample
includeIncludes and evaluates fileinclude(‘another_file.php’);
include_onceIncludes file once to avoid issuesinclude_once(‘another_file.php’);
requireRequires file otherwise haltsrequire(‘another_file.php’);
require_onceRequires file once to avoid issuesrequire_once(‘another_file.php’);

Chapter 4: Loops

Loops execute one or more statements up to a specific number of times. Loops allow repetitive tasks to be executed using minimal code. Loops allow complex tasks to be simplified by automating similar operations.

Table : Loops

TypeDescriptionSyntax
forloops through code block a specific number of timesfor (expr1; expr2; expr3) statement
foreachloops through code block for each element in an array or objectforeach (iterable_expr as $val) statement
whileloops through code block until specific condition is falsewhile (expr) statement
do-whileloops through code block one, repeats until specific condition is falsedo { statement } while (expr);

Table : Common Functions Used With Loops

FunctionDescriptionExample
breakEnd execution of current structurebreak;
continueSkip rest of current loop iterationcontinue;
Sample Code<?php
// Display Even Numbers Without Loops
$even_numbers = [2,4,6,8,10];
echo $even_numbers[0]. " ";
echo $even_numbers[1]. " ";
echo $even_numbers[2]. " ";
echo $even_numbers[3]. " ";
echo $even_numbers[4];
?>
<?php // Type Some Code ?>

Chapter 3: Arrays

An array specifies a variable that can be indexed as a list in rows and columns. The first index is zero as is common in most programming languages.

Table : Common Functions For Manipulating Arrays

FunctionDescriptionExample
unset()Remove an itemunset($even_numbers[3]);
print_r()Print all keys and elementsprint_r($even_numbers);
count()Counts all elementscount(($even_numbers);
reset()Returns first array elementreset($even)numbers);
array_push()Pushes element(s) to endarray_push($even_numbers, 14);
array_pop()Pop the element off the endarray_pop($even_numbers);
array_unshift()Prepend element(s) to beginningarray_unshift($even_numbers, 0);
array_merge()Merge one or more arraysarray_merge($even_numbers,[16,14]);
sort()Sort in ascending orderarray_sort($even_numbers);
array_slice()Extract a slicearray_slice($even_numbers, 2);
array_keys()Returns all keysarray_keys($even_numbers);
Sample Code<?php
$even_numbers = [2,4,6,8,10];
$first_even_number = $even_numbers[0];
$second_even_number = $even_numbers[1];

echo "The first even number is $first_even_number\n";
echo "The second even number is $second_even_number\n";
?>

It is easy to add more items to an array by appending to the end of the list by assigning an index. The 6th item will have the index 5 because the first index is zero.

Sample Code<?php
$even_numbers = [2,4,6,8,10];
$even_numbers[5] = 12;
?>
<?php // Type Some Code ?>

Logical Operators

PHP logical operators are used to combine conditional statements. Each operand is a conditional that can be evaluated to a true or false value. The value of the conditions determines the overall value of the operator or grouping.

Logical operators are use to control flow of the application. They are normally found as part of a control statement such as if and while.

Table : Logical Operators

LogicNameExample
andAnd$num1 and $num2
&&And$num1 && $num2
orOr$num1 or $num2
||Or$num1 || $num2
xorXor$num1 xor $num2
!Not!$num1
Sample Code<?php
$num1 = 5;
$num2 = 10;
if ( $num1 > 0 && $num2 > 0 )
{
	echo “Both AND conditions are true”;
}
else
{
	echo “This condition grouping is false”;
}
if ( $num1 > 0 || $num2 > 0 )
{
	echo “At least one OR condition is true”;
}
else
{
	echo “Both conditions are false”;
}
?>
<?php // Type Some Code ?>