Professional Documents
Culture Documents
Outline
• Why PHP?
• Info Sources
• A Walk Through
• PHP Variables
• Data Types
• Type
• Casting
• Constants
• Operators
• Control
• Structures
Why
PHP?
• Server-side scripting ( web development )
from basic to fully enterprise. ( the
majority of facebook code is in PHP ).
• Command-Line scripting.
• Client-side GUI applications (using PHP
GTK).
• Supported on all Operating systems.
• Can be embedded into HTML.
• Easy to learn.
Why
PHP?
• Around 21M websites are using PHP until
2007
Info
Sources
• http://www.php.net/ h
• ttp://www.mysql.com/
A Walk Through
PHP
• Opening/ closing tags :
– <?php ?> (preferred)
– <? ?>
– <% %>
– <script language="php"> </script>
• Statements end with semi-colon
‘;’ :
$x = 10;
A Walk Through
PHP
• White spaces and Line breaks do not
matter.
• Comments :
– # Single-line comments (or when a code block ends ‘?
>’)
– / / Single-line (or when a code block ends ‘?
>’)
comments
– /*
Multi-line
comments
*/ (can’t be nested)
Variables
• Start with a dollar sign ‘$’.
• Can contain upper/ lower case
letters, number and underscores.
• Can’t start with a number (after the
‘$’ sign).
• They are case sensitive.
Variables
• Examples:
– Legal:
• $name
• $_name
• $name_12
– Illegal:
• $not valid
• $12name
• $|
– These identifiers are not the same: ( $hi,
$Hi ).
Data
Types
PHP is weakly typed
language. 1.Integer:
– Example :
• $x = 12000
• $y = +50
• $z = -30
Data
Types
1. Floating
Point:
– Examples:
•
$x = 0.45
•
$y = +20.2
•
$z = -40.11
•
$k =
•
+0.345E2
$d = -0.234E-
3
Data
Types
1.
– Sequence of characters with an arbitrary
String:
– length. Examples:
• $name = ‘Mohamed’;
• $name = “Mohamed”;
• $str = <<<EOD
Example of
string
spanning multiple
lines
• using heredoc syntax.
• EOD;
•echo ‘here is it’;
print “line 1 \n line2”;
$name = ‘john’; echo “Hello
Data
Types
1.
Boolean:
– true or false values.
– Used in conditional
– statements. Example :
$value = true;
If( $value == true )
/ / do something
Data
Types
1.
Array:
– A collection that holds a number of
– values. Example:
$x = array(“Hi”, “Hello”, 3, 4.01 ).
Data
Types
1. Objects:
– A class is a definition of some variables
and functions that maps to a real entity.
– Example :
class Person{
private $var=‘’;
public function getVar(){
return $var;
}
}
$b =new Person();
Data
Types
1. Resource:
– It is an identifier for an external connection
or object (database or file handle).
Data
Types
1.
NULL:
– It is the no-value
– value. Example:
$x = 10;
$x = NULL; //value is
gone
Type
Casting
<?php
$foo = (float)'1'; / / 1.0
$foo = (int)10.5; / / 10
$foo = (boolean) 5; / /
true
?>
Type
Casting
<?php
$foo = "0"; / / $foo is string
$foo += 2; / / $foo is now an integer (2)
$foo = $foo + 1.3; / / $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; / / $foo is integer
(15)
$foo = 5 + "10 Small Pigs"; / / $foo is integer
(15)
?>
Constant
s
– Is an identifier (name) for a simple value. that
value cannot change during the execution of
the script.
–
Example:
• define("FOO", "something"); / / valid
• define("2FOO", "something"); / / not
valid
Here FOO is a name of a constant
Something is a value of FOO
echo FOO;
Operator
s–
An operator is a symbol that takes
arguments and returns a value.
– Arithmetic Operators :
Operator Description Example Result
+ Addition x=2 4
x+2
- Subtraction x=2 5- 3
x
* Multiplication x=4 20
x*5
/ Division 15/5 3
5/2 2.5
$x = "name";
$name = 10;
echo $$x;
/ / 10
Control
Structures
• ‘If’ statement :
<?php
if ($a > $b)
echo "a is bigger than b";
?>
• ‘else’ statement :
<?php
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than
b";
}
?>
Control
Structures
• ‘elseif’ / ‘else if’ :
<?php
if ($a > $b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than
b";
}
?>
Control
Structures
• ‘switch’ statement:
– The switch statement is similar to a series
of IF/else statements on the same
expression. switch ($i)
if ($i == 0) { { case 0:
echo "i equals 0"; echo "i equals 0";
} elseif ($i == 1) break;
{ echo "i equals = case 1:
1"; echo "i equals 1";
} else { break;
echo “i equals default:
another value”; echo “i equals
} another value”;
}
Control
Structures
• Loops:
– ‘for’
– ‘while’
– ‘do while’
– ‘foreach’ (will be discussed more in
Arrays)
Control
Structures
• The ‘for’ loop:
for( $i = 0; $i <= 5;
$i++ )
{
echo $i . " ";
}
Control
Structures
• The ‘while’
loop:
$i = 0;
While( $i < 5 )
{ echo $i;
$i++;
}
Control
Structures
• The ‘do while’
loop:
$i =
0; do
{
ech
o
$i;
$i+
Control
Structures
• ‘break’ keyword :
– break ends execution of the current for,
foreach, while, do-while or switch structure.
– Example:
While( $x ){
If( $y )
break
; echo
$x;
}
Control
Structures
• ‘continue’ keyword :
– continue is used within looping structures to skip the
rest of the current loop iteration and continue
execution at the condition evaluation and then the
beginning of the next iteration.
– Example:
While( $x < 5 ){
If( $x == 2 )
{
c
o
n
t
i
Assignme
nt
• Write a PHP snippet that will generate the
following output :
*
***
*****
*******
*********