Variables in array.

nem

Verified User
Joined
Apr 9, 2005
Messages
49
How can i put variables in this array? I tried all kinds of ways... Still doesnt work.

PHP:
$sock->query('/CMD_EMAIL_POP',
    array(
        'action' => 'modify',
        'domain' => 'somedomain.com',
        'user' => 'usertomodify',
        'passwd' => '', //no password, just doing quota
        'passwd2' => '',
        'quota' => 'xx'
    ));

I tried:

PHP:
$domain = "http://www.test.com";

$sock->query('/CMD_EMAIL_POP',
    array(
        'action' => 'modify',
        'domain' => '$domain',
        'user' => 'usertomodify',
        'passwd' => '', //no password, just doing quota
        'passwd2' => '',
        'quota' => 'xx'
    ));


PHP:
$domain = "http://www.test.com";

$sock->query('/CMD_EMAIL_POP',
    array(
        'action' => 'modify',
        'domain' => '".$domain."',
        'user' => 'usertomodify',
        'passwd' => '', //no password, just doing quota
        'passwd2' => '',
        'quota' => 'xx'
    ));


etc... All of them just print: $domain
 
Lose the ticks/single quotes or replace them with (double)quotes that are currently around your variables.


Good:
$variable;
"$variable";

Bad:
'$variable';
 
Last edited:
I give it a go. Will let you know in next reply.

Also thanks for your post/suggestion.
 
jmstacey said:
Lose the ticks/single quotes or replace them with (double)quotes that are currently around your variables.


Good:
$variable;
"$variable";

Bad:
'$variable';

Always put PHP variables outside of quotes, newer versions wont parse strings for variables anymore, and there are noumerous examples that clearify that the use of variables inside of quotes is bad. If you have 2 variables, $compu and $computer, and you'd like to display $compu-ter, with quotes it'd become something like 'echo "$computer";', and that would not give the wanted result.
 
PeterB said:
Always put PHP variables outside of quotes, newer versions wont parse strings for variables anymore, and there are noumerous examples that clearify that the use of variables inside of quotes is bad.

I beg to differ. All versions of php that I know of (including php5) parse strings for variables and there's nothing wrong with using variables in a string. Its just redundant to put quotes around a variable if there's nothing else in the string.
example
$var1 = 'tasty';
echo $var1;
as to echo "$var1";
unless its something like echo "mmm... $var1"; :p


If you wanted to literally print $compu-ter you would use the single ticks instead of quotes
example
echo '$compu-ter';

otherwise in a string with quotes don't have the variable sign ($) in front of anything thats not intended to be a variable.
 
Last edited:
That it works doesn't mean that it's good, compare it to superglobals, if your url is 'index.php?action=post', you can call it on 2 ways, $action and $_GET['action'], but the first one will disappear, just like the variable in quotes eventually will.

Read this: Click

Peter
 
In my opinion that's still personal preference. Unless I hear from an official source that php or any other interpreted language (perl, python) stop parsing quoted strings for variables...

To me, it's easier to read a string with the variables in them instead of crowding with concentinations.
$var1 = "This is a " . $cumbersome. " way to do " . $it " .";
Whereas I would rather see
$var1 = "This is a $less_cumbersome way to do $it";

That guide was written way back in 02' and updated in 03'
Nothing has really changed since then...
And I'm surpised that the sprintf method is suggested as a replacement to variables in quotes. That is basically C and to me that seems like the least "readable" of all the others combined.

Just my preference but I continue to use phpBB's guidelines as a base to my own and I consider my code very redable. (But thats just me :p )
http://area51.phpbb.com/docs/coding-guidelines.html
 
Last edited:
Back
Top