0% found this document useful (0 votes)
20 views44 pages

PHP Course 4

The document outlines the objectives and syllabus of a PHP course. The course aims to teach students how to develop dynamic websites, understand web programming concepts, design forms, create and connect to MySQL databases using PHP. The syllabus covers topics such as data types, control structures, functions, debugging, building web pages with PHP, working with forms and sessions, and using PHP to access MySQL databases.

Uploaded by

Naasireet Maniga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views44 pages

PHP Course 4

The document outlines the objectives and syllabus of a PHP course. The course aims to teach students how to develop dynamic websites, understand web programming concepts, design forms, create and connect to MySQL databases using PHP. The syllabus covers topics such as data types, control structures, functions, debugging, building web pages with PHP, working with forms and sessions, and using PHP to access MySQL databases.

Uploaded by

Naasireet Maniga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

1

PHP COURSE
Instructor: Abdullahi Abdikadir Abdisahal
2 Course Objectives

 To develop dynamic and attractive web pages.


 To understand web programming terminologies.
 To understand how to design forms.
 To understand how to create MYSQL database
 To understand how to use SQL statements such select, insert, update,
create, delete, etc.
 To understand how to connect PHP pages to MYSQL database.
3 Course Syllabus

1. Introduction of Web & PHP


2. Exploring Data Types
3. Control Structures: Logical Expressions
4. Control Structures: Loops
5. User-Defined Functions
6. Debugging
7. Building Web Pages with PHP
8. Working with Forms and Form Data
9. Working with Sessions
10. MySQL Basics
11. Using PHP to Access MySQL
4 1. Introduction of Web & PHP
 Web Technology refers to the various tools and techniques that are utilized
in the process of communication between different types of devices over
the internet.
 Web Technology can be classified into the following sections:
 World Wide Web (WWW)
 Web Browser
 Web Server
 Web Pages
 Web Development
 Frontend Development
 Backend Development

 Web Protocols (HTTP, FTP, SMTP)


5 Continue…
1. World Wide Web (WWW): The World Wide Web is based on several different
technologies : Web browsers, Hypertext Markup Language (HTML) and
Hypertext Transfer Protocol (HTTP).
2. Web Browser: The web browser is an application software to explore www
(World Wide Web). It provides an interface between the server and the
client and requests to the server for web documents and services.
3. Web Server: Web server is a program which processes the network requests
of the users and serves them with files that create web pages. This
exchange takes place using Hypertext Transfer Protocol (HTTP).
4. Web Pages: A webpage is a digital document that is linked to the World
Wide Web and viewable by anyone connected to the internet that has a
web browser.
6 Continue…

5. Web Development: Web development refers to the building, creating, and


maintaining of websites. It includes aspects such as web design, web
publishing, web programming, and database management. It is the
creation of an application that works over the internet i.e. websites.
 Web Development can be classified into two ways:
1. Frontend Development: The part of a website that the user interacts directly is
termed as front end. It is also referred to as the ‘client side’ of the application.
2. Backend Development: Backend is the server side of a website. It is the part of
the website that users cannot see and interact. It is the portion of software that
does not come in direct contact with the users. It is used to store and arrange
data.
7 Continue…

6. Web protocols are set of rules followed by everyone communicating over


the web.
 HTTP (Hyper Text Transfer Protocol)
 FTP (File Transfer Protocol)
 SMTP (Simple Mail Transfer Protocol)
8 Frontend Development
9 Backend Development
10 What is a PHP?

 PHP is a general-purpose scripting language geared toward web


development. It was originally created by Danish-Canadian programmer
Rasmus Lerdorf in 1993 and released in 1995.
 PHP was originally an abbreviation of Personal Home Page but it now
stands for: Hypertext Preprocessor
 It is a server-side scripting language that is used for web development.
 It can be easily embedded with HTML files. HTML codes can also be written
in a PHP file.
 The PHP codes are executed on the server-side whereas HTML codes are
directly executed on the browser.
 PHP its syntax like C, C++ and Java
11 PHP is a server-side language

 When you open a website on your web browser, for example,


https://www.example.com

 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

1. Simple and fast


2. Efficient
3. Secured
4. Flexible.
5. Open Source
6. Powerful Library Support
7. Database Connectivity
15 How to run PHP Code

You can run PHP code into two ways:


1. Using Online Compiler
2. Using Offline Compiler
16 PHP Syntax

 A PHP script starts with <?php and ends with ?>

 The default file extension for PHP files is ".php".


 PHP is case sensitivity
 To print your text when you using PHP use the echo keyword
17 2. Exploring Data Types & Variables
 Variables in a program are used to store some values or data that can be
used later in a program.
 The variables are also like containers that store character values, numeric
values, memory addresses, and strings. PHP has its own way of declaring
and storing variables.
 There are a few rules, that need to be followed and facts that need to be
kept in mind while dealing with variables in PHP:
1. Any variables declared in PHP must begin with a dollar sign ($), followed by the
variable name.
2. A variable name can only contain alphanumeric characters and underscores
(i.e., ‘a-z’, ‘A-Z’, ‘0-9, and ‘_’) in their name. Even it cannot start with a number.
3. We do not require to declare the data types of variables
4. PHP automatically detects the data type of the variable by analyzing the value
assigned
5. PHP variables are case-sensitive, i.e., $sum and $SUM are treated differently.
18 PHP Variables Scope
 In PHP, variables can be declared anywhere in the script. The scope of a
variable is the part of the script where the variable can be used.
 In PHP we have three different variable scopes:
1. Local
A variable declared within a function has a LOCAL SCOPE and can only be
accessed within that function:
2. Global
A variable declared outside a function has a GLOBAL SCOPE and can only be
accessed outside a function. The global keyword is used to access a global
variable from within a function:
3. Static
When a function is executed, all of its variables are deleted. But if you want
any variable not to be deleted, the static keyword is used when you first
declare the variable.
19 Data types:

 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

 An integer data type is a non-decimal number between -2,147,483,648 and


2,147,483,647. An integer must have at least one digit and can be either
positive or negative.
22 Float

 A float or floating point number is a number with a decimal point or a


number in exponential form.
23 Boolean

 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

 An object is a data type which stores data and information on how to


process that data. In PHP, an object must be explicitly declared. We need
to declare a class of object using the class keyword.
 We will cover the Objects in the upcoming chapters.
29 NULL

 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

 Operators are used to perform operations on variables and values.


 PHP divides the operators in the following groups:
1. Arithmetic operators
2. Assignment operators
3. Comparison operators
4. Increment/Decrement operators
5. Logical operators
6. String operators
32 Arithmetic & Assignment operators
 The PHP arithmetic operators are used with numeric values to perform
common arithmetical operations, such as addition, subtraction,
multiplication etc.
 The PHP assignment operators are used with numeric values to write a
value to a variable.

Operator Name Example (Arithmetic) Same as (Assigment)


+ Addition $a + $b $a += $b;
- Subtraction $a - $b $a -= $b;
* Multiplication $a * $b $a *= $b;
/ Division $a / $b $a /= $b;
% Modulus $a % $b $a %= $b;
33 Comparison (expressions) Operators
 The PHP comparison operators are used to compare two values (number or
string):

Operator Name Example


== Equal $a == $b
=== Identical $a === $b
!= Not Equal $a != $b
<> Not Equal $a <> $b
!== Not Identical $a !== $b
> Greater than $a > $b
< Less than $a < $b
>= Greater than or equal to $a >= $b
<= Less than or equal to $a <= $b
34 Increment / Decrement Operators

 The PHP increment operators are used to increment a variable's value.


 The PHP decrement operators are used to decrement a variable's value.

Operator Name
++$a Pre-increment
$a++ Post-increment
--$a Pre-decrement
$a-- Post-decrement
35 Logical Operators

 The PHP logical operators are used to combine conditional statements.

Operator Name Example


&& And $a && b
|| Or $a || $b
! Not !$a
36 String Operators

 PHP has two operators that are specially designed for strings.

Operator Name Example


. Concatenation $a . $b
.= Concatenation assignment $a .= $b
37 2. Control Structures (Logical Expressions)

 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 if statement is by far the most frequently used one.


 We check if something is true and do something.
 The basic syntax looks like so
 if (expression) { do something }.
Example
<?php
// check within the if block
if (1 < 2) {
echo "1 is less than 2\n";
}
?>
40 Else 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

 The switch statement can be a little confusing in the beginning.


 Don't worry though - it is rarely used in comparison to the others.
 The main keywords here are switch, case, break and optionally default.
 You can think of each case as an if statement and the optional default is
like an else, which is executed/triggered when none of the cases above
matched.
 The break keyword basically tells PHP to stop evaluating further.
43 Continue…
Example
<?php
$userType = 'user';
switch ($userType) {
case 'admin' :
echo 'Wow - you are an admin!';
break;
case 'moderator' :
echo 'You are a moderator - not bad!';
break;
default :
echo 'Welcome you are guest';
}
?>
44 Ternary Operator ?:
 This weird looking operator is also known as the Elvis operator or the
shorthand if-else statement.
Syntax of Ternary Operator
(Condition) ? (Statement1) : (Statement2);

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");
?>

You might also like