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'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
jest
  • 699
  • 2
  • 12
  • 28
  • wow! overwhelming responses.. :) thanks – jest Jan 20 '10 at 08:37
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – outis Apr 01 '12 at 05:54

7 Answers7

7

That's the ternary operator:

That line translates to

if ($page)
    $page = $page;
else
    $page = 'default';
echo
  • 7,737
  • 2
  • 35
  • 33
  • 1
    What do you mean by "the conditional operator". Don't you mean "the ternary operator" (one of the conditional operator***s***)? – Peter Mortensen Dec 14 '15 at 18:22
  • I wrote "ternary operator" originally. @Ether edited it and changed it to "conditional". Curious. – echo Dec 22 '15 at 15:49
  • "the ternary operator" is ambiguous because there can be more than one. – Ether Dec 23 '15 at 18:33
  • The docs seem to disagree: "Another conditional operator is the "?:" (or ternary) operator." – echo Dec 23 '15 at 21:49
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Cups
  • 6,901
  • 3
  • 26
  • 30
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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nocksock
  • 5,369
  • 6
  • 38
  • 63
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
wallyk
  • 56,922
  • 16
  • 83
  • 148
1

More verbose syntax of the last line is:

if ($page)
{
    $page = $page;
}
else
{
    $page = 'default';
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karsten
  • 14,572
  • 5
  • 30
  • 35
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

Ether
  • 53,118
  • 13
  • 86
  • 159
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • 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. – slebetman Jan 20 '10 at 08:46
  • "Conditional operator"? Don't you mean "ternary operator"? – Peter Mortensen Dec 14 '15 at 18:26
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';
}
Ether
  • 53,118
  • 13
  • 86
  • 159
Douwe Maan
  • 6,888
  • 2
  • 34
  • 35