Professional Documents
Culture Documents
Open Source 34
Open Source 34
Open Source 34
Agenda
●
What is PHP ?
●
History
●
Installation
●
Data Types
●
Control Structure
●
Functions
●
Global Variables
●
GET & POST
●
Cookies & Sessions
●
MySql
●
Include Files 2
What is PHP?
3
What is PHP ?
4
History
●
PHP development began in 1994 when the developer
Rasmus Lerdorf wrote a series of Common Gateway
Interface (CGI) Binaries in the C programming language.
– CGI provides an interface between the Web server and
programs that generate the Web content.
●
PHP early non-released versions were used on his home
page to keep track of who was looking at his online
resume
5
History (Cont’d)
6
History (Cont’d)
7
Installation
8
LAMP Installation
●
The installation of what is known as a "LAMP" system:
Linux, Apache, MySQL and PHP
9
Data Types
10
Data Types
Variables are "containers" for storing information
<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z; // print 11
?>
11
Data Types (Cont’d)
●
A variable starts with the $ sign, followed by the name of the
variable
●
A variable name must start with a letter or the underscore
character
●
A variable name cannot start with a number
●
A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ )
●
Variable names are case sensitive ($y and $Y are two
different variables)
12
Data Types (Cont’d)
●
A variable is created the moment you first assign a value to it
●
Variables in PHP do not have intrinsic types - a variable does
not know in advance whether it will be used to store a
number or a string of characters.
●
PHP is a Loosely Typed Language
– we did not have to tell PHP which data type the variable is.
– PHP automatically converts the variable to the correct data
type, depending on its value.
13
Data Types (Cont’d)
• String
• Integer
• Floating point numbers
• Boolean
• Array
• Object
• NULL
14
Data Types – String
• A string can be any text inside quotes. You can use single
or double quotes
<?php
$x = "Hello world!";
echo $x;
echo "<br>";
?>
15
Data Types - Integer
16
Data Types – Integer (Cont’d)
17
Data Types – Integer (Cont’d)
<?php
$x = 5985;
var_dump($x); echo "<br>";
$x = -345; // negative number
var_dump($x); echo "<br>";
$x = 0x8C; // hexadecimal number
var_dump($x); echo "<br>";
$x = 047; // octal number
var_dump($x);
?> 18
Data Types – Floating Point
<?php
$x = 10.365; var_dump($x); echo "<br>";
$x = 2.4e3; var_dump($x); echo "<br>";
$x = 8E-5; var_dump($x);
?>
19
Data Types - Boolean
$x=true;
$y=false;
20
Data Types - Array
<?php
$cars=array("Volvo","BMW","Toyota");
// OR
$cars[0]="Volvo"; $cars[1]="BMW"; $cars[2]="Toyota";
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] .".";
$arrlength=count($cars);
?>
21
Data Types – Associative Array
<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
// OR
$age['Peter']="35"; $age['Ben']="37"; $age['Joe']="43";
echo "Peter is " . $age['Peter'] . " years old.";
?>
22
Data Types - 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.
●
First we must declare a class of object. For this, we use
the class keyword. A class is a structure that can contain
properties and methods.
23
Data Types – Object (Cont’d)
<?php
class Car
{
var $color;
function Car($color="green") { $this->color = $color; }
function what_color() { return $this->color; }
}
// instantiate one object
$object = new Car("white");
?>
24
Data Types - NULL
<?php
$x="Hello world!";
$x=null;
?>
25
Control Structure
26
Conditional Statements
●
Very often when you write code, you want to perform
different actions for different decisions. You can use
conditional statements in your code to do this.
●
In PHP we have the following conditional statements
– if statement
– if...else statement
– if...elseif....else statement
– switch statement
27
The If Statement
The if statement is used to execute some code only if a
specified condition is true.
28
The If...Else Statement
Use the if....else statement to execute some code if a
condition is true and another code if the condition is
false.
29
The ElseIf Statement
Use the if....elseif...else statement to select one of several
blocks of code to be executed.
30
The Switch Statement
Use the switch statement to select one of many blocks
of code to be executed.
31
The Switch Statement (Cont’d)
32
Loops
●
Loops in PHP are used to execute the same block of code
a specified number of times.
●
PHP supports following four loop types.
– for
– while
– do .. while
– foreach
33
While Loop
The while loop executes a block of code as long as the
specified condition is true.
34
Do.. While Loop
The do...while loop will always execute the block of code
once, it will then check the condition, and repeat the loop
while the specified condition is true.
35
For Loop
The for loop is used when you know in advance how
many times the script should run.
36
For Loop (Cont’d)
37
Foreach Loop
The foreach loop works only on arrays, and is used to
loop through each key/value pair in an array.
38
Functions 39
Functions
●
Besides the built-in PHP functions, we can create our own
functions.
●
A function is a block of statements that can be used
repeatedly in a program.
●
A function will not execute immediately when a page
loads.
●
A function will be executed by a call to the function.
40
Functions (Cont’d)
●
A user defined function declaration starts with the word
"function"
●
A function name can start with a letter or underscore (not
a number).
●
Function names are NOT case-sensitive.
●
Information can be passed to functions through
arguments. An argument is just like a variable.
●
To let a function return a value, use the return statement:
41
Functions (Cont’d)
42
Default Argument Value
44
Local and Global Scope
45
Local and Global Scope (Cont’d)
46
Local and Global Scope (Cont’d)
47
Local and Global Scope (Cont’d)
48
Post & Get
49
POST & GET
50
POST & GET (Cont’d)
51
POST
$_POST is an array of variables passed to the current
script via the HTTP POST method.
52
POST (Cont’d)
53
POST (Cont’d)
54
POST (Cont’d)
55
GET
$_GET is an array of variables passed to the current script
via the URL parameters.
56
GET (Cont’d)
http://www.w3schools.com/php/welcome_get.php?
name=user_name&email=user_email
57
Cookies & Sessions
58
PHP Cookies
• With PHP, you can both create and retrieve cookie values.
59
How to Create a Cookie?
60
How to Retrieve a Cookie Value?
61
How to Delete a Cookie?
62
PHP Sessions
●
The web server does know not when you start the
application and when you end because the HTTP address
doesn't maintain state.
●
A PHP session solves this problem by allowing you to
store user information on the server for later use (i.e.
username, shopping items, etc).
●
Session variables hold information about one single user,
and are available to all pages in one application.
63
PHP Sessions (Cont’d)
●
Session information is temporary and will be deleted
after the user has left the website.
●
Sessions work by creating a unique id (UID) for each
visitor and store variables based on this UID. The UID is
either stored in a cookie or is propagated in the URL.
64
Starting a PHP Session
●
Before you can store user information in your PHP
session, you must first start up the session.
●
The session_start() function must appear BEFORE the
<html> tag
65
Storing a Session Variable
66
Storing a Session Variable
67
Destroying a Session
68
MySql
69
MySql
70
MySql (Cont’d)
<?php
// Create connection
$con=mysqli_connect("localhost","guest","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " mysqli_connect_error();
}
mysqli_close($con);
?>
72
MySql (Cont’d)
<?php
$con=mysqli_connect("localhost","guest","abc123","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
while($row = mysqli_fetch_array($result)) {
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br>";
}
mysqli_close($con);
73
?>
Include Files 74
Include Files
• The include (or require) statement takes all the
text/code/markup that exists in the specified file and copies it
into the file that uses the include statement.
• Including files is very useful when you want to include the same
PHP, HTML, or text on multiple pages of a website.
76
Include Files (Cont’d)
77
Include
78
Include vs Require
79
Include vs Require (Cont’d)
The echo statement will not be executed because the script execution
dies after the require statement returned a fatal error:
80
Include Once
81
References
●
W3Schools
http://www.w3schools.com/
●
Lynda
http://www.lynda.com/MySQL-tutorials/PHP-
MySQL-Essential-Training/119003-2.html
82
Thank
You
83