Practical No2php
Practical No2php
02
1) If statement
<?php
$a="Amar";
$b="Akbar";
if($a==$b)
echo "Both string are equal";
echo "if block not executed";
?>
2) If – else statement
<?php
$a="Amar";
$b="Amar";
if($a==$b)
echo "Both string are equal";
else
echo " Both string are not equal ";
?>
3) switch statement
<?php
$favcolor = "red";
switch ($favcolor) {
case "red":
echo "Your favorite color is red!";
break;
case "blue":
"Your favorite color is blue!";
break;
case "green":
echo "Your favorite color is green!";
break;
default:
echo "Your favorite color is neither red, blue, nor green!";
}
?>
Program Code
<html>
<body>
<form method="post">
Enter a number:
</form>
</body>
</html>
<?php
if($_POST){
$number = $_POST['number'];
if(($number % 2) == 0){
}else{
?>
Exercise
1.Write a program to make the use of logical operators.
<?php
$year = 2021;
} else{
?>
2.Write a program to check no is positive or negative.
<?php
$number=12;
if($number>0)
else
?>
Program code:
Write a program to print first 30 even numbers.
For loop
<?php
if (($i % 2) == 0) {
?>
While loop
<?php
$i=0;
if($i % 2 == 0){
$i++; }
?>
Do-while loop
<?php
$i=0;
do{
if($i % 2 == 0){
$i++;
?>
Exercise
1. Write any program using if condition with for loop.
<?php
$end=20;
for($i=1;$i<=$end;$i++)
{
if($i%2==0)
{
echo $i."Even No.<br>";
}else
echo $i."Odd No.<br>";
}
?>
2. Write a program to display pyramids of star/patterns using
increment/decrement.
<?php
for($i=0;$i<=5;$i++){
for($j=0;$j<=$i;$j++){
echo "*";
}
echo "<br/>";
}
?>
Program code:
Develop a program to using Indexed array.
<?php
$arrlength=count($cars);
for($x= 0;$x<$arrlength;$x++)
echo $cars[$x];
echo "<br>";
?>
Exercise:
1. Develop a program to using Associative array.
<html>
<body>
<?php
$age=array("Ram"=>"35", "Mohini"=>"37", "Rahul"=>"43");
echo "Mohini is".$age['Mohini']. "years old.";
?>
</body>
</html>
2. Develop a program to using Multidimensional array.
<html>
<body>
<?php
$cars=array
(
array("Volvo",100,96),
array("BMW", 60,59),
array("Toyota",110,100)
);
echo $cars[0][0].": Ordered: ".$cars[0][1].". Sold: ".$cars[0][2]."<br>";
echo $cars[1][0].": Ordered: ".$cars[1][1].". Sold: ".$cars[1][2]."<br>";
echo $cars[2][0].": Ordered: ".$cars [2][1].". Sold: ".$cars[2][2]."<br>";
?>
</body>
</html>