Alternative to assign empty value if the parameter not exist in php Alternative to assign empty value if the parameter not exist in php json json

Alternative to assign empty value if the parameter not exist in php


You face problems with PHP 5, because the null coalescing operator was introduced in PHP 7.

An equivalent to that would be using a conditional operator along with isset.

$centre_jurisdiction = isset($data['data']['ctj']) ? $data['data']['ctj'] : "";$state_jurisdiction = isset($data['data']['stj']) ? $data['data']['stj'] : "";


The null coalescing operator "??" is just syntactic sugar that was added in PHP 7; see the Manual. You can also think of it as the "isset ternary" operator; see the RFC.

So, the good news is that that one may rewrite the code as follows for PHP 5:

$centre_jurisdiction = isset($data['data']['ctj'])? $data['data']['ctj'] : "";$state_jurisdiction = isset($data['data']['stj'])?  $data['data']['stj'] : "";

Note: the logic is the same but the syntax is shorter and thus sweeter in PHP 7. The PHP 5 code simply makes everything explicit.