PHP Course 4
PHP Course 4
PHP COURSE
Instructor: Abdullahi Abdikadir Abdisahal
2 Course Objectives
The web browser sends an HTTP request to a web server where example.com
locates. The web server receives the request and responds with an HTML
document.
In this example, the web browser is a client while the web server is the server.
The client requests for a page, and the server serves the request.
PHP runs on the web server, processes the request, and returns the HTML
document.
12 PHP is a cross-platform language
PHP can run on all major operating systems, including Linux, Windows, and
macOS.
You can use PHP with all leading web servers such as Nginx, OpenBSD, and
Apache. Some cloud environments also support PHP like Microsoft Azure
and Amazon AWS.
13 How PHP Works
14 Characteristics of PHP
A variable can store different types of Data. Let’s have a look at some of
the data types supported by PHP.
1. String
2. Integer
3. Float
4. Boolean
5. Array
6. Object
7. NULL
20 String
A string is a sequence of characters. In PHP, you can write the string inside
single or double quotes.
21 Integer
A Boolean represents two possible states: TRUE or FALSE. They are often
used in conditional testing.
24 Array
An array is a data structure that stores one or more similar type of values in
a single value. For example if you want to store 100 numbers then instead of
defining 100 variables its easy to define an array of 100 length.
There are three different kind of arrays and each array value is accessed
using an ID which is called array index.
Numeric array − An array with a numeric index. Values are stored and
accessed in linear fashion.
Associative array − An array with strings as index. This stores element values
in association with key values rather than in a strict linear index order.
Multidimensional array − An array containing one or more arrays and
values are accessed using multiple indices
25 Numeric Array
These arrays can store numbers, strings and any object but their index will
be represented by numbers.
By default array index starts from zero.
Example
Following is the example showing how to create and access numeric arrays
and we use array() function to create array.
<?php $numbers = array( 1, 2, 3, 4, 5); ?>
When we went to access and print one item of the array we
use the index number of this item
<?php echo $numbers[0]; ?>
26 Associative Arrays
The associative arrays are very similar to numeric arrays in term of
functionality but they are different in terms of their index. Associative array
will have their index as string so that you can establish a strong association
between key and values.
Example
Like Numeric Array Associative Array use array() function to create array
<?php $car = (“company” => “Toyota”, “color” => “white”, “type”
=> “sedan”); ?>
When we went to print the Associative Array we use the key name to print
the value
<?php echo ($car[‘company’];); ?>
27 Multidimensional Arrays
A multi-dimensional array each element in the main array can also be an
array. And each element in the sub-array can be an array, and so on.
Values in the multi-dimensional array are accessed using multiple index.
Example
<?php
$devices = array(
0 => array(1, 2, 3, 4),
‘computers’ => array(
“name” => “HP”,
“type” => “Laptop”
)
);
?>
28 Object
Null is a special data type which can have only one value: NULL.
Example
<?php $x = NULL; ?>
30 Comments
Comments allow us to to add information or documentation right into the
code without making them visible to the outside world.
They can also be used to disable parts of the code, when we want to test
something.
// This is a single line comment.
# This is also a single line comment.
/*
This is a multiline comment.
And we are learning PHP Language
*/
31 Operators
Operator Name
++$a Pre-increment
$a++ Post-increment
--$a Pre-decrement
$a-- Post-decrement
35 Logical Operators
PHP has two operators that are specially designed for strings.
A control structure allows you to control the flow of code execution in your
application. Generally, a program is executed sequentially, line by line, and
a control structure allows you to alter that flow, usually depending on
certain conditions.
The following flowchart explains how a control structure works in PHP.
38 Continue…
39 If Statement
The else statement allows us to specify what should happen if the condition in the if
statement is not met (returns false).
Example
<?php
// check within the if block
if (1 < 2) {
echo "1 is less than 2\n";
}
else {
echo "1 is not less than 2\n";
}
?>
41 Elseif Statement
Sometimes two options are just not enough. For this purpose
we have the elseif statement. It allows us to check if maybe a second condition is met.
The expression of the elseif part is only evaluated when the if statement was not true.
By the way - you can have multiple elseif blocks.
Example
<?php
$userType = 'moderator';
if ($userType == 'admin') {
echo 'Wow - you are an admin!';
} elseif ($userType == 'moderator') {
echo 'You are a moderator - not bad!';
} elseif ($userType == 'user') {
echo 'You are a user - welcome!';
} else {
echo ‘Welcome you are guest';
}
?>
42 Switch-case Statement
Example
<?php
$day = “Monday”;
$rightDay = ($day == “Monday”) ? True : False;
echo ($rightDay);
// or you can use this way
$day = "Monday";
echo (($day == "Monday") ? "This is true" : "This is false");
?>