/    Sign up×
Community /Pin to ProfileBookmark

Code explanation

Possible a stupid question, but is the following code like an if statement? The only parts that confuse me are the ? and : If anyone could explain that I would be grateful. Thank you.

[code=php](!empty($_GET[‘pn’])) ? preg_replace(‘/[^0-9]/’, ”, $_GET[‘pn’]) : null ;[/code]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@tfk11Dec 25.2007 — It's similar yes.

If the expression preceding the question mark evaluates to true then the value of the expression after the question mark is returned else the value of the expression after the colon is returned.

true === (true) ? (true) : (false);

false === (false) ? (true) : (false);
Copy linkTweet thisAlerts:
@EJMAES1973authorDec 25.2007 — Ok, Thank you. Does this have a specific name, or is it just kind of there?
Copy linkTweet thisAlerts:
@ZeroKilledDec 25.2007 — is named as ternary conditional expression, however is commonly named as ternary operator. for more reference http://en.wikipedia.org/wiki/%3F:
Copy linkTweet thisAlerts:
@EJMAES1973authorDec 25.2007 — Alright, thank you for the help.
Copy linkTweet thisAlerts:
@NogDogDec 25.2007 — Also see the manual: http://www.php.net/ternary. (Scroll down 2-3 screens and you'll find the "Ternary Operator" heading.)
×

Success!

Help @EJMAES1973 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...