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
    JAG
    JAG is offline
    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
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,299
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    === 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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    JAG (04-25-2014)

  • #3
    JAG
    JAG is offline
    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
    •