How bad is that to have spaces in array keys?

Hello,
is that a problem?


$array['Hello how are you doing'] = 'fine';

and how could I add an apostrophe?


$array['Hello what's up?'] = 'fine';

:slight_smile:

  1. No, thats no problem. But it sure ain’t nice.
  2. Escape it or use double quotes.

$array['Hello what\\'s up?'] = 'fine';
$array["Hello what's up?"] = 'fine';

Why not use an underscore, and if you need to print the keys’ values, just do a string replacement and replace the underscore with spaces.

My pet peeve is when designers use spaces in file names: “my file.html” drives me nuts, so, as you can tell, If I were your manager, I would make you change them…

thanks for your replies :slight_smile:

I see nothing wrong with using spaces for array keys. PHP supports it. Besides, there are lots of times where array keys will contain spaces. It’s very handy for “reverse” types of lookups like:


    $countries['Australia'] = 'au';
  $countries['Bosnia and Herzegovina'] = 'ba';
    $countries['Cook Islands'] = 'ck';
    $countries['New Zealand'] = 'nz';
    
As far as slashes go, just use  
addslashes($var)

on the key regardless. It will add backslashes only if it needs to escape the quotes.

Why not use an underscore, and if you need to print the keys’ values, just do a string replacement and replace the underscore with spaces.

Just beware that if you use a period (.) as a field name in a form etc then PHP automatically converts it to an underscore. So name=“some.value” becomes “some_value” in the $_POST array.

ok thanks :slight_smile: