Expression and control statements in PHP
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
• Variables:
-In php,we can define variable using prefix doller sing($).
Syantax:
$VariableName
Example:
$no=100;
$roll_no=1212;
$name=”VJTech”;
echo $roll_no;
• Rules for PHP Variable:
1)Variable name should begin with $ sign and followed by variable name.
2)Variable name should begin with alphabets or underscore(_).
3)It may Combine with number but should not start with number.
4)It does not allowed any special symbol except underscore( _ ).
5)Variable name are case sensitive(“VJTech,vjtech,VJtech consider as three different
variable name.
6)Does not allowed any white space between variable name.
Example:
_vjtech //invalid
$_vjtech //valid
$vjtech //valid
$vj_tech //valid
$stud name //invalid
$rollno123 //valid
$1no //invalid
$emp#id //invalid
$1_abc //invalid
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
• Variable References:
• <?php
• $a = 100;
• $b = & $a; // b is Reference variable of a
• echo "Value of a=$a";
• echo "<br>value of b=$b";
• $b = 500;
• echo "<br>After Updating value of varibale b";
• echo "<br>Value of a=$a";
• echo "<br>value of b=$b";
• ?>
• Variable Scope:
1)Local Scope
2)Global Scope
User defined function declaration syantax:
function functionName(parameter list)
{
//body
}
Example:
<?php
$x = 100; //Global Variable $x
function vjtech() //Function Defination
{
global $x;
$y = 200;//Local Variable $y
echo "<br>Local Variable y Value=$y";
echo "<br>Inside the function global Variable x
Value=$x";
$x = 700;
}
vjtech(); //Calling Function
echo "<br>Outside the function global Variable x Value=$x";
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
• Data Type:
- PHP supports eight primitive data type.
- 1) Integer:
- It is a collection of 0 to 9 digits combination value without decimal points.
- Its range similar to long int data type in C.
- Its range is -2147483648 to -2147483647.
- You can represent Integer number in decimal,octal and hexadecimal format.
- If it is decimal,it is just the number.For example:145.
- If it is octal,then number should preceeded with zero(0). For example:0123.
- If it is hexadecimal,then number should preceeded with OX. For example:OX23.
- In PHP,we can test whether value is int or not by using is_int() or is_integer()
- Example:
- <?php
- //Integer data type
- $num = "0x34";
- if(is_int($num))
- {
- echo "Number is integer";
- }
- else
- {
- echo "Number is not integer";
- }
- ?>
- Diagram:
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
2)Floating point number:
-Floating point number is called as real number.
- It is a collection of 0 to 9 digits combination value with decimal points.
-In PHP,floating point number will display value upto 15 digits decimal places.
-Its range is 1.7E-308 to 1.7E+308
-You can represent floating point number using two different format.
I)Fractional Format:23.45,67.8,143.78
II)Exponentional Format:1.3E-56,2.3E89.
-In PHP,we can test whether value is float or not by using is_float() or is_real().
-Example:
<?php
//Float data type
$num = 3.14;
if(is_float($num))
{
echo "Number is float";
}
else
{
echo "Number is not float";
}
?>
3)String:
-Collection of characters is known as String.
-We can represent String using single or double quotes.
-For Example: “VJTech” or ‘VJTech’
-PHP Provided method is_string() to check whether given value is String or not.
-Example:
<?php
$name = "VJTech";
//echo "My Class name is $name";
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
if(is_string($name))
echo "$name is String";
else
echo "$name is not String";
?>
4)Boolean:
-Boolean value can be either TRUE or FALSE value.
-Both value are case-insensitive.
-is_bool()method used to check whether value is boolean or not.
-Example:
<?php
$m = true;
if(is_bool($m))
echo "$m is Boolean";
else
echo "$m is not Boolean";
?>
-In PHP,the following values are false.
-The Keyword false,0,0.0,empty string (“”),Null.
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
5)Array:
-Array is used to store multiple values in single variable.
-In PHP collection of different types of values is known as Array.
-Array index should begin with 0 and end with SIZE-1.
-We can retrive the individual array element using subscript and index number.
-Example:
<?php
$p = array('a', 'b', 'c', 'd');
$q = array('first' => '10', 'second' => '20', 'third' => '30');
echo "<br>Value of p array 0 index $p[0]";
echo "<br>Value of p array 1 index $p[1]";
echo "<br>Value of p array 2 index $p[2]";
echo "<br>Value of q array=". $q["first"];
echo "<br>Value of q array=". $q["second"];
echo "<br>Value of q array=". $q["third"];
?>
6)objects:
-PHP supports oop.
-object are the special instance of the class
-class is the collection of properties and methods.
-we can define the class using keyword ‘class’.
-once class is created then we can create no of objects.
-we can access members of class using -> symbol.
-Example:
<?php
class student
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
public $name = '';
function get_stud_info($nm)
$this->name = $nm;
function disp_stud_info()
echo "Hello $this->name";
$s1 = new student();
$s1->get_stud_info("Dennis");
$s1->disp_stud_info();
?>
7)Resource:
-Resource is a special PHP data type.
-It is used to refer to external resources like file,images,etc.
-It is used to interact with outside the world.
-For example:Database Connection function return resource which is identify that
connection.That resource we use while calling query.
-One more benefits of resource is that they are garbage collected when no longer in use.
-is_resource() function to test whether value is resource or not.
-Example:
<?php
$result = database_connect();
database_query($result);
if(is_resource($result))
echo "This is resource data type";
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
else
echo "This is not resource data type";
?>
8)Null:
-This data type is having only one value denoted by the keyword Null.
-The Null value represent a variable that has no value.
-A Variable consider to be null if:
I)It has been assigned constant null.
II)It has not been set to any value.
III)It has been unset();
-is_null() function used to test whether value is null or not.
-Example:
<?php
$result = null;
if(is_null($result))
echo "This is null data type value";
else
echo "This is not null data type value";
?>
Operators:
-Operators are the symbol which indicate operation to be perform.
-There are three maia categories of the operator.
i)Unary operators:require single operands.
ii)Binary operators:requires two operands.
iii)Ternary operators:requires three operands.
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
1)Arithmetic operators:
i)Negation (-) : -$a;
ii)Addition (+) :$a+$b;
iii)Substraction (-) :$a-$b;
iv)Multiplication(*) :$a*$b;
v)Division(/) :$a/$b;
vi)Modulus(%) :$a%$b;
vii)Exponentiation(**):$a**$b;
Example:
<?php
//Arithmetic operators
$no1=100;
$no2=50;
$result=$no1+$no2;
echo "Addition is $result";
$result=$no1-$no2;
echo "<br>Substraction is $result";
$result=$no1*$no2;
echo "<br>Multiplication is $result";
$result=$no1/$no2;
echo "<br>Division is $result";
$result=$no1%$no2;
echo "<br>Modulus is $result";
$result=$no1**$no2;
echo "<br>Exponentiation is $result";
$result=-$no1;
echo "<br>Negation is $result";
?>
2)AutoIncrement(++) and AutoDecrement(--) operator:
-Increment operator increase value of variable by one.
-Decrement operator decrease value of variable by one.
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
Example:
$a=100; a=100; $b=100; $ b=100;
$a++; ++$a; $b - -; --$b;
$a=101; $a=101; $b=99; $b=99;
<?php
$a=100;
echo "Value of a before incrementation=$a";
$a++;
echo "<br>Value of a after incrementation=$a";
$a=100;
echo "<br>Value of a before decrementation=$a";
--$a;
echo "<br>Value of a after decrementation=$a";
?>
3)String Concatenation operators:
-We use dot(.) operator for concatenation.
-Example:
<?php
$str1="Welcome";
$str2=$str1.' '."Friend";
echo "Concatenated String is $str2";
?>
4)Assignment operator:
-The symbol of assignment operator is (=).
-It is used to assign right side expression result or value to the left side variable.
-We use assignment operator in different ways(=,+=,-=,*=,/=,etc).
-Example:
<?php
$a=100;
$b=200;
$a+=$b;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
echo "Addition =$a";
?>
5)Comparison operator:
-PHP provides comparison operators to compare two operands.
-It returns boolean value,either true or false.
i)Equal(==) :$a==$b;
ii)Identical(===) :$a===$b;
iii)Not Equal(!=,<>) :$a!=$b;
iv)Not Identical(!==) :$a!=$b;
v)Less than(<) :$a<$b;
vi)Greater than(>) :$a>$b;
vii)Less than or equal to(<==):$a<=$b;
viii)Greater than or equal to(>=):$a>=$b;
Example:
<?php
$a=100;
$b=200;
$c=$a<$b;
echo "$a<$b=".$c;
$c=$a>$b;
echo "<br>$a>$b=".$c;
$c=$a<=$b;
echo "<br>$a<=$b=".$c;
$c=$a>=$b;
echo "<br>$a>=$b=".$c;
$c=$a==$b;
echo "<br>$a==$b=".$c;
$c=$a!=$b;
echo "<br>$a!=$b=".$c;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
$c=$a!==$b;
echo "<br>$a!==$b=".$c;
$c=$a===$b;
echo "<br>$a===$b=".$c;
?>
6)Logical Operator:
i)Logical AND(&&,and) :True if both sides are true otherwise false.
ii)Logical OR(||,or) :False if both sides are false otherwise true.
iii)Logical XOR(xor) :If both sides are identical then it is false otherwise TRUE
iv)Logical NOT(!) :Reverse the result.
Example:
<?php
$a=100;
$b=200;
$c=($a<$b and $a<=$b);
echo "($a<$b and $a<=$b):".$c;
$c=($a<$b or $a>=$b);
echo "<br>($a<$b or $a<=$b):".$c;
$c=($a<$b xor $a<=$b);
echo "<br>($a<$b xor $a<=$b):".$c;
/*If Both side are true then xor is false and if both
side are false then xor is true*/
$c=!($a<$b and $a<=$b);
echo "<br>!($a<$b and $a<=$b):".$c;
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
7)Bitwise operator:
i)Bitwise AND(and):If both bits are 1 then output would be 1 otherwise 0
ii)Bitwise OR(|) :If both bits are 0 then output would be 0 otherwise 1
iii)Bitwise XOR(^) :if both bits are same then output would be 0 otherwise 1.
iv)Bitwise NOT(~) :Change 1 to 0 or 0 to 1
v)Bitwise Left shift(<<) :
vi)Bitwise Right shift(>>):
Diagram:
Example:
<?php
$a=12;
$b=13;
$c=$a and $b;
echo "<br>Bitwise AND=$c";
$c=$a | $b;
echo "<br>Bitwise OR=$c";
$c=$a ^ $b;
echo "<br>Bitwise XOR=$c";
$c=~12;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
echo "<br>Bitwise AND=$c";
$c=14<<2;
echo "<br>Bitwise Left Shift=$c";
$c=14>>2;
echo "<br>Bitwise Right Shift=$c";
?>
8)Conditional operator(?:)/Ternary operator:
Condition?Expression-1:Expression-2
Example:
<?php
$c=14>20?10+20:34+70;
echo "<br>Conditional Operator:$c";
$m="5";
$n=(int)$m;
echo "<br>Value of n:$n";
echo "<br>Value of m:$m";
?>
Constants:
-Constant is a Variable which can not change value during the execution of program.
-Syantax:define(“ConstantVariableName”,value);
-Example:define(“PI”,3.14);
<?php
define('PI',3.14);
$radius=2;
$area=(PI*$radius*$radius);
echo "Area of circle is $area";
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
Decision Making Control Statement:
1)If Statement:
-Syantax:
If(condition)
//body of if
-Example:
<?php
$str1="VJTech";
$str2="VJTech123";
if($str1==$str2)
{
echo "<br>Both Strings are equal";
}
echo "<br>End of the program!!!";
?>
<?php
$age=21;
if($age>=18)
{
echo "<br>You are eligible for voting!!!";
}
echo "<br>End of the program!!!";
?>
<?php
$age=readline("Enter Your Age:");
if($age>=18)
{
echo "<br>You are eligible for voting!!!";
}
echo "<br>End of the program!!!";
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
<?php
$a=100;
$b=2000;
$c=300;
if($a>$b && $a>$c)
{
echo "Greatest Number is $a";
}
if($b>$a && $b>$c)
{
echo "Greatest Number is $b";
}
if($c>$a && $c>$b)
{
echo "Greatest Number is $c";
}
?>
<?php
$a=100;
$b=2000;
$c=300;
if($a<$b && $a<$c)
{
echo "Smallest Number is $a";
}
if($b<$a && $b<$c)
{
echo "Smallest Number is $b";
}
if($c<$a && $c<$b)
{
echo "Smallest Number is $c";
}
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
2)if-else statement:
-if condition is true then program controller executes if body.
-if condition is false then program controller executes else body.
-syantax:
If(condition)
//body of if
else
//body of else
-Example:
<?php
$x=100;
$y=500;
if($x>$y)
{
echo "Greatest Number is $x";
}
else
{
echo "Greatest Number is $y";
}
?>
<?php
$no=15;
if($no%2==0)
{
echo "Number $no is EVEN!!!";
}
else
{
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
echo "Number $no is ODD!!!";
}
?>
3)Nested-if statement:
-one if else within another if is known as nested if-else.
-syntax:
If(condition)
If(condition)
//body of if
else
//body of else
else
//body of else
-Example:
<?php
$no=-1;
if($no!=0)
{
if($no>0)
{
echo "Number is Positive";
}
else
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
{
echo "Number is Negative";
}
}
else
{
echo "Zero is neither positive nor negative";
}
?>
Switch Statement:
-switch,case,default and break are predefined keywords.
-In switch case,we compare value to multiple cases.
-Syntax:
switch(Expression)
case value-1: //block of code
break;
case value-2: //block of code
break;
case value-3: //block of code
break;
case value-N: //block of code
break;
default://block of code.
-Example:
<?php
$day="sun";
switch($day)
{
case "Mon":echo "It is Monday";
break;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
case "Tues":echo "It is Tuesday";
break;
case "wed":echo "It is wednesday";
break;
case "Fri":echo "It is Friday";
break;
case "sat":echo "It is saturday";
break;
case "sun":echo "It is Sunday";
break;
default: echo "Invalid Day!!!";
}
?>
Loop Control structures:
i)for loop
ii)while loop
iii)foreach loop
for loop:
-Syntax:
for(initialization;condition;increment/decrement)
//body of for loop
Example:
<?php
for($x=1;$x<=5;$x++)
{
echo "<br>Value of X:".$x;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
}
?>
While loop:
-Syntax:
while(condition)
//body of while loop
-Example:
<?php
$x=1;
while($x<=5)
{
echo "<br> Value of x:".$x;
$x++;
}
?>
Do-While loop:
-Syntax:
do
//body of Do-while loop
} while(condition);
-Example:
<?php
$x=1;
do
{
echo "<br>Value of X:".$x;
$x++;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
}while($x<=5);
?>
foreach loop:
-foreach loop provides an easy way to iterate over an array.
-Syntax:
foreach(Array_Expression as $value)
//body of foreach loop
-Example:
<?php
$Num=array(10,20,30,40,50);
echo "Array Element are:";
foreach($Num as $x)
{
echo "$x ";
}
?>
<?php
$Num=array(10,20,30,40,50);
$sum=0;
foreach($Num as $x)
{
$sum=$sum+$x;
}
echo "Sum of Array Element:".$sum;
?>
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
break Statement:
-When break statement is executed then program controller comes out of loop.
-Example:
<?php
for($i=1;$i<=5;$i++)
{
if($i==3)
{
break;
}
echo "<br>Value of i=".$i;
}
?>
continue Statement:
-When continue statement is executed then program controller goes to next iteration.
-Example:
<?php
for($i=1;$i<=5;$i++)
{
if($i==3)
{
continue;
}
echo "<br>Value of i=".$i;
}
?>
Practice Program:
1)Write a PHP program which accept five subject marks from user and display total and
percentage of student.
Ans:
<?php
$clang=readline("Enter c Lang Marks:");
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
$math=readline("Enter Math Marks:");
$java=readline("Enter JAVA Marks:");
$eng=readline("Enter English Marks:");
$dte=readline("Enter DTE Marks:");
$total=($clang+$math+$java+$eng+$dte);
$avg=$total/5;
echo "<br>Total subject marks:".$total;
echo "<br>Percentage marks:".$avg;
?>
2)Write a PHP program to find area of Reactangle.
Ans:
<?php
$length=readline("Enter Length of Reactangle:");
$breadth=readline("Enter breadth of Reactangle:");
$area=$length*$breadth;
echo "Area of Reactangle:".$area;
?>
3)Generates following output in PHP.
**
***
****
*****
Ans:
<?php
for($i=1;$i<=5;$i++)
for($j=1;$j<=$i;$j++)
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
echo "* ";
echo "\n";
?>
4)Generates following output in PHP.
**
***
****
*****
Ans:
<?php
for($i=1;$i<=5;$i++)
for($k=4;$k>=$i;$k--)
echo " ";
for($j=1;$j<=$i;$j++)
echo "* ";
echo "\n";
?>
5)Write a PHP Program to check whether no is prime or not.
Ans:
<?php
$no=readline("Enter Any Integer Number:");
$flag=0;
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
for($i=2;$i<$no;$i++)
if($no%$i==0)
$flag=1;
break;
if($flag==0)
echo "Number is prime";
else
echo "Number is not prime";
?>
Inspiring Your Success
VJTech Academy…
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)
Expression and control statements in PHP
PHP Programming by Mr.Vishal Jadhav Sir’s(VJTech Academy,contact us:+91-9730087674)