2. Agenda
Part I: Introduction
Part II: PHP Syntax
Part III: PHP Form Handling
3. Agenda
Part I: Introduction
What is PHP?
Why we use PHP?
PHP Can
Set Up PHP on Your Own PC
4. What is PHP?
PHP is a server scripting language, and
is a powerful tool for making dynamic
and interactive Web pages quickly.
PHP is a widely-used, open source
scripting language
PHP scripts are executed on the server
PHP costs nothing, it is free to
download and use
5. Why we use PHP?
PHP runs on various platforms (Windows, Linux,
Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used
today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is easy to learn and runs efficiently on the
server side
It is powerful enough to be at the core of the
biggest blogging system on the web
(WordPress)!
It is deep enough to run the largest social
network (Facebook)!
6. PHP Can
Generate dynamic page content
Create, open, read, write, delete, and
close files on the server
Send and receive cookies
Add, delete, modify data in your
database
Encrypt data
Output images, PDF files, and even
Flash movies
7. Set Up PHP on Your Own PC
Install a web server (Apache)
Install PHP
Install a database, such as MySQL
Example: XAMPP (Apache + MySQL +
PHP + Perl)
XAMPP is the most popular PHP
development environment
9. PHP Syntax
PHP script can be placed anywhere in
the document.
A PHP script starts with <?php and
ends with ?>
The default file extension for PHP files
is ".php".
11. Comments in PHP
A comment in PHP code is a line that is
not read/executed as part of the
program.
PHP supports three ways of
commenting:
12. PHP Case Sensitivity
In PHP, all user-defined functions,
classes, and keywords (e.g. if, else,
while, echo, etc.) are NOT case-
sensitive.
Example:
13. PHP Case Sensitivity
However; in PHP, all variables are case-
sensitive.
$color, $COLOR, and $coLOR are
treated as three different variables
Example:
14. PHP Variables
Variables are "containers" for storing
information.
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)
16. PHP echo and print Statements
In PHP there are two basic ways to get output:
echo and print.
There are some differences between echo and
print:
echo - can output one or more strings
print - can only output one string, and returns always
1
Example:
30. PHP Conditional Statements
In PHP we have the following conditional
statements:
if statement - executes some code only if a specified
condition is true
if...else statement - executes some code if a
condition is true and another code if the condition is
false
if...elseif....else statement - selects one of several
blocks of code to be executed
switch statement - selects one of many blocks of
code to be executed
33. PHP Loops
In PHP, we have the following looping
statements:
while - loops through a block of code as long as the
specified condition is true
do...while - loops through a block of code once, and
then repeats the loop as long as the specified
condition is true
for - loops through a block of code a specified
number of times
foreach - loops through a block of code for each
element in an array
38. PHP 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. PHP Arrays
An array is a special variable, which can hold
more than one value at a time.
Example:
41. PHP Associative Arrays
Associative arrays are arrays that use named
keys that you assign to them.
Example:
42. PHP Sorting Arrays
PHP array sort functions:
sort() - sort arrays in ascending order
rsort() - sort arrays in descending order
asort() - sort associative arrays in ascending order,
according to the value
ksort() - sort associative arrays in ascending order,
according to the key
arsort() - sort associative arrays in descending order,
according to the value
krsort() - sort associative arrays in descending order,
according to the key
43. Agenda
Part III: PHP Form Handling
PHP GET vs. POST
PHP $_GET
PHP $_POST
44. PHP Form Handling
One of the most powerful features of PHP is
the way it handles HTML forms. The basic
concept that is important to understand is
that any form element will automatically be
available to your PHP scripts.
The PHP $_GET and $_POST are used to
collect form-data.
45. PHP GET vs. POST
Both GET and POST create an array (e.g. array( key
=> value, key2 => value2, key3 => value3, ...)). This
array holds key/value pairs, where keys are the
names of the form controls and values are the input
data from the user.
When to use GET?
Information sent from a form with the GET method
is visible to everyone
facebook.com?welcome.php?name=Mohamed
When to use POST?
Information sent from a form with the POST
method is invisible to others
facebook.com?welcome.php
47. PHP $_GET
When the user fills out the form above and
clicks the submit button, the form data is sent
for processing to a PHP file named
"welcome.php".
welcome.php
50. PHP $_POST
When the user fills out the form above and
clicks the submit button, the form data is sent
for processing to a PHP file named
"welcome.php".
welcome.php