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 10 of 10
  1. #1
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts

    $_GET with value of 0?

    Is it possible to use $_GET to pick up a value of 0? It will treat the value of 0 as a value that doesn't exist?

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Is it possible to use $_GET to pick up a value of 0? It will treat the value of 0 as a value that doesn't exist?
    who? I mean who treat the value as 0 or not exists? From http point of view is a string '0' or "0" without quote, get method take it as it is.
    form php point of view it's tricky, avoid this if you can. This kind of value due to the implicit conversion in php can means '', false or 0, is hard to test and induce a lot of problem depending of how you code it.

    regards

  • #3
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Quote Originally Posted by oesxyl View Post
    who? I mean who treat the value as 0 or not exists?
    Oops, sorry was referring to the $_GET. For example, say I have $myid = 0 and then I use the url with the query string id=$myid. If you highlight the link in the browser it will show the query string as ?id=0, but when you click on it $_GET['id'] doesn't grab the value of 0 or it treats it as a null value. What would I need to do to get around this?

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Oops, sorry was referring to the $_GET. For example, say I have $myid = 0 and then I use the url with the query string id=$myid. If you highlight the link in the browser it will show the query string as ?id=0, but when you click on it $_GET['id'] doesn't grab the value of 0 or it treats it as a null value. What would I need to do to get around this?
    i'm afraid that I don't understand what is the problem.
    $_GET don't change the value. For example:

    PHP Code:
    <?php

    echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
    echo 
    '<br/>';
    echo 
    $_GET['id'];
    ?>
    if you run this will see that the value displayed under the link is 0. That means that if $myid have the value 0 and is passed correct to the url, as you say, the get method take how it is as 0.
    If you post some code or example maybe I can figure out what is the problem.

    I hope this help.

    regards

  • #5
    Regular Coder
    Join Date
    Apr 2006
    Posts
    186
    Thanks
    21
    Thanked 6 Times in 6 Posts
    Using your example, this is what I was doing:
    Code:
    <?php
    
    echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
    echo '<br/>';
    if ($_GET['id'])
    {
    	echo "Hello";
    }
    ?>
    If the value is 0, it won't work, but if it is 1 it will work. But I tried it with isset and now it works.

  • #6
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    That's because php is evaluating the value 0 as "false", and therefore your conditional statement is false and not executing.

  • #7
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Try this instead
    PHP Code:
    if (isset($_GET['id']))
    {
        echo 
    "Hello";

    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #8
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by PeaTearGriffin View Post
    Using your example, this is what I was doing:
    Code:
    <?php
    
    echo '<a href="'.$_SERVER['PHP_SELF'].'?id=0">click here</a>';
    echo '<br/>';
    if ($_GET['id'])
    {
    	echo "Hello";
    }
    ?>
    If the value is 0, it won't work, but if it is 1 it will work. But I tried it with isset and now it works.
    that is implicit conversion. 0 is false,
    using isset make it work no matter if the id is 0 or 100, because isset($_GET['id']) is true, or 1 for any value 0 or non-0.
    testing with isset assure you that variable 'id' exists and is passed using get method with any value. Additional you must test the value with == or ===, for example

    PHP Code:
    if(isset($_GET['id']) && $_GET['id'] === 0){
       
    // yes it is 0

    as I said, this can be tricky.

    regards

  • #9
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    That will never be true as the $_GET comes in as a string. You need to type cast it or use intval.
    PHP Code:
    <?php
    if(isset($_GET['id']) && intval($_GET['id']) === 0){
       
    // yes it is 0
    }
    ?>
    however intval returns 0 if the value isn't a number. You should probably use is_numeric().
    PHP Code:
    if(isset($_GET['id']) && is_numeric($_GET['id']))
    {
        if(
    intval($_GET['id']) === 0){
            echo 
    '$_GET is a number and equals 0';
        }
        else
        {
            echo 
    '$_GET is a number but is not equal to 0';
        }
    }
    else
    {
        echo 
    '$_GET is not a number';

    Last edited by _Aerospace_Eng_; 05-12-2008 at 01:08 AM.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #10
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    That will never be true as the $_GET comes in as a string. You need to type cast it or use intval.
    PHP Code:
    <?php
    if(isset($_GET['id']) && intval($_GET['id']) === 0){
       
    // yes it is 0
    }
    ?>
    yes you are right, I said in a previous post that is a string but I miss that when I post the snippet.

    however intval returns 0 if the value isn't a number. You should probably use is_numeric().
    PHP Code:
    if(isset($_GET['id']) && is_numeric($_GET['id']))
    {
        if(
    intval($_GET['id']) === 0){
            echo 
    '$_GET is a number and equals 0';
        }
        else
        {
            echo 
    '$_GET is a number but is not equal to 0';
        }
    }
    else
    {
        echo 
    '$_GET is not a number';

    As I said, a lot of complication, better to avoid if you can,

    regards


  •  

    Posting Permissions

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