Unit 02
Unit 02
Unit 02
SEMESTER : 5
UNIT : 2
RASMUS LERDORF
Creator of PHP
How PHP works?
HTML code
+
xyz.php PHP code
Static HTML
Web Page Request code
PHP
HTML Response SCRIPTING
Engine
Client’s Browser
LAMP is an open source web development platform that uses linux as the
operating system, apache as the web server, mysql as the relational
database management system and PHP as the object-oriented scripting
language.
XAMPP
XAMPP :
Installing XAMPP for Windows you need to download installable file from
https://www.apachefriends.org/download.html
After downloading .exe file you need to execute the file.
Sublime Editor :
Installing sublime you need to download installable file from
https://www.sublimetext.com/3
After downloading .exe file you need to execute the file
PHP File structure
Helloworld.php
Output in Web Browser
<html>
Hello World!
<head>
<title>Hello World</title>
</head> Note:
Contents of a .php file between <?php and
<body>
Note :Standard tags are recommended for use other tags required
to enable respective tags in php.ini configuration file
Commenting codes in PHP
A variable must start with a dollar ($) sign, followed by the variable
name.
It can only contain alpha-numeric character and underscore (A-z, 0-9,
_).
A variable name must start with a letter or underscore (_) character.
A PHP variable name cannot contain spaces.
One thing to be kept in mind that the variable name cannot start with
a number or special symbols.
PHP variables are case-sensitive, so $name and $NAME both are
treated as different variable.
Variables examples
$word
$Word
$greenThing
$green_thing
$word4
We can not use reserved keywords as variables like:
$this $if
$function $while
$include $break
Datatypes in PHP
Example :
<?php
$a = 3;
echo gettype($a) . "<br>";
?>
PHP settype() function:
Settype function converts value from one datatype to another datatype
Syntax :
boolean settype($variablename,$type);
Example:
<?php
$a = "32"; // string
settype($a, "integer"); // $a is now integer
?>
var_dump function:
xor Xor $a xor $b Return TRUE if either $ or $b is true but not both
& And $a & $b Bits that are 1 in both $a and $b are set to 1, otherwise 0.
~ Not ~$a Bits that are 1 set to 0 and bits that are 0 are set to 1
<< Shift left $a << $b Left shift the bits of operand $a $b steps
>> Shift right $a >> $b Right shift the bits of $a operand by $b number of places
Assignment Operators
Operat Name Example Explanation
or
= Assign $a = $b The value of right operand is assigned to the left operand.
+= Add then $a += $b Addition same as $a = $a + $b
Assign
-= Subtract then $a -= $b Subtraction same as $a = $a - $b
Assign
*= Multiply then $a *= $b Multiplication same as $a = $a * $b
Assign
/= Divide then $a /= $b Find quotient same as $a = $a / $b
Assign
(quotient)
%= Divide then $a %= $b Find remainder same as $a = $a % $b
Assign
(remainder)
String Operators
PHP constants are name or identifier that can't be changed during the
execution of the script except for magic constants, which are not really
constants. PHP constants can be defined by 2 ways:
1. Using define() function
2. Using const keyword
PHP constants should be defined in uppercase letters
constants are automatically global throughout the script.
Constants are similar to the variable except once they defined, they can never
be undefined or changed. They remain constant across the entire program.
PHP constants follow the same PHP variable rules.
PHP constant: define()
6 __TRAIT__ This magic constant returns the trait name, where it is used.
7 __METHOD__ It returns the name of the class method where this magic constant is included. The method name is
returned the same as it was declared.
8 __NAMESPACE__ It returns the current namespace where it is used.
9 ClassName::class It returns the fully qualified name of the ClassName.
Control Statements
if
if-else
if-else-if
nested if
If statement
Example : <?php
$marks=69;
if ($marks<33){ echo "fail"; }
else if ($marks>=34 && $marks<50) { echo "D grade"; }
else if ($marks>=50 && $marks<65) { echo "C grade"; }
else if ($marks>=65 && $marks<80) { echo "B grade"; }
else if ($marks>=80 && $marks<90) { echo "A grade"; }
else if ($marks>=90 && $marks<100) { echo "A+ grade"; }
else { echo "Invalid input"; }
?>
Nested-if Statement
The nested if statement contains the if block inside another if block. The
inner if statement executes only when specified condition in outer if
statement is true.
Syntax :
if (condition) { //code to be executed if condition is true
if (condition) {//code to be executed if condition is true }
}
Nested-if Statement
Example : <?php
$age = 23;
$nationality = "Indian";
//applying conditions for voting in India on nationality and age
if ($nationality == "Indian")
{ if ($age >= 18)
{ echo "Eligible to give vote"; }
else { echo "Not eligible to give vote"; }
}
else { echo “To Vote in India your nationality Should be Indian”;}
?>
Break Statement
PHP break statement breaks the execution of the current for, while, do-
while, switch, and for-each loop. If you use break inside inner loop, it breaks
the execution of inner loop only.
The break keyword immediately ends the execution of the loop or switch
structure. It breaks the current flow of the program at the specified
condition and program control resumes at the next statements outside the
loop.
The break statement can be used in all types of loops such as while, do-
while, for, foreach loop, and also with switch case.
Syntax : jump statement;
break;
Break Statement
Example:<?php
for($i=1;$i<=10;$i++)
{
echo "$i <br/>";
if($i==5)
{
break;
}
}
?>
Continue Statement
The PHP continue statement is used to continue the loop. It continues the
current flow of the program and skips the remaining code at the specified
condition.
The continue statement is used within looping and switch control structure
when you immediately jump to the next iteration.
The continue statement can be used with all types of loops such as - for,
while, do-while, and foreach loop. The continue statement allows the user to
skip the execution of the code for the specified condition.
Syntax: jump-statement;
continue;
Continue Statement
Example : <?php
for ($x = 0; $x < 10; $x++)
{
if ($x == 4)
{
continue;
}
echo "The number is: $x <br>";
}
?>
Switch statement
The default is an optional statement. Even it is not important, that default must always be
the last statement.
There can be only one default in a switch statement. More than one default may lead to
a Fatal error.
Each case can have a break statement, which is used to terminate the sequence of
statement.
The break statement is optional to use in switch. If break is not used, all the statements
will execute after finding matched case value.
PHP allows you to use number, character, string, as well as functions in switch expression.
Nesting of switch statements is allowed, but it makes the program more complex and less
readable.
You can use semicolon (;) instead of colon (:). It will not generate any error.
Switch statement
Example:
<?php
$num=20;
switch($num){
case 10: echo("number is equals to 10"); break;
case 20: echo("number is equal to 20"); break;
case 30: echo("number is equal to 30"); break;
default: echo("number is not equal to 10, 20 or 30");
}
?>
For Loop
PHP for loop can be used to traverse set of code for the specified number of
times.
It should be used if the number of iterations is known otherwise use while
loop. This means for loop is used when you already know how many times
you want to execute a block of code.
Syntax :
for(initialization; condition; increment/decrement)
{
//code to be executed
}
For Loop
Example :
<?php
for($n=1;$n<=10;$n++)
{
echo "$n<br/>";
}
?>
While Loop
PHP while loop can be used to traverse set of code like for loop.
The while loop executes a block of code repeatedly until the condition is
FALSE. Once the condition gets FALSE, it exits from the body of loop.
It should be used if the number of iterations is not known.
The while loop is also called an Entry control loop because the condition is
checked before entering the loop body. This means that first the condition is
checked. If the condition is true, the block of code will be executed.
Syntax : while(condition) while(condition):
{ //code to be executed //code to be executed
} endwhile;
While Loop
Example:
<?php
$n=1;
while($n<=10)
{
echo "$n<br/>";
$n++;
}
?>
Do While Loop
PHP do-while loop can be used to traverse set of code like php while loop. The PHP do-
while loop is guaranteed to run at least once.
The PHP do-while loop is used to execute a set of code of the program several times. If
you have to execute the loop at least once and the number of iterations is not even fixed,
it is recommended to use the do-while loop.
It executes the code at least one time always because the condition is checked after
executing the code.It is known as Exit Control Loop
The do-while loop is very much similar to the while loop except the condition check. The
main difference between both loops is that while loop checks the condition at the
beginning, whereas do-while loop checks the condition at the end of the loop.
Syntax: do{ //code to be executed
}while(condition);
Do While Loop
Example:
<?php
$n=1;
do{
echo "$n<br/>";
$n++;
}while($n<=10);
?>
THANK YOU