How Are Echo and Print Different in PHP

How are echo and print different in PHP?

From:
http://web.archive.org/web/20090221144611/http://faqts.com/knowledge_base/view.phtml/aid/1/fid/40

  1. Speed. There is a difference between the two, but speed-wise it
    should be irrelevant which one you use. echo is marginally faster
    since it doesn't set a return value if you really want to get down to the
    nitty gritty.

  2. Expression. print() behaves like a function in that you can do:
    $ret = print "Hello World"; And $ret will be 1. That means that print
    can be used as part of a more complex expression where echo cannot. An
    example from the PHP Manual:

$b ? print "true" : print "false";

print is also part of the precedence table which it needs to be if it
is to be used within a complex expression. It is just about at the bottom
of the precedence list though. Only , AND OR XOR are lower.


  1. Parameter(s). The grammar is: echo expression [, expression[,
    expression] ... ]
    But echo ( expression, expression ) is not valid.
    This would be valid: echo ("howdy"),("partner"); the same as: echo
    "howdy","partner"
    ; (Putting the brackets in that simple example
    serves
    no purpose since there is no operator precedence issue with a single
    term like that.)

So, echo without parentheses can take multiple parameters, which get
concatenated:

   echo  "and a ", 1, 2, 3;   // comma-separated without parentheses
echo ("and a 123"); // just one parameter with parentheses

print() can only take one parameter:

   print ("and a 123");
print "and a 123";

Should I use echo or print in php scripts?

Supposedly echo is faster, but either one would work just fine.

Echo also offers a shortcut syntax when embedding php in HTML. i.e.

    I have <?=$foo?> foo.

vs

     I have <?php echo $foo;?> foo.

See http://us2.php.net/manual/en/function.echo.php

PHP: Is there any advantage to using echo instead of print?

The print returns a value while echo does not making echo slightly faster (not a big deal though). You may check out this post for more:

  • echo VS print

Other than that, you use echo to output something unlike print to get some return value too. Therefore, echo gets best in the queue but nothing stops you from using print.

alt text

What is the difference between print_r and echo when providing results to an AJAX call?

The most common built-in output functions in PHP are: echo, print, printf, print_r, var_dump, and var_export. They are all designed to output data, albeit, in slightly different ways. Which is why there are specific use cases associated with each function.

  • echo: The most notorious. It's what you should be using to output data to an AJAX caller. It's simple and effective. It's actually a language construct, so it doesn't require you to use brackets. You just write echo 'string'; or echo $something;, etc.

    It also supports multiple comma-delimited values, making it handy in a variety of use cases; e.g., echo 'string1', 'string2', 'string3';. Note that echo returns null (nothing).


  • print: Also a language construct, it's nearly the same as echo, with the difference being that it accepts only one argument and always returns the same thing: 1. It's OK to use print instead of echo if you like, but many popular PHP projects have code standards that ask you not to do so. So print has become, more or less, an unused (or rarely used) function in PHP.

  • printf: This function and its brother sprintf() are designed to output formatted strings. It's slightly more complex, but does have specific use cases. For example, these are often used to output an integer as a binary number or octal number, for translation, among many other options that are well documented at PHP.net — that's a separate topic.

  • print_r, var_dump, and var_export: These are most commonly used when debugging code or testing code in one way or another. You will rarely find a use case for them in production code. That said, these allow you to pass in complex data types, such as an entire array, and print_r recursively, to dump the entire array for analysis. var_dump does pretty much the same thing, but uses a slightly different approach that can sometimes be useful. var_export has the advantage of outputting a parseable string.

    print_r and var_export both support a parameter that can 'return' the value it would normally output, which can be a handy feature. Something that var_dump is not capable of.

What's the difference between echo and printf

If you search the differences in any search engines, you'll get your answer. For example below is one use for printf:

You can use this function to format the decimal places in a number:

$num = 2.12; 
printf("%.1f",$num);

prints:

2.1

And of course this is only just one use. Another good sample that i use is below:

If you are printing data in a "pre" tag, courier font, or otherwise non-variable length font (like a terminal), you might have use for printf's justify feature.


$value1 = "31298";
$value2 = "98";

print "<pre>\n";
printf ("%'.-15.15s%'.6.6s\n", $heading1, $value1);
printf ("%'.-15.15s%'.6.6s\n", $heading2, $value2);
print "</pre>\n";
?>

And lots of other usage.

Why do print and echo behave differently in a for loop

Expression. print() behaves like a function in that you can do: $ret
= print "Hello World"; And $ret will be 1. That means that print can be used as part of a more complex expression where echo cannot. An
example from the PHP Manual:

$b ? print "true" : print "false";

Some part of my answer are part of below answer. I think this is the answer of your question. Most important part is print() behaves like a function

see this answer: https://stackoverflow.com/a/234255/1848929

What about echo:

Note: Because this is a language construct and not a function, it
cannot be called using variable functions.

see notes part on this page: http://us2.php.net/manual/en/function.echo.php

diference between return , echo and print keyword in PHP

return is a language construct used to exit a function and give a value to the caller of the function.

echo and print are both language constructs that output strings. The main difference is that echo can take multiple arguments separated by commas, but print accepts only a single argument.

Is there any difference between 'print' and 'echo' in PHP?

From this link, suggested by the PHP manual entry for the echo() function:

  1. Speed. There is a difference between the two, but speed-wise it
    should be irrelevant which one you
    use. echo is marginally faster since
    it doesn't set a return value if you
    really want to get down to the nitty
    gritty.

  2. Expression. print() behaves like a function in that you can do: $ret =
    print "Hello World"; And $ret will be

  3. That means that print can be used as part of a more complex expression
    where echo cannot. An example from
    the PHP Manual:

    $b ? print "true" : print "false";


print is also part of the precedence
table which it needs to be if it is
to be used within a complex
expression. It is just about at the
bottom of the precedence list though.
Only "," AND, OR and XOR are lower.


  1. Parameter(s). The grammar is: echo expression [, expression[, expression]
    ... ] But echo ( expression,
    expression ) is not valid. This would
    be valid: echo ("howdy"),("partner");
    the same as: echo "howdy","partner";

    (Putting the brackets in that simple
    example serves no purpose since there
    is no operator precedence issue with a
    single term like that.)

So, echo without parentheses can take
multiple parameters, which get
concatenated:

echo "and a ", 1, 2, 3; //
comma-separated without parentheses

echo ("and a 123"); // just one
parameter with parentheses

print() can only take one parameter:

print ("and a 123"); print "and
a 123";



Related Topics



Leave a reply



Submit