Can php variables have spaces?
I was messing around with variable variables in PHP, so I came up with the code: Show
I was just trying to understand how PHP will behave if we have a string with spaces, and try to make a variable variable from it. And it seems it still stores the variable with spaces, since I did
I can access the 'two words' variable through
alexwlchan 5,2325 gold badges35 silver badges46 bronze badges asked Apr 22, 2015 at 8:56
6
The issue with this is that the string interpolation rules will stop at the first character that's not valid in a variable name. It is not specific to variable variables as such, it's specific to string interpolation. This'll do:
But since this is rather awkward and doesn't work in all the same situations as valid variable names do (e.g. string interpolation), you really shouldn't do this ever. answered Apr 22, 2015 at 9:04
deceze♦deceze 497k81 gold badges719 silver badges867 bronze badges I just found out a very strange PHP feature by browsing the PHP-GTK2 API with the Reflection classes : spaces can be used in some alien places, like variable names. Well, try this in PHP 5.2.5, for instance: The case with constants
is better known, being mentioned on php.net : Stranger is the fact that you are not allowed to use that same syntax on parameter names, class constants, or class properties: in every case, the syntax fails. So how does this related to PHP-GTK2 ? Actually, I discovered this possibility about spaces in variable names because the Reflection API shows There's a bit
of magic in ext/gtk+/gdk.overrides regarding these parameters (starting line 312 in version 1.81), because this is not the default Gdk format. However, at any rate, it seems that this syntax is not allowable from PHP source, even though the PHP-GTK extension uses it. Are spaces allowed in variables?Variable names cannot contain spaces.
Can blank spaces be used in variable names?Variable names can never contain spaces. The underscore character ( _ ) can also appear in a name. It is often used in names with multiple words, such as my_name or price_of_tea_in_china .
Is space a character in PHP?The ctype_space() function in PHP check for whitespace character(s). It returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.
Can PHP variables have dashes?Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive. You can use dashes in folder names, of course.
|