Can php variables have spaces?

I was messing around with variable variables in PHP, so I came up with the code:

$a = 'two words';  
$$a = 'something';  
echo $$a;  // outputs something
echo "$two words"; // error since $two doesn't exist

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 var_dump($GLOBALS); and I have this:

'a' => string 'two words' (length=9)  
'two words' => string 'something' (length=9) 

I can access the 'two words' variable through $GLOBALS['two words'] where two questions arise:

  1. Can I somehow access it directly with the $? I've read somewhere that you need to get the whole variable in curly brackets ({$two words} or I assume ${two words}), but that didn't work.
  2. Can you actually have variables with spaces in PHP? I tried making an associative array with keys that contain spaces and that worked:

    $a['a space'] = 1;
    echo $a['a space']; // 1
    

alexwlchan

5,2325 gold badges35 silver badges46 bronze badges

asked Apr 22, 2015 at 8:56

6

echo "$two words"; // error since $two doesn't exist

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:

echo ${'two words'};

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

decezedeceze

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.

But the doc says you can't !

Well, try this in PHP 5.2.5, for instance:

Spaces in PHP variable names

// Store
${'some var'} = 42;// Use
echo ${'some var'}; // 42

// List

$arVars = get_defined_vars();
print_r($arVars); // yes, it is there !
?>

Spaces in PHP constant names

The case with constants is better known, being mentioned on php.net :

// Store
define('SOME CONST', 42);// Use
echo constant('SOME CONST'); //
// http://www.php.net/manual/en/language.constants.php#76542

// List

$arConst = get_defined_constants();
print_r($arConst); // yes, it is there !
?>

Spaces in other PHP names

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.

Spaces in PHP-GTK names

So how does this related to PHP-GTK2 ? Actually, I discovered this possibility about spaces in variable names because the Reflection API shows GdkColorMap::alloc_color() like this:

Method [ public method alloc_color ] {

  - Parameters [3] {
    Parameter #0 [ $color OR red ]
    Parameter #1 [ $blue ]
    Parameter #2 [ $green ]
  }
}

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.