www.webdeveloper.com
Results 1 to 10 of 10

Thread: What does this line do?

  1. #1
    Join Date
    Nov 2013
    Posts
    5

    What does this line do?

    Hi

    I'm fairly new to PHP and trying to understand what a script is doing.

    This line has me puzzled:

    Code:
    $img=preg_match('/\d/',$selectedImage)?$selectedImage:'none';
    I understand that the first part is looking for numeric characters in the variable, but what does the ? signify.

    Thanks,

    Mike

  2. #2
    Join Date
    Nov 2013
    Location
    London
    Posts
    1
    Can you give me the previous row code?

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,322
    The ? and : together form the ternary operator.
    "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

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    If the value of $selectedImage contains a number (\d) then it will return the value of $selectedImage, if not then it will return the string "none".
    The return result of the expression will be assigned to the variable $img for use elsewhere.
    JavaScript: Learn | Validate | Compact | bionoid

  5. #5
    Join Date
    Sep 2013
    Posts
    221
    ? signify that it will match the preceding pattern zero or one times.
    Well.., u can refer below link to clear more doubts abt regex.
    http://www.noupe.com/php/php-regular-expressions.html

    Hope this helps.
    strad solutionswww.stradsolutions.com

  6. #6
    Join Date
    Nov 2013
    Posts
    5
    Quote Originally Posted by ispanico82 View Post
    Can you give me the previous row code?
    Yes, it's:

    Code:
    $selectedImage=$_GET['i'];
    Mike

  7. #7
    Join Date
    Nov 2013
    Posts
    5
    Quote Originally Posted by bionoid View Post
    If the value of $selectedImage contains a number (\d) then it will return the value of $selectedImage, if not then it will return the string "none".
    The return result of the expression will be assigned to the variable $img for use elsewhere.
    So it's like an IF expression?

    Mike

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by PTO View Post
    So it's like an IF expression?
    It's more like a shorthand IF ELSE

    (expression) ? (expression was true) : (expression was false);
    JavaScript: Learn | Validate | Compact | bionoid

  9. #9
    Join Date
    Nov 2013
    Posts
    5
    Quote Originally Posted by priyankagound View Post
    ? signify that it will match the preceding pattern zero or one times.
    Well.., u can refer below link to clear more doubts abt regex.
    http://www.noupe.com/php/php-regular-expressions.html

    Hope this helps.
    Great resource, thank you.

    Mike

  10. #10
    Join Date
    Nov 2013
    Posts
    5
    Quote Originally Posted by bionoid View Post
    It's more like a shorthand IF ELSE

    (expression) ? (expression was true) : (expression was false);
    Gotcha, thanks

    Mike

Thread Information

Users Browsing this Thread

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

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