Interpolation (Double Quoted String) of Associative Arrays in PHP

Interpolation (double quoted string) of Associative Arrays in PHP

Yes, you may trust it. All ways of interpolation a variable are covered in the documentation pretty well.

If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.

How to put multidimensional arrays double quoted strings?

You should enclose more complicated structures in curly braces:

echo "he is {$twoDimArr['family'][1]}";

Why Superglobals can't be printed in double quotes?

It's not that super globals can't it's that you can't access items in an array in the string. If you did the following it'll work fine.

echo "hello {$GLOBALS['x']}";

How to echo element of associative array in string?

Your syntax is correct.

But, still you can prefer single quotes versus double quotes.

Because, double quotes are a bit slower due to variable interpolation.

(variables within double quotes are parsed, not the case for single quotes.)

A more optimized and cleaned version of your code:

$string = $couple['husband'] .' : ' . $couple['wife'] .' is my wife.';

How to detect a syntax error in PHP?

With arrays and functions, you need to put curley braces:

$jsql_ae3 = mysql_query("select products.formats from products where products.id='{$jrowa2['id']}'") or die(mysql_error());

Why does PHP require double quotes for key variable in adding array key-value pairs?

You do not require double quotes. $envArr[$env] is perfectly legal syntax.

$envArr['$env'] would create the literal key '$env', which is not what you want.

However, if $env is not a string or integer, but, say, an object or null, that's when you'd get an illegal offset notice. Interpolating the variable into a string with "$env" forces the variable to be cast to a string, which avoids that problem. But then arguably the problem is that you're trying to use a non-string as array offset, so an error message would be perfectly justified and preferable.

I'd be guessing that you're using SimpleXML and $env is a SimpleXMLElement object. You should be using this then:

$envArr[(string)$env]
// or
$envArr[$env->__toString()]

That's basically the same as encasing the variable in double quotes, it forces a string cast, but in this case it's explicit and not a mystery.

How to detect a syntax error in PHP?

With arrays and functions, you need to put curley braces:

$jsql_ae3 = mysql_query("select products.formats from products where products.id='{$jrowa2['id']}'") or die(mysql_error());

Quotes in PHP array keys

No. There is no extra functionality gained by that. It probably slows it down because it is trying to see if there is any literal content in the string as well as any variables to evaluate.

Is it possible to reuse a double quoted string with variables?

You can set these spans as format strings instead.

$badges = [
"<span class='badge badge--brand badge--inline'>%s</span>",
"<span class='badge badge--dark badge--inline'>%s</span>",
"<span class='badge badge--primary badge--inline'>%s</span>",
"<span class='badge badge--success badge--inline'>%s</span>",
"<span class='badge badge--info badge--inline'>%s</span>",
"<span class='badge badge--warning badge--inline'>%s</span>",
"<span class='badge badge--danger badge--inline'>%s</span>",
];

Then use sprintf to print the spans with your variables.

$TargetStr = 'one,two,three';

$Targets = explode(',', $TargetStr);
$Badges = '';
foreach ($Targets as $index => $target) {
$Badges .= sprintf($badges[$index % count($badges)], $target);
}
echo $Badges;

If you like, you could also use sprintf() to insert the different badge types to eliminate the repeated markup.

$badges = ['brand', 'dark', 'primary', 'success', 'info', 'warning', 'danger'];
$span = "<span class='badge badge--%s badge--inline'>%s</span>";

$TargetStr = 'one,two,three';

$Targets = explode(',', $TargetStr);
$Badges = '';
foreach ($Targets as $index => $target) {
$Badges .= sprintf($span, $badges[$index % count($badges)], $target);
}
echo $Badges;


Related Topics



Leave a reply



Submit