Ternary operator examples in PHP

Sometimes the ternary operator is efficient, other times it obfuscates code

The ‘inline if’ (iif) ?: also known as ternary operator is a potentially time-saving syntax for conditional expressions. Dating back to Christopher Stracheys CPL ‘Combined Programming Language’ (otherwise known as ‘Cambridge plus London’ programming language) which first appeared in 1963.

A conditional expression allows for conditional execution of code. We can write programmes which run different operations depending on whether a statement returns true or false. A basic example of this is the ‘if’ and ‘else’ constructs which are available in the PHP language.

if(0 === 0) { print 'Success'; } else { print 'Failure'; }

The above conditional statement uses the binary ‘Identical’ === comparison operator, which is provided by the PHP language. Most programming languages provide several useful comparison operators which allow us to compare values, and to perform different routines depending upon the boolean result of an expression. Usually we use binary comparison operators (which compose of 2 parts) e.g. 1 == 1, composes of the two integer values, either side of the ‘Equal’ comparison operator.

However some programming languages provide support for a ternary comparison operator (composed of 3 parts)

e.g. $a = 0 === 0 ? true : false;

The syntax given above is the ‘inline / ternary if’ statement.

*Since php 5.3 omission of the second element is permitted*

e.g. $a = 0 === 0 ?: false; returns `true’

## Examples (in PHP)

*1. Checking if the HTTP request method is ‘PUT’:*

Standard ‘if else’ example:
if($_POST['REQUEST_METHOD'] == 'PUT') { return true; } else { return false; }

Ternary comparison operator version:

$request = $_POST['REQUEST_METHOD'] == 'PUT' ? true : false;

*2. Checking if ‘user’ session key is empty:*

Standard ‘if else’ example:

if(empty($_SESSION['user'])) { return $_SESSION['user']; } else { return false; }

Ternary comparison operator version:

$user = empty($_SESSION['user']) ? $_SESSION['user'] : false;

*3. Checking value assigned to $level variable:*

Standard ‘switch statement’ example:

switch ($level) { case 2: return 'public'; break; case 1: return 'member'; break; default: return 'admin'; break; }

Ternary comparison operator version:

$group = $level == 0 ? 'admin' : $level == 1 ? 'member' : $level == 2 ? 'public' : false;

Solution for ‘sudo subl’ – “cannot find sublime” error… with Sublime Text 3 on OSX 10.11.5

If you use Sublime Text 3 as your editor on OSX (like myself) and you have created a symbolic link to launch sublime from your terminal, but you have noticed that when attempting to launch Sublime Text 3 with super user privileges by typing sudo subl file_name_here and your terminal outputs ‘Unable to find Sublime Text’…

The problem seems to be that the symbolic link ‘subl’ script is attempting to look for Sublime Text at the following path /Applications/Sublime\ Text\ 3.app… However when you download & install Sublime Text 3 on OSX into the usual /Applications/ directory, the path to┬áSublime Text 3 will just be as follows /Applications/Sublime\ Text.app… This is why you are seeing the ‘Unable to find Sublime Text’ message when running sudo subl.

In order to fix this, you must firstly delete the symbolic link you have created at ‘usr/local/bin/subl’. To do this run the following command in your terminal:

cd /usr/local/bin; rm subl;

Next rename┬áSublime Text 3 to ‘Sublime Text 3’ in your Applications directory. (It will probably be named ‘Sublime Text’ at the moment.) Do this by running the following command:

mv /Applications/Sublime\ Text.app /Applications/Sublime\ Text\ 3.app;

Next you can follow the normal procedure of creating the symbolic link to the script provided by Sublime Text 3, by running the following command in your terminal:

ln -s /Applications/Sublime\ Text\ 3.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

Hope this is clear and that it helps!