SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Faulty IF Function?

    Can anyone spot the problem with my IF statement? It does BOTH, echoing $Nickname AND including the file nickname.php.

    Thanks.

    PHP Code:
    // INTERNAL SWITCH
    if($Nickname['Symbol'] != '')
    { echo 
    $Nickname; }
    else
    echo 
    "\n"; include($_SERVER['DOCUMENT_ROOT']."/nickname.php");
    // END INTERNAL SWITCH 

  2. #2
    Wadge! F4nat1c's Avatar
    Join Date
    Oct 2005
    Location
    South Wales, UK
    Posts
    1,134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If what you're checking is for an empty, then why not use:

    PHP Code:
    // INTERNAL SWITCH
    if( empty($Nickname['Symbol']) )

    { echo 
    $Nickname; }

    else {

    echo 
    "\n"

    include(
    $_SERVER['DOCUMENT_ROOT']."/nickname.php");

    }
    // END INTERNAL SWITCH 
    OMFG SitePoint ROXORZ TEH BIG ONE111!
    Wish you were invisible?

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I got kind of confused, but your script works for me when I reverse it. Thanks.

    PHP Code:
    if( empty($Nickname['Symbol']) )
    include(
    $_SERVER['DOCUMENT_ROOT']."/nickname.php");
    else {
    echo 
    "\n";
    echo 
    $Nickname;


  4. #4
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would advise again using empty() for checking variables. Use strlen($var) > 0, or $var === '' or something like that, because empty has rather strange behaviour: it returns true for
    - "" (an empty string)
    - 0 (0 as an integer)
    - "0" (0 as a string)
    - NULL
    - FALSE
    - array() (an empty array)
    - var $var; (a variable declared, but without a value in a class)

    it's the "0" and array() values that bother me, so I don't like empty at all. But it's just a matter of preference.

    Regards

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Like this eh?

    PHP Code:
    if( strlen($Nickname['Symbol']) > )
    echo 
    $Nickname;
    else {
    include(
    $_SERVER['DOCUMENT_ROOT']."/a1/inc/suggest/nickname.php");

    That seems to work fine, too. Since you feel it's more secure, I'll use it. Thanks for the tip.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Location
    Amsterdam
    Posts
    88
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This happens because PHP interprets
    PHP Code:
    $x 4;
    if (
    $x == 4)
    {
        echo 
    "dsa";
    }
    else echo 
    "sa"; echo '123'
    like
    PHP Code:
    $x 4;
    if (
    $x == 4)
    {
        echo 
    "dsa";
    }
    else echo 
    "sa"
    echo 
    '123'
    so always use brackets if you have more than one command to run - or if you use more than one ';' in the statement.
    Hope that makes it clear...


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
  •