SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if, elseif and else - PHP n00b question

    Hi,

    I'm learning PHP and I was wondering the following: is there a possibility to combine certain requirements in an 'if statement'?

    Example:

    Say I need the page's language to be English (en) and the page id to be test (test) in order for the if-statement to be read as true.

    How do I state that in PHP?

    PHP Code:
     
    if ($_GET["lang"] == "en" AND $_GET["id"] == "test") {
      print (
    "blahblah english test");
    } else (
    $_GET["lang"] != "en" AND $_GET["id"] != "test") {
      print (
    "blahblah NOT english test"); 
    It's obvious fromt the test I did this won't work with the AND in between. I believe this issue must be so simple and easy it isn't even in the PHP manual of the three Sitepoint books I have...
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions

  2. #2
    SitePoint Zealot s21825's Avatar
    Join Date
    Oct 2003
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't put any conditions in the 'else' statement. The 'else' part of the code will get executed whenever the 'if' statement evaluates to false.

    If you really need a second set of conditions for the 'else' part, use 'elseif' instead.

    PHP Code:
    if ($_GET["lang"] == "en" AND $_GET["id"] == "test") {
      print (
    "blahblah english test");
    } else {
      print (
    "blahblah NOT english test");

    s21825

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if($_GET["lang"] == "en" && $_GET["id"] == "test")
    echo "English test";
    else
    echo "NOT english test";

    It will go to the else only if $_GET['lang'] is not set to "en" and $_GET[id] is not set to test, so checking again is really no use.

  4. #4
    reputation consultant ThaVincy's Avatar
    Join Date
    Mar 2003
    Location
    Flanders
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah, the recheck is totally my bad, I copy-pasted to fast in that post there, it used to be an elseif but I figured that would only complicate the example...

    Ok, now I know the '&&' should do the trick. Thanks!
    ThaVincy | redesigning your image
    Twizted Imagebuilding personal and corporate reputation solutions


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
  •