0% found this document useful (0 votes)
33 views12 pages

Practical No2php

The document discusses PHP programming concepts like if/else statements, switch statements, loops, and arrays. It includes code examples to demonstrate these concepts and exercises for the reader to practice them. Specifically, it shows how to use if/else statements to check conditions, switch statements to select between multiple cases, for, while and do-while loops to iterate, and indexed, associative and multidimensional arrays to store and access data.

Uploaded by

Nikita Borhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views12 pages

Practical No2php

The document discusses PHP programming concepts like if/else statements, switch statements, loops, and arrays. It includes code examples to demonstrate these concepts and exercises for the reader to practice them. Specifically, it shows how to use if/else statements to check conditions, switch statements to select between multiple cases, for, while and do-while loops to iterate, and indexed, associative and multidimensional arrays to store and access data.

Uploaded by

Nikita Borhade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Practical no.

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:

<input type="number" name="number">

<input type="submit" value="Submit">

</form>

</body>

</html>

<?php

if($_POST){

$number = $_POST['number'];

if(($number % 2) == 0){

echo "$number is an Even number";

}else{

echo "$number is Odd number";

?>
Exercise
1.Write a program to make the use of logical operators.

<?php

$year = 2021;

if(($year % 400 == 0) || (($year % 100 != 0) && ($year % 4 == 0))){

echo "$year is a leap year.";

} else{

echo "$year is not a leap year.";

?>
2.Write a program to check no is positive or negative.

<?php

$number=12;

if($number>0)

echo "$number is positive";

else

echo "$number is negative";

?>
Program code:
Write a program to print first 30 even numbers.
For loop

<?php

for ($i = 1; $i <= 60; $i++) {

if (($i % 2) == 0) {

echo " $i ";

?>

While loop

<?php

$i=0;

while($i <= 60){

if($i % 2 == 0){

echo $i." ";}

$i++; }
?>

Do-while loop

<?php

$i=0;

do{

if($i % 2 == 0){

echo $i." ";

$i++;

}while($i <= 60)

?>
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

$cars= array("Volvo", "BMW", "Toyota");

$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>

You might also like