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.
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.
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
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
Type
Description
Syntax
for
loops through code block a specific number of times
for (expr1; expr2; expr3) statement
foreach
loops through code block for each element in an array or object
foreach (iterable_expr as $val) statement
while
loops through code block until specific condition is false
while (expr) statement
do-while
loops through code block one, repeats until specific condition is false
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
Function
Description
Example
unset()
Remove an item
unset($even_numbers[3]);
print_r()
Print all keys and elements
print_r($even_numbers);
count()
Counts all elements
count(($even_numbers);
reset()
Returns first array element
reset($even)numbers);
array_push()
Pushes element(s) to end
array_push($even_numbers, 14);
array_pop()
Pop the element off the end
array_pop($even_numbers);
array_unshift()
Prepend element(s) to beginning
array_unshift($even_numbers, 0);
array_merge()
Merge one or more arrays
array_merge($even_numbers,[16,14]);
sort()
Sort in ascending order
array_sort($even_numbers);
array_slice()
Extract a slice
array_slice($even_numbers, 2);
array_keys()
Returns all keys
array_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.
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
Logic
Name
Example
and
And
$num1 and $num2
&&
And
$num1 && $num2
or
Or
$num1 or $num2
||
Or
$num1 || $num2
xor
Xor
$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”;
}
?>