What is difference between echo print and printf in php?

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 ('
'.$var.'
');

or

echo "'
'.$var.'
'";

What is the proper way to do that?

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

$tempvar = '
'.$var.'
' print ($tempvar);

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

syrkull

2,2454 gold badges32 silver badges63 bronze badges

asked Oct 1, 2009 at 15:54

menardmammenardmam

9,62027 gold badges82 silver badges113 bronze badges

2

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.

answered Oct 1, 2009 at 15:57

2

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

answered Oct 1, 2009 at 16:03

AtmocreationsAtmocreations

9,70313 gold badges64 silver badges102 bronze badges

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

';
  var_dump($your_var);
  echo '
'; ?>

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

answered Oct 1, 2009 at 16:24

JF SimonJF Simon

1,2359 silver badges12 bronze badges

1

http://us2.php.net/echo

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

if you have the short_open_tag option enabled you can do


But some find that messy.

answered Oct 1, 2009 at 15:58

Will ShaverWill Shaver

11.7k5 gold badges49 silver badges64 bronze badges

2

You can also use the following syntax:

    echo <<
            $var
        
ENDOFTEXT;

Just make sure the ENDOFTEXT is not indented.

answered Oct 1, 2009 at 16:02

AnaxAnax

8,8725 gold badges32 silver badges67 bronze badges

You could do something like this:

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.

answered Oct 1, 2009 at 15:57

Doctor BlueDoctor Blue

3,5876 gold badges38 silver badges60 bronze badges

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

$var>

while the second will result in

'
'.$var>.'
'

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.

answered Oct 1, 2009 at 16:01

GumboGumbo

627k106 gold badges766 silver badges836 bronze badges

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

answered Jun 13, 2010 at 13:44

redbenredben

5,4785 gold badges45 silver badges62 bronze badges

What is the difference between print and printf?

The difference between printf and print is the format argument. This is an expression whose value is taken as a string; it specifies how to output each of the other arguments. It is called the format string. The format string is very similar to that in the ISO C library function printf() .

What is the difference between echo and printf () Give an example for each?

Printf provides for the creation of a formatting string and offers a non-zero quit status when it fails. Whereas echo normally leaves with a 0 status and typically outputs inputs headed by the end of line character upon this standard result. The “printf” gives you more options for the output format than the “echo”.

What is printf in PHP?

Definition and Usage. The printf() function outputs a formatted string. The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step". At the first % sign, arg1 is inserted, at the second % sign, arg2 is inserted, etc.

What is print function differentiate between echo print () and Print_r ()?

The print and echo are both language constructs to display strings. The echo has a void return type, whereas print has a return value of 1 so it can be used in expressions. The print_r is used to display human-readable information about a variable.