2. Outline
• Why PHP?
• Info Sources
• A Walk Through PHP
• Variables
• Data Types
• Type Casting
• Constants
• Operators
• Control Structures
• Assignment
• What's Next?
3. 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.
6. A Walk Through PHP
• Opening/ closing tags :
– <?php ?> (preferred)
– <? ?>
– <% %>
– <script language="php"> </script>
• Statements end with semi-colon ‘;’ :
$x = 10;
7. A Walk Through PHP
• White spaces and Line breaks do not matter.
• Comments :
– # Single-line comments (or when a code block ends ‘?>’)
– // Single-line comments (or when a code block ends ‘?>’)
– /*
Multi-line comments
*/ (can’t be nested)
8. 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.
9. Variables
• Examples:
– Legal:
• $name
• $_name
• $name_12
– Illegal:
• $not valid
• $12name
• $|
– These identifiers are not the same: ( $hi, $Hi ).
10. Data Types
PHP is weakly typed language.
1.Integer:
– Example :
• $x = 12000
• $y = +50
• $z = -30
12. Data Types
1. String:
– Sequence of characters with an arbitrary 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 $name”;
13. Data Types
1. Boolean:
– true or false values.
– Used in conditional statements.
– Example :
$value = true;
If( $value == true )
// do something
14. Data Types
1. Array:
– A collection that holds a number of values.
– Example:
$x = array(“Hi”, “Hello”, 3, 4.01 ).
15. 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;
}
}
16. Data Types
1. Resource:
– It is an identifier for an external connection or
object (database or file handle).
17. Data Types
1. NULL:
– It is the no-value value.
– Example:
$x = 10;
$x = NULL; //value is gone
19. 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)
?>
20. Constants
– 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
21. Operators
– An operator is a symbol that takes arguments
and returns a value.
– Arithmetic Operators :
Operator Description Example Result
+ Addition x=2
x+2
4
- Subtraction x=2
5-x
3
* Multiplication x=4
x*5
20
/ Division 15/5
5/2
3
2.5
%
Modulus (division
remainder)
5%2
10%8
10%2
1
2
0
++ Increment x=5
x++
x=6
-- Decrement x=5
x--
x=4
23. Operators
• Comparison Operators:
Operator Description Example
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
<> is not equal 5<>8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
=== Identical to 5 === “5” returns false
24. Operators
• Logical Operators :
Operator Description Example
&& And x=6
y=3
(x < 10 && y > 1) returns true
|| Or x=6
y=3
(x==5 || y==5) returns false
! not x=6
y=3
!(x==y) returns true
26. 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";
}
?>
27. 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";
}
?>
29. Control Structures
• ‘switch’ statement:
– The switch statement is similar to a series of
IF/else statements on the same expression.
if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} else {
echo “i equals another value”;
}
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
default:
echo “i equals another value”;
}
=
35. 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;
}
36. 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 ){
$x++;
continue;
}
echo $x;
$x++;
}
37. Assignment
• Write a PHP snippet that will generate the following
output :
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************