loyx Posted March 24, 2009 Share Posted March 24, 2009 I need to use spaces in the name of an array (something along the lines of $_POST[name with spaces in it]) so I can preserve I am trying to make a php mailer for. I read that using braces like this would work: ${'_POST[name with spaces in it]'} however when I do this it doesn't seem to output anything. Does anyone know how to do this? Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/ Share on other sites More sharing options...
trq Posted March 24, 2009 Share Posted March 24, 2009 Theres no special trick to it.... $a = array(); $a['this is foo'] = 'hello'; echo $a['this is foo']; Works fine. You need to explain a little more about what your doing. Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-792302 Share on other sites More sharing options...
loyx Posted March 24, 2009 Author Share Posted March 24, 2009 I'm doing it like this: $report= "First name: $_POST['First Name'] Last name: $_POST['Last Name'] ...etc " I tried your tip but it gives me an error when I used it (tried it before). Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-792525 Share on other sites More sharing options...
wildteen88 Posted March 24, 2009 Share Posted March 24, 2009 If you're retrieving values from a form and you have spaces in your field names. Your browser will convert the spaces to underscores, so you should use $_POST['first_name'] Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-792531 Share on other sites More sharing options...
thebadbad Posted March 24, 2009 Share Posted March 24, 2009 And if you want to use array values like that inside double quotes, encase them in curly brackets: <?php $report= "First name: {$_POST['First Name']} Last name: {$_POST['Last Name']} ...etc " ?> Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-792570 Share on other sites More sharing options...
loyx Posted March 25, 2009 Author Share Posted March 25, 2009 Thanks, both solutions worked just fine . Just one more question. There is a # in the name (something like $_POST[Just Like_# This]) and when I try to use it it spits out the following error Parse error: syntax error, unexpected T_LNUMBER, expecting ']' This error pops up the moment I include a # in the name. Does anyone know how to get php to cooperate with that symbol? Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-793301 Share on other sites More sharing options...
thebadbad Posted March 25, 2009 Share Posted March 25, 2009 With that error it seems you're forgetting to enclose the string in quotes. $_POST['Just Like_# This'] If that value is empty when you don't expect it to be, have a look at the names of the actual array keys, since certain characters are converted, like wildteen88 pointed out: <?php echo '<pre>', print_r($_POST, true), '</pre>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-793439 Share on other sites More sharing options...
loyx Posted March 25, 2009 Author Share Posted March 25, 2009 It's working great now and I learned some great tips, thanks for the help everyone Quote Link to comment https://forums.phpfreaks.com/topic/150811-solved-spaces-in-array-name/#findComment-793845 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.