I have a PHP Object with an attribute having a dollar ($) sign in it.
How do I access the content of this attribute ?
Example :
echo $object->variable; // Ok
echo $object->variable$WithDollar; // Syntax error :-(
$ is the way to refer to variables in PHP. Variables in PHP are dynamically typed, which means that their type is determined by what's assigned to them. Here's the page about variables from the PHP manual. $a = "This is a string"; $b = 1; // This is an int.
The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL.
What does $$ (dollar dollar or double dollar) means in PHP ? The $x (single dollar) is the normal variable with the name x that stores any value like string, integer, float, etc. The $$x (double dollar) is a reference variable that stores the value which can be accessed by using the $ symbol before the $x value.
It makes PHP suppress any error messages (notice, warning, fatal, etc) generated by the associated expression. It works just like a unary operator, for example, it has a precedence and associativity.
With variable variables:
$myVar = 'variable$WithDollar';
echo $object->$myVar;
With curly brackets:
echo $object->{'variable$WithDollar'};
Thanks to your answers, I just found out how I can do that the way I intended :
echo $object->{'variable$WithDollar'}; // works !
I was pretty sure I tried every combination possible before.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With