1

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

3
  • "As you know, a good programmer is a lazy programmer, but I'm just lazy." I did not know either of those things. Now I feel enlightened. Commented Sep 26, 2010 at 16:42
  • I don't know how much echo-ing you're doing in your project, but if it's a lot then do think about using one of the many available template libraries to help you print output. Smarty is a popular one. Commented Sep 26, 2010 at 16:42
  • Apart from Smarty (the popular one) there is Twig (the good one). Twig supports some things Smarty does not (most handy of all: Template Inheritance) and is faster than smarty. Commented Sep 26, 2010 at 17:02

5 Answers 5

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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?
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'];
thanks very much for that! Is there an alternative to "$array[element]" or $array['element'] that I can use that ISN'T evil? :)
printf (php.net/printf) would be my choice, a few extra bytes though :p printf("start %s end", $array['element']);
2

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";

Comments

2
$s = 'string start ';
$e = ' string end';
$v = $arr['element'];

// Now you need only 14 keystrokes:
echo $s.$v.$e;

3 Comments

You can do even better $t = $s.$v.$e; echo $t. Or you can define('t', $s.$v.$e); echo t.
@nikic - I guess my laziness has its limits. :)
although the actual echo statement is shorter, your solution actually makes the code longer, and more cumbersome to use inline. Thanks anyway though.
1

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.

Comments

0

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']);

2 Comments

echo sprintf() is an "antipattern". There is absolutely no reason that anyone should ever write echo sprintf() in any code for any reason -- it should be printf() every time.
That's an interesting point of view mickmackusa, although I'm not sure it's as universally true as you make it seem. Many teams decide that echo should be the only function used for output and they deliberately avoid print, printf, print_r, etc. Favoring print or echo isn't really an "antipattern" -- it's just a decision that individual projects make based on the (pretty minor) differences between the two approaches.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.