DEV Community

loading...
Cover image for Fast assignation of a php value

Fast assignation of a php value

jorgecc profile image Jorge Castro Originally published at southprojects.com ・2 min read

Let's say the next exercise.

$id=$_GET['id'];
Enter fullscreen mode Exit fullscreen mode

The code will work if we open the website and we send a value /example.php?id=20

But what if the argument id is missing.

Notice: Undefined index: id in example.php on line 3

It is because we are trying to read an index of the array that it does not exist.

What we could do?

Validating if the information exists.

if(isset($_GET['id'])) {
    $id = $_GET['id'];
} else {
    $id=null;
}
Enter fullscreen mode Exit fullscreen mode

While it works but it is verbose. What if we need to validate +30 arguments?

Ternary operator

We could use a ternary operator

$id=isset($_GET['id']) ? $_GET['id'] : null;
Enter fullscreen mode Exit fullscreen mode

Null coalesce

If we use PHP>=7.0 then we could use the operator "??". Its short than the ternary operator but it requires PHP over 7.0, so if you are targeting old PHP, then you can't use it.

$id=$_GET['id'] ?? null;
Enter fullscreen mode Exit fullscreen mode

Note, both ternary operator and null coalesce have the same performance.

https://github.com/EFTEC/php-benchmarks/blob/master/benchmark_isset_vs_at.php

@ symbol

The symbol @ is used to ignores errors. So, if the value does not exist, then it assigns the default value null.

$id=@$_GET['id'];
Enter fullscreen mode Exit fullscreen mode

However, it impacts the performance considerably (if you are doing a high demand site)

Another alternative

In all the other exercises, we are doing the next operation, if the index exists in the array, then we return the value in the index.. If not, then we return null. In any case, we assign a value to $id But what if we don't want to assign the value (if has not set). We could do the next code

if(isset($_GET['id'])) {
    $id = $_GET['id'];
} // if not, then $id could be indefined.
Enter fullscreen mode Exit fullscreen mode

But again, it is verbose.

We could do the next operation

Boolean operator

isset($_GET['id']) and $id=$_GET['id'];
Enter fullscreen mode Exit fullscreen mode

What? why?

First, it is insanely fast. Take note that isset() is not a function but a language construction.

But how it works?

Lets say this code

$a=true;
$b=false;
if($a && $b) {
    echo "both are true";
}
Enter fullscreen mode Exit fullscreen mode

The if is evaluated as follow. If the first expression ($a) is true, then it evaluates the second expression $b. If $a is false, the $b is never evaluated, because the result of the whole expression is already false, no matter the value of $b.

Also, in PHP, we could use the operator && or "and", also we could use || or "or". Commonly we use the symbols && and || in a conditional branch and "and"/"or" in code.

Discussion (2)

Collapse
moopet profile image
Ben Sinclair • Edited
  isset($_GET['id']) and $id = $_GET['id'];

First, it is insanely fast.

Just in case, I profiled these over ~60 seconds. The short-circuit evaluation took marginally longer though that's such a small difference it's within the margins of my test.

Even if it wasn't taking exactly the same time as

if (isset($_GET['id'])) {
  $id = $_GET['id'];
}

then it'd be worse because it's (subjectively to me) harder to read and maintain. Since you can only really get away with it for very short conditions without becoming difficult to parse, you'll end up mixing and matching different approaches, which makes the code messy.

Your best option, in PHP > 7 is to use the null coalescing way:

  $id = $_GET['id'] ?? $sensible_default ?? null;

This way $id is always defined - relying on isset later on is a bit of a code smell to me. That's a second point against any of these options, unless you remember to pre-define $id = null; earlier in the function.

The worst approach is to suppress warnings with @. That's really something you shouldn't do, because it makes debugging harder. You don't get to see what caused the error anymore!

Collapse
darkwiiplayer profile image
DarkWiiPlayer

Not a PHP person, but couldn't you use the ternary operator like that and write isset($_GET['id']) ? $id=$_GET['id']? Or is that illegal syntax?

Forem Open with the Forem app