KEMBAR78
Class 2 - Introduction to PHP | PPT
Introduction to PHP
Outline
• Why PHP?
• Info Sources
• A Walk Through PHP
• Variables
• Data Types
• Type Casting
• Constants
• Operators
• Control Structures
• Assignment
• What's Next?
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/
• http://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 comments (or when a code block ends ‘?>’)
– /*
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. 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”;
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;
}
}
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)
?>
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
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
Operators
• Assignment Operators:
Operator Example Is The Same As
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
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
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
Advanced trick
• Variable variables :
$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";
}
?>
Demo
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”;
}
=
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 {
echo $i;
$i++;
} while ($i < 5);
Demo
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 ){
$x++;
continue;
}
echo $x;
$x++;
}
Assignment
• Write a PHP snippet that will generate the following
output :
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
What's Next?
• Functions.
Questions?

Class 2 - Introduction to PHP

  • 1.
  • 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-sidescripting ( 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.
  • 4.
    Why PHP? • Around21M websites are using PHP until 2007
  • 5.
  • 6.
    A Walk ThroughPHP • Opening/ closing tags : – <?php ?> (preferred) – <? ?> – <% %> – <script language="php"> </script> • Statements end with semi-colon ‘;’ : $x = 10;
  • 7.
    A Walk ThroughPHP • 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 witha 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 isweakly typed language. 1.Integer: – Example : • $x = 12000 • $y = +50 • $z = -30
  • 11.
    Data Types 1. FloatingPoint: – Examples: • $x = 0.45 • $y = +20.2 • $z = -40.11 • $k = +0.345E2 • $d = -0.234E-3
  • 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
  • 18.
    Type Casting <?php $foo =(float)'1'; // 1.0 $foo = (int)10.5; // 10 $foo = (boolean) 5; // true ?>
  • 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 anidentifier (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 operatoris 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
  • 22.
    Operators • Assignment Operators: OperatorExample Is The Same As = x=y x=y += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y .= x.=y x=x.y %= x%=y x=x%y
  • 23.
    Operators • Comparison Operators: OperatorDescription 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
  • 25.
    Advanced trick • Variablevariables : $x = "name"; $name = 10; echo $$x; // 10
  • 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"; } ?>
  • 28.
  • 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”; } =
  • 30.
    Control Structures • Loops: –‘for’ – ‘while’ – ‘do while’ – ‘foreach’ (will be discussed more in Arrays)
  • 31.
    Control Structures • The‘for’ loop: for( $i = 0; $i <= 5; $i++ ) { echo $i . " "; }
  • 32.
    Control Structures • The‘while’ loop: $i = 0; While( $i < 5 ){ echo $i; $i++; }
  • 33.
    Control Structures • The‘do while’ loop: $i = 0; do { echo $i; $i++; } while ($i < 5);
  • 34.
  • 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 aPHP snippet that will generate the following output : * *** ***** ******* ********* *********** ************* *************** ***************** *******************
  • 38.
  • 39.