Date post: | 16-Jan-2017 |
Category: |
Technology |
Upload: | ahmed-swilam |
View: | 300 times |
Download: | 1 times |
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.
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
1. Floating Point:
– Examples:• $x = 0.45• $y = +20.2• $z = -40.11• $k = +0.345E2• $d = -0.234E-3
Data Types1. String:– Sequence of characters with an arbitrary length.– Examples:• $name = ‘Mohamed’;• $name = “Mohamed”;• $str = <<<EOD
Example of stringspanning multiple linesusing 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).
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=2x+2 4
- Subtraction x=25-x 3
* Multiplication x=4x*5 20
/ Division 15/55/2
32.5
% Modulus (division remainder)
5%210%810%2
120
++ Increment x=5x++ x=6
-- Decrement x=5x-- 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=6y=3(x < 10 && y > 1) returns true
|| Or x=6y=3(x==5 || y==5) returns false
! not x=6y=3!(x==y) returns true
Control Structures• ‘If’ statement :
<?phpif ($a > $b) echo "a is bigger than b";
?>• ‘else’ statement :
<?phpif ($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.
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
• ‘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 :
* *** ***** ******* ********* *********** ************* *************** ************************************