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

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 ?>

Increment And Decrement Operators

PHP increment operators are used to increment a variable’s value and decrement operators are used to decrement a variable’s value. PHP also supports pre- and post-increment and decrement operators allowing for the increment or decrement of the value by one.

Table : Increment And Decrement Operators

Increment / DecrementNameExample
++$vatPre-increment++$num1
$var++Post-increment$num1++
–$varPre-decrement–$num1
$var–Post-decrement$num1–
Sample Code<?php
$num1 = 5;
$num2 = 10;
$num1++; //Post-increment
echo num1;
$num2--; // Post-decrement
echo $num2;
++$num1; //Pre-increment
echo num1;
--$num2; // Pre-decrement
echo $num2;
?>
<?php // Type Some Code ?>

Comparison Operators

Comparison operators compare two values which can be numbers of strings.

Table : Comparison Operators

ComparisionNameExample
==Equal$num1 == $num2
===Identical$num1 === $num2
!=Not Equal$num1 != $num2
<>Not Equal$num1 <> $num2
!==Not Identical$num1 !== $num2
>Greater Than$num1 > $num2
<Less Than$num1 < $num2
>=Greater Than Or Equal To$num1 >= $num2
<=Less Than Or Equal To$num1 <= $num2
<=>Spaceship$num1 <=> $num2
Sample Code<?php
$name1 = "John";
$name2 = "James";
var_dump($name1 == $name2);
var_dump($name1 == 10); 
$num1 = 12;
$num2 = 22;
var_dump($ num1 > $num2);
var_dump($num1 == 10);
?>
<?php // Type Some Code ?>

Assignment Operators

Assignment operators are used to define and manipulate scalar variables and not arrays. Scalar variables have the same naming convention where the dollar symbol $ is prepend. Therefore arithmetic types are scalar types. Arrays are aggregate types.

Table : Assignment Operators

AssignmentEquivalentDescription
$num1 = $num2$num1 = $num2Set left operand to same value on right
$num1 += $num2$num1 = $num1 + $num2Addition
$num1 -= $num2$num1 = $num1 – $num2Subtraction
$num1 *= $num2$num1 = $num1 * $num2Multiplication
$num1 /= $num2$num1 = $num1 / $num2Division
$num1 %= $num2$num1 = $num1 % $num2Modulus
Sample Code<?php
$num = 1;
$counter = 1; 
$num += $counter; // Arithmetic Assignment Operator
echo $num;

$greeting = 'Hello ';
$name = 'John';
$greeting .= $name; // Concatenation Assignment Operator
echo $greeting;
?>
<?php // Type Some Code ?>

Arithmetic Operators

The arithmetic operators perform addition, subtraction, multiplication, division, exponentiation, and modulus operations.

Table : Arithmetic Operators

OperatorNameExample
+Addition Or Sum$num1 + $num2
Subtraction Or Difference$num1 – $num2
*Multiplication Or Product$num1 * $num2
/Division Or Quotient$num1 / $num2
%Modulus Or Division Remainder$num1 % $num2
**Exponentiation Or Raising Power$num1 ** $num2
Sample Code<?php
$num1 = 5;
$num2 = 3;
echo ($num1 % $num2)."\n"; // Prints 2
echo ($num1 % $num3)."\n"; // Prints 2
$num1 = -5; // Set To Negative Value
echo ($num1 % $num2)."\n"; // prints -2
$num2 = -3; // Set To Negative Value
echo ($num1 % $num2)."\n"; // prints -2
?>
<?php // Type Some Code ?>

String Formatting

PHP double quoted strings can format strings using defined variables.

Newline character \n causes a line break when using double quotes.

Concatenation character . and concatenation assignment is .= to combine two strings.

Sample Code<?php
$name = "Jake\nRoberts";
echo "Your name is $name";    // prints Your name is Jake and Roberts on next line
$name .= " Roberts"; // Appends to string via concatenation assignment
echo "My name is" . $name; // Concatenation of string
 ?>

Table : Common Functions For Manipulating Strings

FunctionDescriptionExample
strlen()Gets string lengthstrlen($name);
substr()Return part of stringsubstr($name, -1);
explode()Split a string by a stringexplode(“.”, $height);
implode()Join array elements with a stringimplode(“,”, [“name”,”email”);

<?php // Type Some Code ?>