Practical 2 (A)
Practical 2 (A)
In an if conditional statement, the following types of operators are commonly used to evaluate conditions:
These operators compare values and return a Boolean result (true or false):
==: Equal to
!=: Not equal to
>: Greater than
<: Less than
>=: Greater than or equal to
<=: Less than or equal to
2. Logical Operators
These operators work at the bit level but can be used in conditional logic in some cases:
These operators are typically used in the condition part of the if statement to control the flow based on
logical expressions.
php
Copy code
if (condition) {
// This block is executed if the condition is true
} else {
// This block is executed if the condition is false
}
Example:
php
Copy code
$age = 20;
In this example, since $age is 20, which is greater than or equal to 18, the condition ($age >= 18) is true,
so the code inside the if block (echo "You are an adult.";) will be executed. The else block will be
skipped.
3) State the condition when the else part will be executed with example.
In PHP, the else part of an if-else statement will be executed when the condition in the if part evaluates
to false.
The else block will execute if the condition in the if statement is not true (i.e., the condition
evaluates to false).
Example:
php
Copy code
<?php
$age = 15;
Explanation:
Thus, the else part will be executed when the condition in the if statement is false.
4o mini
Program Code :-
<Html>
<Head>
<Title> Find out odd or even number. . ! </title>
</Head >
<body>
<?php
$a = 10;
if ($a % 2==0)
{
echo "Given number is" . "<br>" . "<b>EVEN<b>" ;
}
else
{
echo "Given number is" . "<br>" . "<b>ODD<b>";
}
?>
</body>
</html>
Output :-
Exercise :-
Program Code :-
<?php
$a = 42;
$b = 0;
if ($a && $b)
{
echo "TEST1 : Both a and b are true \n";
}
else
{
echo "TEST1 : Either a or b is false \n";
}
if ($a and $b)
{
echo "TEST2 : Both a and b are true \n";
}
else
{
echo "TEST2 : Either a or b is false \n";
}
if ($a || $b)
{
echo "TEST3 : Either a or b is true \n";
}
else
{
echo "TEST3 : Both a and b are false \n";
}
if ($a or $b)
{
echo "TEST4 : Either a or b is true \n";
}
else
{
echo "TEST4 : Both a and b are false \n";
}
$a = 10;
$b = 20;
if ($a)
{
echo "TEST5 : a is true \n";
}
else
{
echo "TEST5 : a is false \n";
}
if ($b)
{
echo "TEST6 : b is true \n";
}
else
{
echo "TEST6 : b is false \n";
}
if (!$a)
{
echo "TEST7 : a is true \n";
}
else
{
echo "TEST7 : a is false \n";
}
if (!$b)
{
echo "TEST8 : b is true \n";
}
else
{
echo "TEST8 : b is false";
}
?>
Ootput :-
2) Write a program to check given number is positive or negative.
Program Code :-
<?php
$number = 128;
if ($number > 0)
{
echo $number . " is a positive number";
}
else if ($number < 0)
{
echo $number . " is a negative number ";
}
else if ($number == 0)
{
echo "You have entered zero";
}
else
{
echo " please enter a numeric value";
}
?>
Output :-
Program Code :-
?php
// Function to get the number of days in a month
function getDaysInMonth($month, $year)
{
// Switch statement to determine the number of days
switch ($month)
{
case 1: // January
case 3: // March
case 5: // May
case 7: // July
case 8: // August
case 10: // October
case 12: // December
return 31;
break;
case 4: // April
case 6: // June
case 9: // September
case 11: // November
return 30;
break;
case 2: // February
// Check for leap year
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0))
{
return 29; // Leap year
}
else
{
return 28; // Not a leap year
}
break;
default:
return "Invalid month";
}
}
// Example usage
$month = 2; // February
$year = 2024; // Leap year
$days = getDaysInMonth($month, $year);
echo "The number of days in month $month of year $year is: $days";
?>
Output :-