0% found this document useful (0 votes)
46 views35 pages

Introductiontophp

Here is a PHP snippet using a for loop that will generate the output: <?php for($i=1; $i<=5; $i++) { for($j=1; $j<=$i; $j++) { echo "*"; } echo "<br>"; } ?>

Uploaded by

Advi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views35 pages

Introductiontophp

Here is a PHP snippet using a for loop that will generate the output: <?php for($i=1; $i<=5; $i++) { for($j=1; $j<=$i; $j++) { echo "*"; } echo "<br>"; } ?>

Uploaded by

Advi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to PHP

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

Modulus (division 5%2 1


% 10%8 2
remainder)
10%2 0

++ Increment x=5 x+ x=6


+
-- Decrement x=5 x-- x=4
Operator
s
• 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
Operator
s
• 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
Operator
s
• 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";
}
?>
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 :

*
***
*****
*******
*********

You might also like