Results 1 to 14 of 14

Thread: [RESOLVED]Multiple IF's

  1. #1

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Resolved [RESOLVED]Multiple IF's

    hi, how can i have several IF statements in a statement. if you know vb then it may make more sence as:
    VB Code:
    1. If var1 = "something" then
    2.      If var2 = "somethingelse" then
    3.           Do_Something
    4.      End If
    5. End If
    does that help? this is like the best i can explain which either makes me stupid or not that good at thinking.
    thanks, dandono
    Last edited by dandono; Dec 28th, 2005 at 05:26 PM.
    If there is only one perfect person in the universe, does that make them imperfect?

  2. #2
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: Multiple IF's

    if ($var1 == $var2 && $var2 == $var3) {

    }

    && = And
    || = Or

    That what your after?
    Don't Rate my posts.

  3. #3
    Fanatic Member sql_lall's Avatar
    Join Date
    Jul 2002
    Location
    Up Above (i.e. AUS)
    Posts
    571

    Re: Multiple IF's

    If you want it similar to the code you have, i think you can just do:
    Code:
    if($var1 == "hi"){
        if($var2 == "all"){
            // stuff
        }
        // other stuff if you want
    }
    sql_lall

  4. #4

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: Multiple IF's

    thanks. i have not been able to figure that one out for the time i have "known" php.
    If there is only one perfect person in the universe, does that make them imperfect?

  5. #5
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Multiple IF's

    you could also use if/elseif/else statements.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  6. #6

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: Multiple IF's

    so i could do
    PHP Code:
    If (var1 == "blah")
    {
    echo 
    "blah";
    }elseif(
    var2 == "blah2")
    {
    echo 
    "blah2";

    If there is only one perfect person in the universe, does that make them imperfect?

  7. #7
    Fanatic Member neicedover1982's Avatar
    Join Date
    Jun 2005
    Posts
    566

    Re: Multiple IF's

    Yes, the best format is as follows.
    PHP Code:
    If (var1 == "blah") { 
        echo 
    "blah"
    } elseif (
    var2 == "blah2") { 
        echo 
    "blah2";
    } else {
        echo 
    "blah3";

    You can put as many elseif's as you want, I would suggest you also use the "else" at the end as a "catch all" incase the if statements dont catch whats being passed, kind of like error checking.

    I use it and have it post the info its looking at so if I ever see it, I know whats going on.
    Kevin | New England Iced Over | http://www.kevincawleyjr.com

  8. #8

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: Multiple IF's

    ok thanks. this will help in a new login script for my website.
    If there is only one perfect person in the universe, does that make them imperfect?

  9. #9

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: [RESOLVED]Multiple IF's

    i got an error on this line of code:
    PHP Code:

    elseif(
    $admin == "daniel"

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in D:\main\index.php on line 22
    i dont know what is wrong. if it helps i can post the whole code but it is 99 lines of code but most of it is html
    If there is only one perfect person in the universe, does that make them imperfect?

  10. #10

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: [RESOLVED]Multiple IF's

    sorry i fixed it. the problem was that i was echoing a string and it was started with the " type thing and closed with the ' type thing. my brain is a bit slow today and i cant remember what they are called
    If there is only one perfect person in the universe, does that make them imperfect?

  11. #11
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: [RESOLVED]Multiple IF's

    double "quotes" single 'quotes'
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  12. #12

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: [RESOLVED]Multiple IF's

    oh yeah
    If there is only one perfect person in the universe, does that make them imperfect?

  13. #13
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [RESOLVED]Multiple IF's

    Try to use single quotes rather than double quotes, unless you want embedded variables and/or escape sequences such as \n in the string.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  14. #14

    Thread Starter
    Hyperactive Member dandono's Avatar
    Join Date
    Aug 2004
    Location
    Cornwall, UK
    Posts
    485

    Re: [RESOLVED]Multiple IF's

    i normaly use single quotes because if you do something like
    PHP Code:
    echo "<img src="someimg.jpg">"
    then it thinks the string ends at "src=" and then makes an error but if you did
    PHP Code:
    echo '<img src="someimg.jpg">'
    then it would work fine.
    If there is only one perfect person in the universe, does that make them imperfect?

Posting Permissions

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



Click Here to Expand Forum to Full Width