echo and print in php differece

echo has no return value, print has return value, print with out () and one argument

echo return fast , print gets some time

print ("This ", "string ", "was ", "made ", "with multiple parameters.");- only one argument -> syntax error- print with or without paranthesis

$sum = 10 + print("5"); // Using print as part of an expression

echo $sum; // Output: 11

echo "This ", "string ", "was ", "made ", "with multiple parameters.";->print the thing, not () for echo


 In PHP, both echo and print are used to output strings or variables to the browser or command line. However, there are a few differences between echo and print:

  1. Return Value: echo does not have a return value, while print has a return value of 1. This means that print can be used as part of an expression, whereas echo cannot.

  2. Syntax: echo is a language construct and does not require parentheses. You can use it with or without parentheses, but the latter is more common. On the other hand, print is a function and requires parentheses.

    Example:


echo "Hello, World!"; // without parentheses
echo("Hello, World!"); // with parentheses

print("Hello, World!"); // with parentheses


Multiple Arguments: echo can take multiple arguments, separated by commas, and it can output all the arguments at once. print, however, can only accept a single argument.

Example:



echo "Hello", " ", "World!"; // outputs "Hello World!" print "Hello", " ", "World!"; // produces a syntax error




  1. Performance: echo is generally considered to be slightly faster than print because it is a language construct and does not have the overhead of being a function.

In practice, echo and print are often used interchangeably for simple string output. However, echo is more commonly used in PHP code due to its flexibility and slightly better performance.


No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...