Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: Day 4 - Decision Making - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 4 - Decision Making - Become PHP Expert in 30 days

    Decision Making

    Like in real life, computer programs have to make decisions. IF statement is used to make decisions in PHP.



    cow_can_fly.php

    PHP Code:
    <?php

    $cowsCanFly 
    'yes'// try changing the value to 'no'

    if ($cowsCanFly == 'yes') {
        echo 
    'We choose to go to the moon';
    }

    if (
    $cowsCanFly == 'no') {
        echo 
    'Earth is better than heaven.';
    }
    == comparison operator compare value of variable $cowsCanFly with string 'yes', if matches, execute statements inside {}.



    Comparison Operators in PHP

    These are useful operators, you can skip to examples, you will learn to use them as required, don't have to spend much time remembering these at this point.

    == Check if two values are equal.
    != Check if values are not equal
    > Greater than
    < Less than
    >= Greater than or Equal
    <= Less than or Equal


    More Examples


    if_greater.php

    In this example, we check if student have passed exam or not.

    PHP Code:
    <?php

    $passMark 
    60;

    $studentName 'Alex';
    $studentMark 80;

    if (
    $studentMark $passMark) {
       echo 
    'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
    }
    In this code, experiment changing value of $passMark and $studentMark.

    If..Else

    In previous example, we only show message if student passed exam. With help of If..Else, we can tell student when he failed exam.

    if_else_greater.php

    PHP Code:
    <?php

    $passMark 
    60;

    $studentName 'Alex';
    $studentMark 80;

    if (
    $studentMark $passMark) {
       echo 
    'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
    } else {
       echo 
    'Sorry! You failed. Good luck next time.';
    }
    A Bug and unlucky students...

    We have a bug in our code. Try following example.

    if_else_greater_bug.php


    PHP Code:
    <?php

    $passMark 
    60;

    $studentName 'Unlucky Alex';
    $studentMark 60;

    if (
    $studentMark $passMark) {
       echo 
    'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
    } else {
       echo 
    'Sorry! You failed. Good luck next time.';
    }

    The student have 60 Marks, but student failed as we are checking if student have more marks than $passMark (60).

    How to fix this ? Hacky way to fix this is

    find

    Code:
    if ($studentMark > $passMark) {
    Replace With

    Code:
    if ($studentMark > ($passMark - 1)) {
    The proper way is to use Greater Than Or Equal Comparison Operator (>=)

    The code will look like

    if_else_greater_bug_fix.php

    PHP Code:
    <?php

    $passMark 
    60;

    $studentName 'Alex';
    $studentMark 60;

    if (
    $studentMark >= $passMark) {
       echo 
    'Congratulations ' $studentName '! You passed exam with ' $studentMark ' mark'
    } else {
       echo 
    'Sorry! You failed. Good luck next time.';
    }
    If..Else..If

    If statement can be nested, so that you can check for multiple conditions.

    Lets go back and improve our cow code.

    cow_version_2.php

    PHP Code:
    <?php

    $cowsCanFly 
    'what ?'// try changing the value - yes, no, what ?

    if ($cowsCanFly == 'yes') {
        echo 
    'We choose to go to the moon';
    } else if (
    $cowsCanFly == 'no') {
        echo 
    'Earth is better than heaven.';
    } else {
        echo 
    'You should learn more about cows.';
    }



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/dogs.php

    Code:
    <?php
    
    $dogsCanFly = 'yes';  // try changing the value to 'no'
    
    if ($dogsCanFly =='yes') {
       echo 'we choose got to space';
    }
    
    if (dogsCanFly == 'no') {
       echo 'Kennel is better than space.';
    }

  3. #3
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/numbers.php

    Code:
    <?php
    // Number comparison
      $a='3';
      $b='8';
     if ($a<$b)
     {
     echo $a. 'is smaller than'.$b;
     }

  4. #4
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/numbers1.php

    Code:
    <?php
    if ($a > $b) {
      echo "a is greater than b";
    } else {
      echo "a is NOT greater than b";
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/abc.php

    Code:
    Parse error:  syntax error, unexpected '{' in /home/fwhphp/public_html/sherly/abc.php on line 5
    Code:
    <?php
    
    if ($a > $b) {
        echo 'a is bigger than b';
    }  esle  if ($a == $b)  {
       echo 'a is equal to b';
    }  else  ($a < $b) {
       echo 'a is smaller than b';
    }
    Last edited by sherlyk; 05-13-2014 at 07:04 AM.

  6. #6
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/ressult.php

    Code:
    <?php
    $result = 80;
    
    if ($result >= 85) { 
        echo "Passed: Grade A <br />";
    }
    elseif ($result >= 70) {
        echo "Passed: Grade B <br />";
    }
    elseif ($result >= 65) {
        echo "Passed: Grade C <br />";
    }
    else {
        echo "Failed <br />";
    }

  7. #7
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/cow_can_fly.php

    Code:
    <?php
    $cowsCanFly = 'no'; //try changing value to 'no'
    
    if  ($cowsCanFly == 'yes' )
    
    { echo 'There is no time like the present '; }
    
    if ($cowsCanFly == 'no' )
    
    { echo 'When in Rome, do as the Romans'; }

  8. #8
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/numbers1.php

    Code:
    <?php
    if ($a > $b) {
      echo "a is greater than b";
    } else {
      echo "a is NOT greater than b";
    }
    Where did this $a and $b comes from ? have you assigned any value ? i don't see it in code.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  9. #9
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by sherlyk View Post
    http://php.flashwebhost.com/sherly/abc.php

    Code:
    Parse error:  syntax error, unexpected '{' in /home/fwhphp/public_html/sherly/abc.php on line 5
    Code:
    <?php
    
    if ($a > $b) {
        echo 'a is bigger than b';
    }  esleif ($a == $b)  {
       echo 'a is equal to b';
    }  else  ($a < $b) {
       echo 'a is smaller than b';
    }
    First of all, where did you set $a and $b ? With out such a variable how it works ?

    The error is because

    esleif
    Spelling mistake, use else if

    Fixed code


    Code:
    <?php
    
    $a = 1;
    $b = 100;
    
    if ($a > $b) {
        echo 'a is bigger than b';
    }  else if ($a == $b)  {
       echo 'a is equal to b';
    }  else  ($a < $b) {
       echo 'a is smaller than b';
    }
    
    echo '<p>Value of $a = ' . $a . ' and value of $b = ' . $b . '</p>';
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  10. #10
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Quote Originally Posted by annie View Post
    http://php.flashwebhost.com/annie/cow_can_fly.php

    Code:
    <?php
    $cowsCanFly = 'no'; //try changing value to 'no'
    
    if  ($cowsCanFly == 'yes' )
    
    { echo 'There is no time like the present '; }
    
    if ($cowsCanFly == 'no' )
    
    { echo 'When in Rome, do as the Romans'; }
    Code formatting is not proper.

    Code:
    <?php
    
    $cowsCanFly = 'no'; //try changing value to 'no'
    
    if  ($cowsCanFly == 'yes' ) {
       echo 'There is no time like the present '; 
    }
    
    if ($cowsCanFly == 'no' ) { 
        echo 'When in Rome, do as the Romans'; 
    }
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Page 1 of 5 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •