Day 4: Comments and Control Structures

Comment code is a line that is not executed as a part of the program. It’s only readable by someone who is editing the code!

Comments are useful for Documentation to let other members of your team understand what you were doing in your code.

PHP supports three ways of commenting:

<?php
// This is a single line comment

# This is another way for single line comment

/*
This is a multiple lines comment
for more then
one line
*/
?>

Control Structures

  1. If
  2. If-else
  3. While loop
  4. For loop
  5. Switch – case

1. Understanding “if” Control Structure

“If” control structure is use to check the condition, if condition will true, then the statement will execute.

Syntax:
if (condition) {
statement here will execute when condition is true.
}

Program 9: Simple execution of “if”, checking is number is greater then 5

<?php
$a = 7;
if($a > 5) {
echo “a is greater then 5”;
}
?>

OUTPUT:
a is greater then 5


Explanation: in above example if value of $a will greater than 5, only then the condition will true and statement will execute, otherwise it will display nothing. You can change the value of $a and see results.

Program 10: “If” with comparison operator

<?php
$a = 5;
if($a == 5) {
echo “a is equal to 5”;
}
?>

OUTPUT:
a is equal to 5


Explanation: in above example if value of $a will equal to 5, only then the condition will true and statement will execute otherwise it will display nothing. You can change the value of $a and see results.

Note: In above example “==” is used to compare the value of $a. “==” is called comparison operator.
We can also use “<, <=, >, >=, !=, <>” with if control structure. These are called relational operators.

2) Understanding “if-else” Control Structure

If we have to check only ture condition and display result than we can simply use “if” as above, but if we have to display statement according to both true and false condition then “if – else” control structure will use.

Program 11: “if-else” example

<?php
$a = 4;
if($a > 5) {
echo “a is greater than 5”;
} else {
echo “a is less than 5”;
}
?>

OUTPUT:
a is less than 5


Explanation: in above example, if value of $a will greater than 5, then “a is greater than 5” will execute else “a is less than 5” will display.

Note: Guess what will the output if value of $a will 5. It will display “a is less than 5” because “($a > 5)” condition is false. So in this case there can be more than 2 possible outputs. See below example of “else-if” to display more than 2 outputs with more than 2 conditions.

Program 12: Multiple if-else

<?php
$a = 5;
if($a > 5) {
echo “a is greater than 5”;
} else if ($a<5) { echo "a is less than 5"; } else { echo "a is equal to 5"; } ?>

OUTPUT:
a is equal to 5


Assignments:

  • Write a program to find a constant is odd or even.
  • [Hint: if( $n % 2 == 0)], you can use “%” (mode operator), it returns reminder.

  • Write a program to find a constant is leap year or Not.
  • [Hint: $y=2000; if( $y % 4 == 0 )]

  • Write a program to calculate sum and average of 3 numbers (assume marks in hindi, english and math), apply following conditions in average:
  • If average of marks greater than 60% output should be: You got 1st Division.
    If average of marks greater than 50% output should be: You got 2nd Division.
    If average of marks greater than 35% output should be: You got 3rd Division.
    If average of marks less than 35% output should be: Failed.

Leave a Reply