JavaScript disabled
While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.
While it will still mostly work, a lot of this site's functionality relies on JavaScript - please enable it for the best experience.
Have you ever seen code using a similar syntax to the following and wondered what it does?
<?php
$id = (isset($_GET['id'])) ? $_GET['id'] : 1;
$name = (isset($_GET['name'])) ? $_GET['name'] : 'Anonymous';
$email = (isset($_GET['email'])) ? $_GET['email'] : 0;
I am referring to the question marks and colons. This syntax is known as the ternary operator. The ternary operator is a shortcut comparison operator that replaces an if-else statement in a PHP script. If you use a lot of if-else statements in your scripts, using the ternary operator can shorten your code hugely. The ternary operator is very simple to use, but it does tend to confuse new PHP programmers.
Basically, the previous code sample is a short way of writing:
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
} else {
$id = 1;
}
if (isset($_GET['name'])) {
$name = $_GET['name'];
} else {
$name = 'anonymous';
}
if (isset($_GET['email'])) {
$email = $_GET['email'];
} else {
$email = 0;
}
As you can see, it is significantly shorter (and often far easier to read). Let’s break it down a bit:
<?php
$varname = (statement) ? truecode : falsecode;
Fairly obvious?
statement - the statement to test. For example, could test a variable to see whether it matches some regex.truecode - the code to execute if the statement returns true.falsecode - the code to execute if the statement returns falseIt is very good for displaying different text to different people, for example:
<?php
echo ($country === 'UK') ? 'Welcome!' : 'This website is UK only, sorry';
In PHP 5.3, the optional adaptation was introduced:
<?php
$notset = $_GET['var'] ?: 'Variable not set';
If $_GET['var'] is set, $notset will be set to that. If it isn't, it will be set to "Variable not set". This is the equivilant of:
<?php
if ($_GET['var']) {
$notset = $_GET['var'];
} else {
$notset = 'Variable not set';
}
<div class="content" id="<?php echo (is_home()) ? 'wrapper' : ''; ?>">
<?php
$vartrue = 'Thanks for reading.';
$varfalse = 'Read it again, or ask in a comment.';
$message = ($understood) ? $vartrue : $varfalse;
echo $message;
Thanks for reading.
# This is an <h1> tag
## This an <h2> tag
###### This is an <h6> tag
Inline markup: _this text is italic_, **this is bold**, and `code()`.
[Link text](link URL "Optional title")
[Google](http://google.com/ "Google!")


1. Ordered list item 1
2. Ordered list item 2
* Unordered list item 1
* Unordered list item 2
* Item 2a
* Item 2b
And some code:
// Code is indented by one tab
echo 'Hello world!';
Horizontal rules are done using four or more hyphens:
----
> This is a blockquote
Inline markup: this text is italic, this is bold, and code().

And some code:
// Code is indented by one tab
echo 'Hello world!';
Horizontal rules are done using four or more hyphens:
This is a blockquote
Comments