I have a 'problem' and it is the following I am receiving a parameter with a value in my method null
, this value null
comes as if it were a string 'null'
, what I have tried to do is the following script
$data['preg_servicio_asociado'][$i] == "null" ? null :
$data['preg_servicio_asociado'][$i]
my doubt is generated in the following, how to say that it returns $data['preg_servicio_asociado'][$i]
without the need to assign it in the else
, will this be possible?
I appreciate the interest
You could use the Null Fusion Operator (
??
) , but BEWARE, this was added as of PHP 7 , in older versions it doesn't work.For example:
Doing that, if it
$data['preg_servicio_asociado'][$i]
is equal to it"null"
will acquire the valuenull
. I don't see much point in it either, but well... And if not, it will keep its value.For before PHP 7 there is no other way than this, with ternary operators:
I don't understand very well what you mean... but if I haven't understood you very badly, you could do the following:
If this is not what you want, please post more source code and try to explain yourself a bit more.
I hope I've helped.
A slightly crappy way I can think of, although it works, would be to use eval while handling the error to perform a textual assignment of the variable's content. In case of being "null" it will assign the value null while if it is not null, it will simply leave its current string value:
Like I said, it's not elegant, it has potential security holes, and it's not the right way to proceed, but I thought of experimenting with eval and here's the result.