31

Is there a better way to output data to html page with PHP?

If I like to make a div with some var in php, I will write something like that

print ('<div>'.$var.'</div>');

or

echo "'<div>'.$var.'</div>'";

What is the proper way to do that?

Or a better way, fill a $tempvar and print it once? like that:

$tempvar = '<div>'.$var.'</div>'
print ($tempvar);

In fact, in real life, the var will be fill with much more!

syrkull
  • 2,295
  • 4
  • 35
  • 68
menardmam
  • 9,860
  • 28
  • 85
  • 113
  • See http://stackoverflow.com/questions/1006586/is-there-any-difference-between-print-and-echo-in-php/1006600#1006600 – karim79 Oct 01 '09 at 16:02
  • You should also use a presentation layer (such as Smarty) to separate the display logic from your application logic. Smarty templates compile to normal PHP pages, but your code stays much cleaner. – Will Bickford Oct 01 '09 at 19:01

8 Answers8

39

There are 2 differences between echo and print in PHP:

  • print returns a value. It always returns 1.

  • echo can take a comma delimited list of arguments to output.

Always returning 1 doesn't seem particularly useful. And a comma delimited list of arguments can be simulated with multiple calls or string concatenation. So the choice between echo and print pretty much comes down to style. Most PHP code that I've seen uses echo.

printf() is a direct analog of c's printf(). If you're comfortable in the c idiom, you might use printf(). A lot of people in the younger generation though, find printf()'s special character syntax to be less readable than the equivalent echo code.

There are probably differences in performance between echo, print and printf, but I wouldn't get too hung up on them since in a database driven web application (PHP's typical domain), printing strings to the client is almost certainly not your bottleneck. The bottom line is that any of the 3 will get the job done and one is not better than another. It's just a matter of style.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • 10
    Returning 1 is NOT the only difference between `echo` and `print`. IMHO the main difference is that `echo` can output multiple values without concatenating them (i.e., `echo '
    ', $a, $b, $c, '
    ';`) while `print` can't do this.
    – binaryLV Apr 23 '10 at 14:20
  • 1
    @binaryLV: Good point. I forgot about that. I've updated my answer. Thanks. – Asaph Apr 23 '10 at 16:03
10

you can even write

$var = "hello";

echo "Some Text $var some other text";
// output:
// Some Text hello some other text

or

print("Some Text $var some other text");
// output:
// Some Text hello some other text

doesn't make big difference. This works with double-quotes only. With single quotes it doesn't. example:

$var = "hello";

echo 'Some Text $var some other text'; // Note the single quotes!
// output:
// Some Text $var some other text

or

print('Some Text $var some other text'); // Note the single quotes!
// output:
// Some Text $var some other text
Atmocreations
  • 9,923
  • 15
  • 67
  • 102
3

Just try this you gonna love the well formated amount of infos :

<?php
  echo '<pre>';
  var_dump($your_var);
  echo '</pre>';
?>

OK, I explain : set a "code" html format and var_dump show the value, the type, the params ... of the variable.

JF Simon
  • 1,235
  • 9
  • 13
1

http://us2.php.net/echo

<div><? print($var); ?></div>

Or if you don't have short tags on, you might need to

<div><?php print($var); ?></div>

if you have the short_open_tag option enabled you can do

<?=$var?>

But some find that messy.

Will Shaver
  • 12,471
  • 5
  • 49
  • 64
1

You can also use the following syntax:

    echo <<<ENDOFTEXT
        <div>
            $var
        </div>
ENDOFTEXT;

Just make sure the ENDOFTEXT is not indented.

Anax
  • 9,122
  • 5
  • 34
  • 68
0

You could do something like this:

<div><?php echo $var; ?></div>

One of the nice things about PHP is that you can insert it into regular HTML, and accomplish things like the above with ease. I've always used echo myself in PHP. Not sure if it's the "proper" way, but it's the easiest.

Doctor Blue
  • 3,769
  • 6
  • 40
  • 63
0

While echo and print are almost equal, you a using different values. Your first value will result in

<div><value of $var><div>

while the second will result in

'<div>'.<value of $var>.'<div>'

But the rest is semantically almost equal. Since echo and print are no real functions but special language constructs, the parenthesis in your first example is just wrapping the single string value and not the parameter list.

See also https://stackoverflow.com/questions/1462581#1462636 and https://stackoverflow.com/questions/1163473#1163793.

Community
  • 1
  • 1
Gumbo
  • 643,351
  • 109
  • 780
  • 844
0

I have read somewhere that echo is faster that print. But its just too small of a performance gain.

redben
  • 5,578
  • 5
  • 47
  • 63