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
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Can someone explain two snippets of code?

    Hi all.
    Code:
    $testbox = isset($_POST['testbox'])?$_POST['testbox']:null;
    I was looking for answer at php tutorial, but have just waste my time.
    I need to know what the "?" and the ":" are and how they work.
    Greatly appreciated.

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Ternary operator: http://php.net/manual/en/language.op...comparison.php

    Code:
    $val = ((if this expression is true) ? [use this value] : [else use this value]);

  • Users who have thanked MattF for this post:

    angelwww (06-03-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for very quick responding.
    Last edited by angelwww; 06-03-2010 at 09:30 PM.

  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by angelwww View Post
    Perhaps I asked my question incorrectly.

    I understand how the code functions, but I've never seen the "?" or ":" defined or even used before. What's the purpose of the if, else statement if we have "?" and ":" to use.
    Read the link I posted regarding the ternary operator. It's a shorthand method, not always suitable.


  •  

    Posting Permissions

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