I have a variable that sometimes doesn't exist that I want to use inside of a string (or heredocs).
My code is like this:
if(someExternalThings()) {
$variable = 'value';
}
echo "I have a {$variable}";
The code works fine when someExternalThings()
is true, but if it doesn't, PHP throws an error, as expected.
Since I know that variable won't break anything (other than inside of the strings) I decided to add @
before it.
echo "I have a {@$variable}";
But it outputs: I have a {@}
and throws an error.
I fixed this by doing this:
$variable = @$variable;
echo "I have a {@$variable}"
//outputs: I have a {@} and throws no errors
Or by putting the entire echo
inside of the if that checks someExternalThings()
if(someExternalThings()) {
$variable = 'value';
echo "I have a {$variable}";
}
This kinda confuses me since we can use these...
echo "{$foo->bar[0]}";
echo "{${foo::bar}}";
...and it just works fine.