/    Sign up×
Community /Pin to ProfileBookmark

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’;[/CODE]

I understand that the first part is looking for numeric characters in the variable, but what does the ? signify.

Thanks,

Mike

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@ispanico82Nov 08.2013 — Can you give me the previous row code?
Copy linkTweet thisAlerts:
@NogDogNov 08.2013 — The ? and : together form the [url=www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary]ternary operator[/url].
Copy linkTweet thisAlerts:
@bionoidNov 08.2013 — 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.
Copy linkTweet thisAlerts:
@priyankagoundNov 09.2013 — ? 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.
Copy linkTweet thisAlerts:
@PTOauthorNov 09.2013 — Can you give me the previous row code?[/QUOTE]

Yes, it's:

[CODE]$selectedImage=$_GET['i'];[/CODE]

Mike
Copy linkTweet thisAlerts:
@PTOauthorNov 09.2013 — 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.[/QUOTE]


So it's like an IF expression?

Mike
Copy linkTweet thisAlerts:
@bionoidNov 09.2013 — So it's like an IF expression?[/QUOTE]

It's more like a shorthand IF ELSE ?

(expression) ? (expression was true) : (expression was false);
Copy linkTweet thisAlerts:
@PTOauthorNov 09.2013 — ? 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.[/QUOTE]


Great resource, thank you.

Mike
Copy linkTweet thisAlerts:
@PTOauthorNov 09.2013 — It's more like a shorthand IF ELSE ?

(expression) ? (expression was true) : (expression was false);[/QUOTE]


Gotcha, thanks ?

Mike
×

Success!

Help @PTO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...