views:

85

answers:

5

Hi All

As you know, a good programmer is a lazy programmer, but I'm just lazy. My question is this: Is there a simpler way to print out an element of an array (from a MySQL query) in a PHP echo statement?

I usually do this:

echo "string start " . $array['element'] . " string end";

It works FINE, I'd just like a shorter way of printing it out, because echo sees the "['element']" bit of the variable as a string. I could use list() to get all the elements, but that's not what I'm after.

So, are there any answers out there?

Thanks for reading,

James

A: 

Surround your array with {} for string interpolation, or use sprintf

<?php
    $array = array('foo'=>'bar');

    echo "Print it with {}: {$array['foo']}";
    echo "\n";
    echo sprintf("Print it with sprintf: %s", $array['foo']);
Alan Storm
+2  A: 

I'm note sure i understand what you want to do.

If you want a "shorter" version, ommit the concatenation like so:

echo "string start $array[element] string end";

This also works:

echo "string start {$array['element']} string end";
alexn
+1 for mentioning the extended variable syntax.
nikic
+4  A: 

You can actually just do

echo "string start $array[element] string end";

PHP allows vars in double-quoted strings. That being said, please don't.

Alexander Sagen
+1 for "please don't".
GZipp
You're saying even normal variables (not arrays) shouldn't be printed in an echo? If so, why? Also, the above example would work, but I'm using strings as the element name, unless you mean PHP will accept array elements without quotes?
JamWaffles
Cause it's bad karma... Including arrays. Good idea to keep literals and variables separated, to keep the code readable and ease refactoring. (No-one's gonna shoot you though, but I would at least avoid if you're working on a team) It will work for you, echo "$array[element]"; is the same as echo $array['element'];
Alexander Sagen
@sagen - thanks very much for that! Is there an alternative to "$array[element]" or $array['element'] that I can use that ISN'T evil? :)
JamWaffles
printf (php.net/printf) would be my choice, a few extra bytes though :p printf("start %s end", $array['element']);
Alexander Sagen
+2  A: 
$s = 'string start ';
$e = ' string end';
$v = $arr['element'];

// Now you need only 14 keystrokes:
echo $s.$v.$e;
GZipp
You can do even better `$t = $s.$v.$e; echo $t`. Or you can `define('t', $s.$v.$e); echo t`.
nikic
@nikic - I guess my laziness has its limits. :)
GZipp
although the actual echo statement is shorter, your solution actually makes the code longer, and more cumbersome to use inline. Thanks anyway though.
JamWaffles
<...snicker...>
GZipp
+1  A: 

As I heavily dislike interpolating variables in strings I prefer using echo with several arguments:

echo 'string start ', $array['element'], ' string end';

Apart from this being faster then string concatenation (.) it deals better with echoing results of expressions, because , has the lowest of all precedences.

nikic
thanks very much!
JamWaffles