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 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Question Checking Request Method

    In the following code, what is the importance of using === instead of == ? I have tested the code using both and it works, but everyone recommends using ===, why is that?

    Code:
    <?php
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    	if (isset($_POST['action']) and $_POST['action'] == "Submit") {
                // Do something.
    	}
    	else {
                // Do something.
    	}
    }
    ?>
    Last edited by JAG; 04-25-2014 at 09:56 PM.

  2. #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,826
    Thanks
    79
    Thanked 575 Times in 562 Posts
    === is for checking if a value / result is equivalent to another value / result.

    EG 0 is an integer but it is also the same as false and thus if you write a function that can return both 0 and false you would use === to check it.

    For your case, $_SERVER['REQUEST_METHOD'] will be a string so the 3rd equals sign is not needed because you are comparing two strings and thus only need == instead.
    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.

  3. Users who have thanked tangoforce for this post:

    JAG (04-25-2014)

  4. #3
    New Coder
    Join Date
    Feb 2011
    Posts
    63
    Thanks
    24
    Thanked 0 Times in 0 Posts

    Thumbs up

    Quote Originally Posted by tangoforce View Post
    === is for checking if a value / result is equivalent to another value / result.

    EG 0 is an integer but it is also the same as false and thus if you write a function that can return both 0 and false you would use === to check it.

    For your case, $_SERVER['REQUEST_METHOD'] will be a string so the 3rd equals sign is not needed because you are comparing two strings and thus only need == instead.
    Perfect answer. Thanks for enlightening me.


 

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
  •