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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 26

Thread: isset()

  1. #1
    New Coder
    Join Date
    Aug 2017
    Posts
    78
    Thanks
    1
    Thanked 0 Times in 0 Posts

    isset()

    From reading the php documentation, isset ó Determine if a variable is set and is not NULL. Just for clarification, my understanding, is this testing if the variable exists firstly, and if so that its not NULL?

  2. #2
    Administrator VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    10,822
    Thanks
    6
    Thanked 1,292 Times in 1,262 Posts
    Here’s a nice explanation of isset, empty and is_null: https://www.virendrachandak.com/tech...ty-vs-is_null/

    And you can easily find the answer to your question by trying it. Don’t worry, you can’t break anything (seriously).

  3. #3
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    North Carolina
    Posts
    1,499
    Thanks
    4
    Thanked 237 Times in 234 Posts
    Yes to your question. It would have to be set to be tested for NULL. As stated in the documentation:

    Returns TRUE if var exists and has value other than NULL. FALSE otherwise.
    Dave .... HostMonster for all of your hosting needs

  4. #4
    New Coder
    Join Date
    Aug 2017
    Posts
    78
    Thanks
    1
    Thanked 0 Times in 0 Posts
    When it says the variable is set, that can be interpreted as an existing variable has been set a value or a variable name has been set/declared. That to me isn't definitive enough. I think the documentation needs to be clearer than it is, just my opinion. I would replace isset with two functions, is_declared (self-explanatory) and has_value(again self-explanatory). Thanks for the replies.

  5. #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    North Carolina
    Posts
    1,499
    Thanks
    4
    Thanked 237 Times in 234 Posts
    Did you read the information at the link VIPStephan provided? There is no need for additional functions as what is provided already provides what is needed.
    Dave .... HostMonster for all of your hosting needs

  6. #6
    Master Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    5,189
    Thanks
    113
    Thanked 610 Times in 596 Posts
    Quote Originally Posted by richarddunnebsc View Post
    I think the documentation needs to be clearer than it is, just my opinion.
    There isn't much point telling us that, we can't change it. We're not the php people from php.net, we're just a bunch of volunteers on a totally unrelated website that try to help people.

    Quote Originally Posted by richarddunnebsc View Post
    I would replace isset with two functions, is_declared (self-explanatory) and has_value(again self-explanatory). Thanks for the replies.
    Well thats a bit daft really because then you're executing two lines of code instead of one which is twice the work for the php engine and thus you lose efficiency.

    isset is a good function if you know how to use it. If you're complaining that you want a function that only checks the existence of a variable then you're not writing good code - you should already have a good idea of what variables you have in use.
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  7. #7
    New Coder
    Join Date
    Sep 2017
    Location
    Lithuania
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Quote Originally Posted by richarddunnebsc View Post
    When it says the variable is set, that can be interpreted as an existing variable has been set a value or a variable name has been set/declared. That to me isn't definitive enough. I think the documentation needs to be clearer than it is, just my opinion. I would replace isset with two functions, is_declared (self-explanatory) and has_value(again self-explanatory). Thanks for the replies.
    In this case, you may want to use some other function to check if variable needs to be processed. For example, I saw many websites/scripts that check if form was submitted using this function:

    PHP Code:
    if (isset($_POST)) 
    However, it always returns true, even if form was never submitted. Hence, isset should be used with caution or replaced with empty or another function in many situations.

  8. #8
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,262
    Thanks
    3
    Thanked 128 Times in 123 Posts
    Quote Originally Posted by phpmillion View Post
    In this case, you may want to use some other function to check if variable needs to be processed. For example, I saw many websites/scripts that check if form was submitted using this function:

    PHP Code:
    if (isset($_POST)) 
    However, it always returns true, even if form was never submitted. Hence, isset should be used with caution or replaced with empty or another function in many situations.
    As far as this goes, the correct method is
    PHP Code:
    if ($_SERVER['REQUEST_METHOD'] == 'POST'
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  9. #9
    New Coder
    Join Date
    Sep 2017
    Location
    Lithuania
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Well, I simply use if
    PHP Code:
    (!empty($_POST)) 

  10. #10
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,262
    Thanks
    3
    Thanked 128 Times in 123 Posts
    Quote Originally Posted by phpmillion View Post
    Well, I simply use if
    PHP Code:
    (!empty($_POST)) 

    When you submit a form, filled out or not filled out, $_POST is not ever going to be anything other than not empty so what exactly do you think you are testing for? if is a conditional that tests for true or false. You will only ever have true.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  11. #11
    Master Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    5,189
    Thanks
    113
    Thanked 610 Times in 596 Posts
    Quote Originally Posted by phpmillion View Post
    Well, I simply use if
    PHP Code:
    (!empty($_POST)) 
    I personally just look for a hidden field via the isset function. If its there then the rest of the form should be too (additional error checking needed though). If it isn't.. the form hasn't been submitted. Incidentally don't bother looking for the submit button, there is a bug in IE that still hasn't been fixed some 10 years later which means that the button value is not always sent with the form.

    You can do it benanamens way too which seems to be the in thing these days but it is a few more characters of code to write lol.
    Quote Originally Posted by deathshadow View Post
    So seriously, loosen up that tie, let out the belt, and try relating to normal people on the street instead of the gentleman's club crowd.

  12. #12
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,262
    Thanks
    3
    Thanked 128 Times in 123 Posts
    Quote Originally Posted by tangoforce View Post
    I personally just look for a hidden field via the isset function. If its there then the rest of the form should be too (additional error checking needed though). If it isn't.. the form hasn't been submitted. Incidentally don't bother looking for the submit button, there is a bug in IE that still hasn't been fixed some 10 years later which means that the button value is not always sent with the form.

    You can do it benanamens way too which seems to be the in thing these days but it is a few more characters of code to write lol.
    The problem with a hidden field is that means you expect that every user is going to use YOUR form to submit data. There are several other ways it can be done and many reasons why someone might not use YOUR form.

    Say someone was posting data with cURL, they would have to know that your script REQUIRES some hidden piece of data in order to work or it will completely fail. Checking for the REQUEST METHOD is the only foolproof way for your script to always work.

    Having to code in your hidden data pretty much throws out your argument of "a few more characters of code to write" in order to properly handle a form submission.

    Another scenario, consider a team development with guys how do the code and other guys that write the templates. All the template guys would have to know and remember to put in the magical hidden text that allows the script to work and will fail without it.
    Last edited by benanamen; 10-14-2017 at 02:18 AM.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  13. #13
    Senior Coder benanamen's Avatar
    Join Date
    Oct 2015
    Posts
    1,262
    Thanks
    3
    Thanked 128 Times in 123 Posts
    Followup: Proof of concept

    Try submitting this checkbox form unchecked using phpmillion's "technique". This will completely fail

    PHP Code:
    <?php
    var_dump
    ($_POST);

    if (!empty(
    $_POST))
    {
        echo 
    'The check says: POST request';
    }
    ?>
    <form action="" method="post">
     <input name="box1" type="checkbox">
     <input type="submit" value="Submit">
    </form>

    Now try it using the correct method. No failure.

    PHP Code:
    <?php
    var_dump
    ($_POST);

     if (
    $_SERVER['REQUEST_METHOD'] == 'POST')
    {
        echo 
    'The check says: POST request';
    }
    ?>
    <form action="" method="post">
     <input name="box1" type="checkbox">
     <input type="submit" value="Submit">
    </form>
    * EDIT: Removed incorrectly used else
    Last edited by benanamen; 10-14-2017 at 07:38 PM.
    To save time, lets just assume I am almost never wrong.

    The XY Problem
    The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

    "This text has been encoded with ROT26. If you can read this you must have found a backdoor. Congratulations!"

  14. #14
    New Coder
    Join Date
    Sep 2017
    Location
    Lithuania
    Posts
    30
    Thanks
    0
    Thanked 9 Times in 9 Posts
    Wow, I never ever imagined someone would create a form with a single checkbox and nothing else. Sure thing, if you create forms with single checkbox and nothing else, and want the form to be processed every single time (when there's no reason to do that if your form only has single checkbox and this remains empty, so no actual data is submitted), you are welcome to use the "correct" method.

    However, all my code (and I guess code written by other folks here) is designed for real-life and form doesn't need to be processed when no single bit of data was submitted. In other words, all of us can create absurd examples to demonstrate how our method is "correct" and other methods are "total fails", but that's a desperate thing...

  15. #15
    Regular Coder Vege's Avatar
    Join Date
    Jan 2008
    Posts
    825
    Thanks
    21
    Thanked 113 Times in 112 Posts
    Quote Originally Posted by phpmillion View Post
    Wow, I never ever imagined someone would create a form with a single checkbox and nothing else. Sure thing, if you create forms with single checkbox and nothing else, and want the form to be processed every single time (when there's no reason to do that if your form only has single checkbox and this remains empty, so no actual data is submitted), you are welcome to use the "correct" method.

    However, all my code (and I guess code written by other folks here) is designed for real-life and form doesn't need to be processed when no single bit of data was submitted. In other words, all of us can create absurd examples to demonstrate how our method is "correct" and other methods are "total fails", but that's a desperate thing...
    We could argue that handling PHP and HTML on the same page is absurd. We could argue that using $_POST array directly is absurd. We could argue that using empty on anything is absurd. We could argue that using == instead of === is absurd.
    But we don't.
    Don't pick on the people who give you correct advice.
    I have seen plenty of forms where the only form field is checkbox. EULA:s for example.


 
Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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