PHP
Introduction
1 PHP-Introduction 4/10/2016
What is PHP?
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the serve
PHP File
PHP files can contain text, HTML, CSS, JavaScript, and PHP code
PHP code are executed on the server, and the result is returned to the browser
as plain HTML
PHP files have extension ".php"
2 PHP-Introduction 4/10/2016
How PHP works
when the user navigates in his browser to a page with a .php extension, the
request is sent to a web server, which directs the request to the PHP interpreter.
The PHP page is processed and the results is sent back to client.
3 PHP- Introduction 4/10/2016
Life cycle of a PHP web request
The user requests a .php file.
Server runs any script code inside.
script produces output that becomes the response sent back to user’s browser.
4 PHP- Introduction 4/10/2016
Using PHP we can:
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
5 PHP- Introduction 4/10/2016
PHP Syntax
A PHP script can be placed anywhere in the document.
A PHP script starts with <?php and ends with ?>
6 PHP- Introduction 4/10/2016
The PHP code is executed on the server, generating HTML which is sent back to the
client.
The hello world PHP page renders to an HTML page as bellow:
7 PHP- Introduction 4/10/2016
Comments in PHP
A comment in PHP code is a line that is not read/executed as part of the
program. Its only purpose is to be read by someone who is looking at the code.
8 PHP- Introduction 4/10/2016
Declaring PHP Variables
In PHP, a variable starts with the $ sign, followed by the name of the variable.
9 PHP- Introduction 4/10/2016
Rules for PHP variables
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
10 PHP- Introduction 4/10/2016
Variables Scope
PHP has three different variable scopes:
local
global
Static
static variable is a local variable in a function that keeps its value after
the function executed.
11 PHP- Introduction 4/10/2016
12 PHP- Introduction 4/10/2016
13 PHP- Introduction 4/10/2016
PHP Data Types
PHP supports the following data types:
Integers − are whole numbers, without a decimal point, like 4195.
Doubles − are floating-point numbers, like 3.14159 or 49.1.
Booleans − have only two possible values either true or false.
NULL − is a special type that only has one value: NULL.
Strings − are sequences of characters, like 'PHP supports string operations.'
Arrays − are named and indexed collections of other values.
Objects − are instances of programmer-defined classes, which can package
up both other kinds of values and functions that are specific to the class.
Resources − are special variables that hold references to resources external
to PHP (such as database connections).
14 PHP- Introduction 4/10/2016
Object Example
<?php
class foo
{{
function do_foo()
{ {
echo "Doing foo.";
} }
}}
bar.do_foo(); $bar->do_foo(); $bar = new foo;
$bar->do_foo();
?
Resource Example
<?php
$c = mysql_connect();
$fp = fopen("foo", "w");
>?
15 PHP- Introduction 4/10/2016
PHP String Functions
Function Uses Example
strlen() returns the length of a string. echo strlen("Hello world!"); // outputs 12
str_word_count() counts the number of words in echo str_word_count("Hello world!"); // outputs 2
a string
strrev() reverses a string echo strrev("Hello world!"); // outputs !dlrow olleH
strpos() searches for a specific text echo strpos("Hello world!", "world"); // outputs 6
within a string
str_replace() replaces some characters with echo str_replace("world", "Dolly", "Hello world!"); //
some other characters in a outputs Hello Dolly!
string.
16 PHP- Introduction 4/10/2016
17 PHP- Introduction 4/10/2016
18 PHP- Introduction 4/10/2016
PHP Constants
A constant is an identifier for a value and its value cannot be changed during
the script.
The define() function is used to create a constant.
define(name, value, case-insensitive)
define(“PI", “3.14");
Constants are automatically global and can be used across the entire script.
19 PHP- Introduction 4/10/2016
20 PHP- Introduction 4/10/2016
PHP Operators
Operators are used to perform operations on variables and values.
PHP divides the operators in the following groups:
Arithmetic operators
Assignment operators
Comparison operators
Increment/Decrement operators
Logical operators
String operators
Array operators
21 PHP- Introduction 4/10/2016
Arithmetic operators
Assignment operators
22 PHP- Introduction 4/10/2016
23 PHP- Introduction 4/10/2016
Comparison operators
Logical operators
24 PHP- Introduction 4/10/2016
25 PHP- Introduction 4/10/2016
26 PHP- Introduction 4/10/2016
The if Statement
27 PHP- Introduction 4/10/2016
The if else Statement
28 PHP- Introduction 4/10/2016
The elseif Statement
29 PHP- Introduction 4/10/2016
The switch Statement
30 PHP- Introduction 4/10/2016
The ? Operator
31 PHP- Introduction 4/10/2016
while Loops
32 PHP- Introduction 4/10/2016
do...while Loops
33 PHP- Introduction 4/10/2016
for Loops
34 PHP- Introduction 4/10/2016
Breaking Out of a Loop
35 PHP- Introduction 4/10/2016
36 PHP- Introduction 4/10/2016
The continue Statement
37 PHP- Introduction 4/10/2016
38 PHP- Introduction 4/10/2016
foreach Loop
foreach ($array as $value) {
code to be executed;
}
39 PHP- Introduction 4/10/2016
40 PHP- Introduction 4/10/2016
Thanks!