Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    variable in if structure does not run

    wh doesnot it run folllowing $value?

    whare is wrong ?
    PHP Code:
    $i=3;


        if(
    $i 1){$value  "a";} 
    elseif(
    $i 2){$value  "b";}  
    elseif(
    $i 3){$value  "c";}  
    elseif(
    $i 4){$value  "d";}
    elseif(
    $i 5){$value  "e";}


    echo 
    $value 

  • #2
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    You need to compare the values with "==" instead of assigning with "=" (which, also returns the value you set)

    i.e.
    PHP Code:
    $i=3;

        if (
    $i == 1) {$value "a";} 
    elseif (
    $i == 2) {$value "b";}  
    elseif (
    $i == 3) {$value "c";}  
    elseif (
    $i == 4) {$value "d";}
    elseif (
    $i == 5) {$value "e";}

    echo 
    $value
    It might be a bit prettier if you did:

    PHP Code:
    $i=3;

    switch (
    $i) {
        case 
    1:
           
    $value "a";
           break;
        case 
    2:
           
    $value "b";
           break;
        case 
    3:
           
    $value "c";
           break;
        case 
    4:
           
    $value "d";
           break;
        case 
    5:
           
    $value "e";
           break;
       default:
          echo 
    'Unknown value for $i';
    }

    echo 
    $value
    -- but don't forget the "break"s!
    Last edited by Ahri; 03-10-2010 at 03:06 PM. Reason: Added semicolon
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #3
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Ahri View Post
    You need to compare the values with "==" instead of assigning with "=" (which, also returns the value you set)

    i.e.
    PHP Code:
    $i=3;

        if (
    $i == 1) {$value "a";} 
    elseif (
    $i == 2) {$value "b";}  
    elseif (
    $i == 3) {$value "c";}  
    elseif (
    $i == 4) {$value "d";}
    elseif (
    $i == 5) {$value "e";}

    echo 
    $value
    It might be a bit prettier if you did:

    PHP Code:
    $i=3;

    switch (
    $i) {
        case 
    1:
           
    $value "a";
           break;
        case 
    2:
           
    $value "b";
           break;
        case 
    3:
           
    $value "c";
           break;
        case 
    4:
           
    $value "d";
           break;
        case 
    5:
           
    $value "e";
           break;
       default:
          echo 
    'Unknown value for $i';
    }

    echo 
    $value
    -- but don't forget the "break"s!

    is it possible to do it using ternary operator?

  • #4
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by zodehala View Post
    is it possible to do it using ternary operator?
    Anything's possible:
    PHP Code:
    $i 3;

    $value = ($i == 1) ? "a" : (
             (
    $i == 2) ? "b" : (
             (
    $i == 3) ? "c" : (
             (
    $i == 4) ? "d" : (
             (
    $i == 5) ? "e" : (
             
    'Unknown value for $i')))));

    echo 
    $value
    ... though not necessarily desirable

    I'm not a fan of using a ternary "if" in this case as it's pretty unreadable: this is the sort of tactic used to obfuscate code!
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini


  •  

    Posting Permissions

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