Do you have used Elvis Operator in PHP?

I am not a good PHP programmer, I use the language to work on projects that are not product base. And usually I work on the project myself. Because I do it by myself, I code it according to what I learned myself.

I am not know what the version of PHP before, is your code working in PHP version 5.6 or 7+ version?

I am not aware thats. But now, i need to know that deeply because we create product base mean customer will not break our product. I now always care about other programmer write their code.

Then I worked with many programmers. Then look at some strange operator code in my opinion, I have never used that operator. Then I tried to search on google and found it was Elvis Operator.

Wikipedia said in this link [https://en.wikipedia.org/wiki/Elvis_operator]

Yep, Elvis operator is part of binary operator. Its already implemented in many programming languages. Elvis operator also implemented in PHP since 5.3 version.

[https://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary]

Today we learn how elvis operator help us in PHP programming. Let start, i have example code like this:

<?php

$bar = true;
$baz = false;

if ($bar) {
    $foo = $bar;
} else {
    $foo = $baz;
}

echo $foo . PHP_EOL; // result is 1

As you see above, we have $bar variable with value true boolean. Then we will assign $bar value into $foo variable right if $bar value is true.

We can make that code more effective like this code:

<?php

$bar = true;
$baz = false;

$foo = $bar ? $bar : $baz;

echo $foo . PHP_EOL;

With same value, you can write like above code. Nice right?

But, thats not Elvis Operator. We can use that case with elvis operator like this code:

<?php

$bar = true;
$baz = false;

$foo = $bar ?: $baz;

echo $foo . PHP_EOL;

It basic that we work with 3 variable; $bar, $baz and $foo.

I have another example with 2 only variable. This code like this:

<?php
    var_dump(5 ?: 0); // 5
    var_dump(false ?: 0); // 0
    var_dump(null ?: 'foo'); // 'foo'
    var_dump(true ?: 123); // true
    var_dump('rock' ?: 'roll'); // 'rock'

Mean if we use 2 variable, first variable will check self, if first variable have true condition, it will return first variable or assign into first variable. If not have true condition, it will return second variable or it will assign into second variable.

Let say, we have code like this.

<?php

// this variable is sets from other function with function base64_decode.
$string = "2451231sdgsdgwewet42623tsdg35235sdgewtywetsdgweweweweye=";

$parseFromOtherSource = base64_decode($string);

list($code, $name) = explode("_", $parseFromOtherSource);

$code = $code ?: "";

$name = $name ?: "";

// use $code and $name in others flow

That code above is for set default of $code and $name variable to be empty string if $code and $name not set in base64_encode from other function. So its not break our flow.

Hopely this article will make you to be good PHP Programmer.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.