www.webdeveloper.com
Results 1 to 4 of 4

Thread: syntax explanation, please?

  1. #1
    Join Date
    Sep 2008
    Posts
    61

    Question syntax explanation, please?

    Hey!

    I'm making a tiny application, and I'm using post-data. And the PHP 5.3 throws warnings if i don't check if a $_POST variable is set before trying eg. $var = $_POST['somevariable'];

    And what I wanted to do is making as small code as possible, but still check if the post-var is set or not, and if one of the post-vars is not set, it should set a $error variable to true.

    So I wrote the code like this.

    PHP Code:
    $error false;

    $userid = isset($_POST['userid']) ? $_POST['userid'] : $error true 43;

    if(
    $error == true) echo "error\n"
    What happens is that, if $_POST['userid'] is set then $userid should get it's value. But if its not, $userid should get the value 43, and the $error variable should be set to true. And it sure works. But I don't really get what happens there. I just tried the $error = true | 43 by a coincidence, and it works, but I'm not sure if it's really the right way of doing it? Is this really correct syntax?

    And if it is good syntax... Is it any way of making this even shorter? for example so I don't have to write $_POST['userid'] two times? So I could replace it with $1 or something like that.

    Cheers,
    Artheus

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    The single pipe "|" is the bitwise "or" operator. So true is cast to an integer 1 ("1" in binary), while 43 in binary is "101011". So when those two values are used with the "|" operator, "Bits that are set in either $a or $b are set." (See PHP Bitwise Operators), the result is still binary "101011" which is 43. If you instead use 42 for the "default", you would find that $userid is still set to 43 because of that ones digit being set by the "true".

    Oh, and the if is evaluating to true because you use the "==" comparison operator, which allows type-casting, so the 43 is considered "true" since it is non-zero. If you used the "===" is-identical-to operator, then it would fail since integer 43 is not the same type/value as a boolean true.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2008
    Posts
    61
    okay. So what the code is really saying is:

    "set $userid to $_POST['userid'], if $_POST['userid'] is set, else set $userid to (if $error can be set to true) combined with 43 bitwise"

    so 1 | 43 is bitwise 1 combined with 101011 which is 101011
    and if I would put 42 it'd be 101010 combined with 1 which is also 101011

    Is this the correct way to interpret it?

    Cheers,
    Artheus

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,148
    Quote Originally Posted by artheus View Post
    okay. So what the code is really saying is:

    "set $userid to $_POST['userid'], if $_POST['userid'] is set, else set $userid to (if $error can be set to true) combined with 43 bitwise"

    so 1 | 43 is bitwise 1 combined with 101011 which is 101011
    and if I would put 42 it'd be 101010 combined with 1 which is also 101011

    Is this the correct way to interpret it?

    Cheers,
    Artheus
    Yes, I think you understood it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles