Page 1 of 2 12 LastLast
Results 1 to 10 of 42

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

Hybrid View

Previous Post Previous Post   Next Post Next Post
  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
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    Do not use single quotes for numbers

    $a = 3;
    $b = 8;



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

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

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

    Default

    I got an error

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

    Code:
    Parse error:  syntax error, unexpected 'if' (T_IF) in /home/fwhphp/public_html/sherly/medicine.php on line 3
    Code:
    <?php
    $overtime=40
    if ($overtime<=60)
    {
    $pay_amount=1500;
    $medicine=1000;
    echo 'Pay Amount : $pay_amount : Medicine : $medicine';
    }
    else
    {
    $$pay_amount=2000;
    $medicine=1700;
    echo 'pay Amount : $pay_amount : Medicine : $medicine';
    }

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

    Default

    $overtime=40
    Missing ending semicolon (;)
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

    Default

    http://php.flashwebhost.com/annie/if...reater_bug.php

    Code:
    <?php
    $passScore = 210;
    $studentName = 'Mike';
    $studentScore = 190;
    if ($studentScore > $passScore) {
    echo 'Congratulations' . $studentName . 'You passed the exam with' . $studentMark . 'mark.';
    } else {
    
    echo 'Passing Score is'  .  $passScore . '.Your Score is' . $studentScore ; 
    
    echo '<br>';
    
    echo 'Sorry You did not pass the exam. Good luck and Cheers next time. ';
    }

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

    Default

    Try this code

    Code:
    <?php
    $overtime=40;
    if ($overtime<=60)
    {
    $pay_amount=1500;
    $medicine=1000;
    echo 'Pay Amount ' . $pay_amount .' Medicine ' . $medicine;
    }
    else
    {
    $pay_amount=2000;
    $medicine=1700;
    echo 'pay Amount ' . $pay_amount . ' Medicine ' . $medicine;
    }

    Quote Originally Posted by sherlyk View Post
    I got an error

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

    Code:
    Parse error:  syntax error, unexpected 'if' (T_IF) in /home/fwhphp/public_html/sherly/medicine.php on line 3
    Code:
    <?php
    $overtime=40
    if ($overtime<=60)
    {
    $pay_amount=1500;
    $medicine=1000;
    echo 'Pay Amount : $pay_amount : Medicine : $medicine';
    }
    else
    {
    $$pay_amount=2000;
    $medicine=1700;
    echo 'pay Amount : $pay_amount : Medicine : $medicine';
    }

  9. #9
    Join Date
    Feb 2005
    Location
    India
    Posts
    11,004

    Default

    http://php.flashwebhost.com/melbin/days.php

    PHP Code:
    <?php
    $day
    ='Sunday';
    if (
    $day="Sunday") {
        echo 
    'Have a nice Sunday';
    }

    else {
        echo 
    'Have a nice day';
    }
    In this code when i use == on line no:3
    if ($day=="Sunday") {
    i am getting the result as FALSE. Why ?

    Doubt 2

    Is it possible to get the day by using PHP date and time function ?
    VIDEO WORLD : LATEST HOLLYWOOD || BOLLYWOOD || SOUTH INDIAN VIDEOS || TRAILERS

  10. #10
    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";
    }

Page 1 of 2 12 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
  •