You are on page 1of 41

Unit – 01

Expression and
Control
Statements in
PHP

2
PHP Definition:-
• The PHP Hypertext Preprocessor (PHP) is a programming language that allows
web developers to create dynamic content that interacts with databases. PHP is
basically used for developing web based software applications
• It is integrated with a number of popular databases, including MySQL,
PostgreSQL, Oracle, Sybase, Informix, and Microsoft SQL Server.
• PHP supports a large number of major protocols such as POP3, IMAP, and LDAP.
PHP4 added support for Java and distributed object architectures.
• PHP Syntax is C-Like. Server side interpreted, non-compiled and scripting
language
• Written with HTML as code is executed by the server, but the result is displayed
to the user as plain HTML
• It is used to manage dynamic content, databases, session tracking, even build
entire e-commerce sites.

3
Science Calculations

System

System

C uses curly
braces { } for
code blocks.
Scripting/
Interpreted
PHP Advantages:-
• Simplicity
• Efficiency
• Security
• Flexibility
• Familiarity
• Open Source
• Database
• Support
• Maintenance
• Scripting Langauge
5
Syntax of PHP :-
• Syntax • Example
A PHP script starts with <html>
<?php and ends with ?> <body>
<?php <h1>My first PHP page</h1>

echo “Welcome to Online class”; <?php


?> echo “Welcome to Online class”;
?>
The default file extension for PHP
files is ".php" </body>
</html>
6
Embedding PHP within HTML :-
• Inside a PHP file you can write • Example
HTML like you do in regular HTML <!DOCTYPE html>
pages as well as embed PHP code <html lang=“en”>
for server side execution. <head>
• When it comes to integrating PHP <meta charset=“UTF-8”>
code with HTML content, you need to <title>How to put PHP in HTML - Simple
enclose the PHP code with the PHP Example</title>
start tag <?php and the PHP end tag </head>
?>. <body>
• The important thing in the above <h1><?php echo " Welcome to Online
example is that the PHP code is class.“; ?></h1>
wrapped by the PHP tags. </body>
</html>
7
PHP Comments:-
• A comment in PHP code is a line that is • Example
not executed as a part of the program. <!DOCTYPE html>
Its only purpose is to be read by <html>
<body>
someone who is looking at the code.
• Purpose of comment is to make code <?php
more readable. It may help other // This is a single-line comment
developer to understand the code. # This is also a single-line comment
• PHP support single line as well as
multiline comments. /* This is multiline comment block that
span across more than.
• To start a single line comment either
*/
start with two slash (//) or hash (#). ?>
• Multiline comments start with /* and </body>
end with */ </html>

8
PHP echo and Print:-
• In PHP, there are 2 way to get output or • Example
print output: echo and print. <?php
• The echo statement is used with or echo "Hello world!<br/>"; // display string
without parentheses: echo or echo(). echo "This ", "string ", "was ", "made ", "with
• The echo statement can display multiple parameters.<br/>"; // strings as multiple
anything that can be displayed to the
browser: string, number, variable value $a= “Hello, PHP”;
and result of expression . $b= 5; $c=10;
• The PHP print statement is similar to the echo “$a<br/>”;
echo statement and use as a alternative. echo $b.”+”.$c.”=“; //Display variable
• echo has no return value while print has a echo $b+$c;
return value of 1 so it can be used in ?>
expressions </body> </html>

9
PHP Print:-
• In PHP, echo can take multiple • Example
parameters (although such usage is <?php
rare) while print can take one print "Hello world!<br/>";
argument.
$a= “Hello, PHP”;
• echo is marginally faster than print.
$b= 5;
Output :-
$c=10;
Hello world!
print “$a<br/>”;
Hello, PHP
print $b.”+”.$c.”=“; //Display variable
5+10=15
print $b+$c;
?>

10
PHP Variable Scope:-
• Local Variable:- • Example

• The variable declared within a <?php


function called Local variable. $num=10;
• So Local Variable scope is within function local_var()
function only. {
• Local Variable can not accessed $num=50;
outside that function.
echo “local num=$num<br/>”;
• And outside function such a } local_var();
variable treat as different variable.
echo “Variable num outside
local_var()=$num<br/>”;
?>
11
PHP Variable Scope:-
• Global Variable:- • Example

• The variable declared outside a <?php


function called Local variable. $num=10;
• So Global Variable scope is all function local_var()
function. {
• Global Variable can accessed global $num;
outside that function. echo “Access global variable within
• To get access within a function we function=$num<br/>”;
need to use “global” keyword } local_var();
before the variable . echo “Global Variable num outside of
local_var()=$num<br/>”;
?>
12
PHP $ and $$ Variable:-
• The $var is normal variable that • Example
store any value. <?php
• The $$var is reference variable that
store the value of $variable inside it. $a=“Hi”;
• $a represent variable but $$a $$a=“GPG”;
represent a variable with the echo $a.“<br/>”;
content of $a.
echo $$a.“<br/>”; // echo “$$a<br/>;”
$$a=$($a)
echo $Hi;
Output:-
Hi ?>
GPG
GPG
13
PHP Operators:-
Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

• Arithmetic operators
• Assignment operators
• Comparison operators
• Increment/Decrement operators
• Logical operators
• String operators
• Array operators
• Conditional assignment operators

14
PHP Arithmetic Operators:-

15
PHP Assignment Operators:-

16
PHP Comparison Operators:-

17
PHP Increment / Decrement Operators:-

18
PHP Logical Operators:-

19
PHP String Operators:-

20
PHP Array Operators:-

21
PHP Conditional Assignment Operators:-

22
PHP Data Types:-
• Variables can store data of different • PHP String
types, and different data types can do
different things. • A string is a sequence of characters, like
• PHP supports the following data types: "Hello world!".
1) String • A string can be any text inside quotes. You
2) Integer can use single or double quotes:
3) Float (floating point numbers - also <?php
called double) $x = "Hello world!";
4) Boolean $y = 'Hello GPG!';
5) Array
6) Object echo $x;
7) NULL echo "<br>";
8) Resource echo $y;
?>
23
PHP Integer:-
• An integer data type is a non-decimal • In the following example $x is an integer. The
number between -2,147,483,648 and PHP var_dump() function returns the data
2,147,483,647 type and value:
• Rules for integers: <html>
1) An integer must have at least one <body>
digit
<?php
2) An integer must not have a decimal
point $x = 5985;
3) An integer can be either positive or var_dump($x);
negative ?>
4) Integers can be specified in: decimal </body>
(base 10), hexadecimal (base 16),
octal (base 8), or binary (base 2) </html>
notation
24
PHP Float:-
• A float (floating point number) is a • In the following example $x is an integer. The
number with a decimal point or a PHP var_dump() function returns the data
number in exponential form. type and value:
<html>
<body>
• Output:-
<?php
float(10.68) $x = 10.68;
var_dump($x);
?>
</body>
</html>

25
PHP Boolean:-
• A Boolean represents two possible • Example:-
states: TRUE or FALSE. <?php
• Booleans are often used in $a = 11;
conditional testing. You will learn $b = 11.22;
more about conditional testing in a $c=“Hello”;
later chapter of this tutorial. $d= True;
• Output:- var_dump($a);
int(11) var_dump($b);
float(11.22) var_dump($c);
string(5) “Hello” var_dump($d);
bool(true) ?>

26
PHP Array:-
• An array stores multiple values in • Example:-
one single variable. • In the following example $cars is an array.
The PHP var_dump() function returns the
data type and value:
• Output:- <html>
array(3) { [0]=> string(5) "Volvo" <body>
[1]=> string(3) "BMW" [2]=> string(6) <?php
"Toyota" }
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
</body>
</html>
27
PHP Object:-
• Classes and objects are the two • Example:-
main aspects of object-oriented • <?php
class Car {
programming. public $color;
public $model;
• A class is a template for objects, public function __construct($color, $model) {
and an object is an instance of a $this->color = $color;
$this->model = $model;
class. }
public function message() {
• When the individual objects are return "My car is a " . $this->color . " " . $this->model . "!";
}
created, they inherit all the }
properties and behaviors from the
class, but each object will have $myCar = new Car("black", "Volvo");
echo $myCar -> message();
different values for the properties. echo "<br>";
$myCar = new Car("red", "Toyota");
echo $myCar -> message();
?>

28
PHP NULL Value:-
• Null is a special data type which can • Example:-
have only one value: NULL.
• A variable of data type NULL is a <html>
variable that has no value assigned <body>
to it. <?php
• If a variable is created without a $x = "Hello world!";
value, it is automatically assigned a $x = null;
value of NULL.
var_dump($x);
• If a variable is created without a ?>
value, it is automatically assigned a
</body>
value of NULL.
</html>

29
PHP Resource:-
• The special resource type is not an • Example:-
actual data type. It is the storing of <html>
a reference to functions and <body>
resources external to PHP.
<?php
• A common example of using the $f1 = fopen(“note.txt“,”r”);
resource data type is a database
var_dump($f1);
call.
echo”<br>”;
$link=mysql_connect(“localhost”,”root”,””);
var_dump($link);
?>
</body>
</html>
30
PHP Type Juggling:-
• Means dealing with variable type. • Example:-
• In PHP a variable type is determined <html>
by the context in which it is used. <body>
• If an Integer value is assign to a <?php
variable, it become a integer. $var1= 1;
• If an String value is assign to a $var2=“20”;
variable, it become a String. $var3=$var1+$var2;
• PHP does not required explicit type $var1=$var1+1.3
definition in a variable declaration. $var1=5*”10 small birds”;
• The conversion process whether ?>
explicit or implicit known as…….. </body>
</html>
31
PHP Type Casting:-
• Typecasting is a way to convert one • Example:-
data type variable into different <html>
data type. <body>
• A type can be cast by inserting one <?php
of the cast in front of variable. $count=“5”;
• Output:- echo gettype($count);
string $count is a string
integer settype($count,’int’);
echo gettype($count);
$count is a integer
?>
</body> </html>
32
PHP Decision Making Control Statements:-
• If Statement:- • Example:-
• The if statement is used to execute <html>
block of code only if the specified <body>
condition is true. <?php
• Syntax:- $a=10;
if(condition) if($a>0)
{
{
echo “The number $a is Positive”;
// code to be execute
}
}
?>
</body> </html>

33
PHP Decision Making Control Statements:-
• If-else Statement:- • Example:-
• The if...else statement executes some <?php
code if a condition is true and another $t = date("H");
code if that condition is false.
• Syntax:- if ($t < "10") {
echo "Have a good morning!";
if (condition) }
{
else
// code to be executed if condition is true;
{
} else
echo "Have a good day!";
{ }
// code to be executed if condition is false;
?>
}

34
PHP Decision Making Control Statements:-
• Nested-If Statement:- • Example:-
• Means an if block inside another if block. <?php
We use it when we have more than 2 $t = date("H");
condition and also called as if-else-if
statement if ($t < "10") {
• Syntax:- echo "Have a good morning!";
if (condition) }
{ // code to be executed1 if condition is true; elseif ($t < "20") {
} elseif (condition) echo "Have a good day!";
{ // code to be executed2 if condition is true; }
} else else {
{ // code to be executed1 if both condition1 and echo "Have a good night!";
condition2 are false }
} ?>

35
PHP Decision Making Control Statements:-
• Switch Statement:- • Example:-
• The switch statement is used to perform different actions
based on different conditions. <?php
• Use the switch statement to select one of many blocks of code to be $favcolor = "red";
executed.
• Syntax:-
switch ($favcolor) {
switch (n) {
case "red":
case label1:
echo "Your favorite color is red!";
code to be executed if n=label1;
break;
break;
case "blue":
case label2: echo "Your favorite color is blue!";
code to be executed if n=label2; break;
break; case "green":
case label3: echo "Your favorite color is green!";
code to be executed if n=label3; break;
break; default:
... echo "Your favorite color is neither red, blue, nor
default: green!";
code to be executed if n is different from all labels; }
} ?>

36
PHP Break and Continue Statements:-
• Break Statement:- • Example:-
• The keyword break ends execution <?php
of the current for, for each, while, for($i=1; $i<=5; $i++)
do while or switch structure. {
• When break executed inside loop echo”$i<br/>”;
the control automatically passes to if($i==3)
the first statement outside loop.
{
• Output:- break;
1 }
2 }
3 ?>

37
PHP Break and Continue Statements:-
• Continue Statement:- • Example:-
• It is used to stop processing the <?php
current block of code in the loop for($i=1; $i<=5; $i++)
and goes to the next iteration. {
• It is used to skip a part of the body if($i==3)
of loop under certain condition. {
• Output:- continue;
1 }
2 echo”$i<br/>”;
4 }
?>
5
38
PHP Loop Control Statements:-
• while Statement:- • Example:-

• It is execute block of code if and as <?php


long as a test condition is true. $i=0;
• While is an entry controlled loop while($i<=10)
statement.
{
• Syntax:-
echo”$i<br/>”;
while(if the condition is true)
$i+=2;
{
}
// code is executed
?>
}
39
PHP Loop Control Statements:-
• Do- while Statement:- • Example:-
• It is exit control loop which means <?php
that it first enter the loop, execute $i=1;
the statements and then check
condition. do
• Statements is executed at least once .
{
• Syntax:-
echo”$i<br/>”;
do
{ $i+=2;
// code is executed } while($i<10);
} while(if the condition is true); ?>

40
PHP Loop Control Statements:-
• For Statement:- • Example:-
• It is used when you know how many <?php
time you want to execute a $sum=0;
statement or a block of statement.
• Also known as entry controlled loops. for($i=0;$i<=10;$i+=2)
• Syntax:- {
for(initialization expression; echo”$i<br/>”;
test condition; update exp.)
{ $sum+=$i;
// code is executed }
} echo “Sum=$sum”;
?>
41
PHP Loop Control Statements:-
• foreach Statement:- • Example:-
• It is used for array and object. <?php
• For every counter of loop, an array $arr= array (10,20,30,40,50);
element and the next counter is
shifted to next element. foreach($arr as $i)
• Syntax:- {
foreach (array_element as echo”$i<br/>”;
value) }
{ ?>
// code is executed
}
42

You might also like