I have seen a lot of people being used to do this in their code:
$value = isset($text) ? $text : "";
This code essentially means set $value
to $number
(if it is set) else set $value
to ""
I experimented a bit and came up with a much cleaner syntax:
$value = "".$text;
Apart from when $text
is undefined
, this code covers all the cases and works the same way.
So, my questions are:
- Is there a better, shorter, cleaner way to do
$value = isset($text) ? $text : "";
- Am I wrong to assume my approach works the same way?(apart from
isset
case) - Can my approach be modified to address the
isset
case as well ?