What does this syntax ( page = $page ? $page : 'default' ) in PHP mean?
Asked Answered
C

7

3

I'm new to PHP. I came across this syntax in WordPress. What does the last line of that code do?

$page = $_SERVER['REQUEST_URI'];
$page = str_replace("/","",$page);
$page = str_replace(".php","",$page);
$page = $page ? $page : 'default'
Clerical answered 20/1, 2010 at 8:9 Comment(2)
wow! overwhelming responses.. :) thanksClerical
possible duplicate of Reference - What does this symbol mean in PHP?Hale
E
6

It's an example of the conditional operator in PHP.

It's the shorthand version of:

if (something is true ) {
    Do this
}
else {
    Do that
}

See Using If/Else Ternary Operators http://php.net/manual/en/language.operators.comparison.php.

Emia answered 20/1, 2010 at 8:13 Comment(0)
A
7

That's the ternary operator:

That line translates to

if ($page)
    $page = $page;
else
    $page = 'default';
Averir answered 20/1, 2010 at 8:11 Comment(4)
What do you mean by "the conditional operator". Don't you mean "the ternary operator" (one of the conditional operators)?Chuppah
I wrote "ternary operator" originally. @Biles edited it and changed it to "conditional". Curious.Averir
"the ternary operator" is ambiguous because there can be more than one.Biles
The docs seem to disagree: "Another conditional operator is the "?:" (or ternary) operator."Averir
E
6

It's an example of the conditional operator in PHP.

It's the shorthand version of:

if (something is true ) {
    Do this
}
else {
    Do that
}

See Using If/Else Ternary Operators http://php.net/manual/en/language.operators.comparison.php.

Emia answered 20/1, 2010 at 8:13 Comment(0)
D
3

It's a ternary operation which is not PHP or WordPress specific, it exists in most langauges.

(condition) ? true_case : false_case 

So in this case the value of $page will be "default", when $page is something similar to false — otherwise it will remain unchanged.

Dede answered 20/1, 2010 at 8:13 Comment(0)
A
2

It means that if $page does not have a value (or it is zero), set it to 'default'.

Algonquian answered 20/1, 2010 at 8:11 Comment(0)
I
1

More verbose syntax of the last line is:

if ($page)
{
    $page = $page;
}
else
{
    $page = 'default';
}
Inquisition answered 20/1, 2010 at 8:11 Comment(0)
X
1

It means if the $page variable is not empty then assign the $page variable on the last line that variable or set it to 'default' page name.

It is called conditional operator

Xanthophyll answered 20/1, 2010 at 8:12 Comment(2)
It's misnamed the ternary operator where it really is just a ternary operater. Granted that in most language it is the only ternary operator implemented but that doesn't preclude the creation of other operators that takes 3 arguments. A language could for example have an operator for declaring functions, much like Forth's : operator, that operates on function name, parameter list and code block. That would also be a ternary operator.Boatel
"Conditional operator"? Don't you mean "ternary operator"?Chuppah
G
0

That's the so-called conditional operator. It functions like an if-else statement, so

$page = $page ? $page : 'default';

does the same as

if($page)
{
    $page = $page;
}
else
{
    $page = 'default';
}
Georgettegeorgi answered 20/1, 2010 at 8:13 Comment(1)
The reference says it is the ternary operator (or a ternary operator?), one of the conditional operators. Can you elaborate?Chuppah

© 2022 - 2024 — McMap. All rights reserved.