0% found this document useful (0 votes)
5 views9 pages

Practical 2 (A)

The document provides an overview of PHP conditional statements, including operators used in if statements, the execution flow of if-else constructs, and examples of code to determine even/odd numbers, check positivity/negativity, and calculate days in a month using a switch statement. It explains relational, logical, ternary, and bitwise operators, along with practical programming exercises. The document serves as a practical guide for understanding and implementing conditional logic in PHP.
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)
5 views9 pages

Practical 2 (A)

The document provides an overview of PHP conditional statements, including operators used in if statements, the execution flow of if-else constructs, and examples of code to determine even/odd numbers, check positivity/negativity, and calculate days in a month using a switch statement. It explains relational, logical, ternary, and bitwise operators, along with practical programming exercises. The document serves as a practical guide for understanding and implementing conditional logic in PHP.
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/ 9

Subject :- Web Based Application Development with PHP Subject Code :- 22619

Name :- Shivam Sainath Korpakwad Batch :- CO 6 IA Roll No. 24


Practical Related Questions :-

1) List operators used in if conditional statement.

In an if conditional statement, the following types of operators are commonly used to evaluate conditions:

1. Relational (Comparison) Operators

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 combine multiple conditions:

 &&: Logical AND (true if both conditions are true)


 ||: Logical OR (true if at least one condition is true)
 !: Logical NOT (inverts the truth value of the condition)

3. Ternary (Conditional) Operator

A shorthand for an if-else statement:

 condition ? expr1 : expr2: If condition is true, expr1 is executed; otherwise, expr2 is


executed.

4. Bitwise Operators (less common in basic conditionals but can be used)

These operators work at the bit level but can be used in conditional logic in some cases:

 &: Bitwise AND


 |: Bitwise OR
 ^: Bitwise XOR
 ~: Bitwise NOT

These operators are typically used in the condition part of the if statement to control the flow based on
logical expressions.

2) In if-else construct which part will be executed if condition is true.


In a PHP if-else construct, the part that gets executed when the condition is true is the block of code
within the if statement.

Here is the general structure:

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;

if ($age >= 18) {


echo "You are an adult."; // This will be executed because the condition is true
} else {
echo "You are a minor.";
}

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.

Condition for executing the else part:

 The else block will execute if the condition in the if statement is not true (i.e., the condition
evaluates to false).

Syntax of if-else in PHP:


php
Copy code
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}

Example:
php
Copy code
<?php
$age = 15;

if ($age >= 18) {


echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}
?>

Explanation:

 In this example, the condition $age >= 18 is being checked.


 If $age is 18 or more, the if block will execute, and the message "You are eligible to vote." will be
displayed.
 If $age is less than 18 (as it is in this case, since $age = 15), the else block will execute, and the
message "You are not eligible to vote." will be displayed.

Output of the example:


css
Copy code
You are not eligible to vote.

Thus, the else part will be executed when the condition in the if statement is false.

4o mini
Program Code :-

1) Write a program to find given number is even or odd.

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

1) Write a program to make the use of logical operators.

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

3) Write a calendar program using switch statement.

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

You might also like