New Website

I've made a new website, as lynx.io is dead. You can find it here: http://macr.ae/—it's similar in format to lynx.io, but has better articles, and they're all written by me.

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.

The If Shortcut: The Ternary Operator

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?

What can this be used for?

It is very good for displaying different text to different people, for example:

<?php

echo ($country === 'UK') ? 'Welcome!' : 'This website is UK only, sorry';

 

Optional adaptation

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';
}

A couple examples

<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.

About Callum Macrae:

Callum Macrae is the founder of lynx.io and a JavaScript developer from the United Kingdom. He is currently writing his first book, to be published by O'Reilly Media.

You can view more articles by this author here.

Tags: php shortcuts

Comments

No comments; leave one below.

says:

Add comment

 

You can use markdown in comments (press "m" for a cheatsheet).

Enable JavaScript to post a comment

Markdown Cheat Sheet

# 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!")

![Alt text](image URL)

![This is a fish](images/fish.jpg)

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

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 Google

This is a fish

  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

Toggle MarkDown / HTML (t), full reference or close this