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 5 of 5

Thread: ?: operator

  1. #1
    New Coder
    Join Date
    Nov 2011
    Posts
    97
    Thanks
    2
    Thanked 0 Times in 0 Posts

    ?: operator

    Hello, does this code mean something like default $u is equal to the parameter get 'user', if it doesn't get 'user' try if the get parameter was userid, username, uid and u right?

    PHP Code:
    $u $_GET['user'];
    $u $u $u $_GET['userid'];
    $u $u $u $_GET['username'];
    $u $u $u $_GET['uid'];
    $u $u $u $_GET['u']; 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    That's correct. ?: is the ternary operation, it is equivalent to:
    PHP Code:
    $u $_GET['user'];
    $u $u $u $_GET['userid'];
    $u $u $u $_GET['username'];
    $u $u $u $_GET['uid'];
    $u $u $u $_GET['u'];

    // is the same as
    $u $_GET['user'];
    if (
    $u == null)
    {
        
    $u $_GET['userid'];
    }
    if (
    $u == null)
    {
        
    $u $_GET['username'];
    }
    if (
    $u == null)
    {
        
    $u $_GET['uid'];
    }
    if (
    $u == null)
    {
        
    $u $_GET['u'];

    But, this is not the correct approach to it. Since any and all of these may not exist, its best to check an isset boundary on them. The above will trigger notices on underfined offsets if you check the error logs or have error reporting enabled.
    Since you have a priority, I'd simply pull the values of what is available and choose in that priority. Something simple like this:
    PHP Code:
    $aPickFrom = array('user''userid''username''uid''u');
    $aUsableKeys array_keys($_GET);

    $aAvailable array_intersect($aUsableKeys$aPickFrom);
    if (isset(
    $aAvailable[0]))
    {
        
    $u $_GET[$aAvailable[0]];
    }

    print 
    $u

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,294
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    The format is called a ternary.

    Basically

    $Something = ($If_something_is = true)? $DefaultValue: $ElseValue;

    Edit:
    Beaten to it by Fou
    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!

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I win!
    Although my comparison's are actually wrong. The more accurate would be:
    PHP Code:
    if ($u != false)
    {
        
    $u $u;
    }
    else
    {
        
    $u 'the next one';

    but I was too lazy to type all that.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,294
    Thanks
    57
    Thanked 523 Times in 510 Posts
    Blog Entries
    5
    Quote Originally Posted by Fou-Lu View Post
    I win!
    Bah.. next time Fou, next time
    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!


  •  

    Posting Permissions

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