UNIT-I_Expression_and_control_Statement_in_PHP
UNIT-I_Expression_and_control_Statement_in_PHP
• Variables:
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
• 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
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";
?>
• Data Type:
- Diagram:
-In PHP,floating point number will display value upto 15 digits decimal places.
-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:
-PHP Provided method is_string() to check whether given value is String or not.
-Example:
<?php
$name = "VJTech";
else
?>
4)Boolean:
-Example:
<?php
$m = true;
if(is_bool($m))
else
?>
-We can retrive the individual array element using subscript and index number.
-Example:
<?php
?>
6)objects:
-Example:
<?php
class student
function get_stud_info($nm)
$this->name = $nm;
function disp_stud_info()
$s1->get_stud_info("Dennis");
$s1->disp_stud_info();
?>
7)Resource:
-One more benefits of resource is that they are garbage collected when no longer in use.
-Example:
<?php
$result = database_connect();
database_query($result);
if(is_resource($result))
?>
8)Null:
-This data type is having only one value denoted by the keyword Null.
-Example:
<?php
$result = null;
if(is_null($result))
else
?>
Operators:
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";
?>
<?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";
?>
-Example:
<?php
$str1="Welcome";
$str2=$str1.' '."Friend";
echo "Concatenated String is $str2";
?>
4)Assignment operator:
-It is used to assign right side expression result or value to the left side variable.
-Example:
<?php
$a=100;
$b=200;
$a+=$b;
5)Comparison operator:
i)Equal(==) :$a==$b;
ii)Identical(===) :$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;
$c=$a!==$b;
echo "<br>$a!==$b=".$c;
$c=$a===$b;
echo "<br>$a===$b=".$c;
?>
6)Logical Operator:
iii)Logical XOR(xor) :If both sides are identical then it is false otherwise TRUE
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;
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.
Diagram:
Example:
<?php
$a=12;
$b=13;
$c=$a | $b;
echo "<br>Bitwise OR=$c";
$c=$a ^ $b;
echo "<br>Bitwise XOR=$c";
$c=~12;
$c=14<<2;
echo "<br>Bitwise Left Shift=$c";
$c=14>>2;
echo "<br>Bitwise Right Shift=$c";
?>
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";
?>
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
$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";
}
?>
-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
{
3)Nested-if statement:
-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
{
echo "Number is Negative";
}
}
else
{
echo "Zero is neither positive nor negative";
}
?>
Switch Statement:
-Syntax:
switch(Expression)
break;
break;
break;
break;
default://block of code.
-Example:
<?php
$day="sun";
switch($day)
{
case "Mon":echo "It is Monday";
break;
i)for loop
ii)while loop
iii)foreach loop
for loop:
-Syntax:
for(initialization;condition;increment/decrement)
Example:
<?php
for($x=1;$x<=5;$x++)
{
echo "<br>Value of X:".$x;
}
?>
While loop:
-Syntax:
while(condition)
-Example:
<?php
$x=1;
while($x<=5)
{
echo "<br> Value of x:".$x;
$x++;
}
?>
Do-While loop:
-Syntax:
do
} while(condition);
-Example:
<?php
$x=1;
do
{
echo "<br>Value of X:".$x;
$x++;
}while($x<=5);
?>
foreach loop:
-Syntax:
foreach(Array_Expression as $value)
-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;
?>
-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
$total=($clang+$math+$java+$eng+$dte);
$avg=$total/5;
?>
Ans:
<?php
$area=$length*$breadth;
?>
**
***
****
*****
Ans:
<?php
for($i=1;$i<=5;$i++)
for($j=1;$j<=$i;$j++)
echo "\n";
?>
**
***
****
*****
Ans:
<?php
for($i=1;$i<=5;$i++)
for($k=4;$k>=$i;$k--)
for($j=1;$j<=$i;$j++)
echo "\n";
?>
Ans:
<?php
$flag=0;
if($no%$i==0)
$flag=1;
break;
if($flag==0)
else
?>
VJTech Academy…