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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2007
    Posts
    269
    Thanks
    28
    Thanked 0 Times in 0 Posts

    what is question mark

    what is question mark ? (what is its task ?)

    would you give me a sample


    PHP Code:
    $link $this->functions[$usepconnect 'pconnect' 'connect']("$servername:$port"$username$password); 

  • #2
    Regular Coder mlse's Avatar
    Join Date
    Mar 2005
    Posts
    624
    Thanks
    20
    Thanked 19 Times in 18 Posts
    Hi,

    The example you have there can be re-written as follows:

    PHP Code:
    if ($usepconnect)
    {
      
    $index "pconnect";
    }
    else
    {
      
    $index "connect";
    }

    $link $this->functions[$index]("$servername:$port"$username$password); 
    That's all there is to it! Very simple It is just quicker to use the (condition) ? (true) : (false) notation.
    Last edited by mlse; 01-04-2008 at 04:29 PM.

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    It's a ternary operator (meaning it involves 3 operands).

    It's a simplified if statement.

    PHP Code:
    $result = isset($variable) ? $variable 'default'
    Can also be written as.

    PHP Code:
    if (isset($variable)) {
        
    $result $variable;
    } else {
        
    $result 'default';

    You can see it saves some typing, and quite a few common languages have this feature so it's okay to use when helpful (since it's not language specific).

    One of the benefits is that you can put the ternary operator inline.

    PHP Code:
    echo 'Your cart contains ' $total ' item' . ($total == '' 's'); 

  • #4
    Regular Coder anarchy3200's Avatar
    Join Date
    Mar 2003
    Location
    England
    Posts
    261
    Thanks
    0
    Thanked 1 Time in 1 Post
    Very slow today! Just repeat of the above...
    Last edited by anarchy3200; 01-04-2008 at 04:27 PM. Reason: Removed, beaten to it.
    Mike


  •  

    Posting Permissions

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