Echo In PHP, is a language construct not a function, so you don’t need to use parenthesis with it. In this article we will explore this concept in detail. We would focus on following two pointers,
So let us get started in then,
Echo In PHP
In order to output one or more strings, we can use echo statement. Anything that can be displayed to the browser using echo statement, such as string, numbers, variables values, the results of expressions, etc. It is required to use parenthesis if you want to use more than one parameter. Unlike some other language constructs echo does not behave like a function, so it cannot always be used in the context of a function. The end of echo statement is identified by the semi-colon (‘;’).
Below example demonstrates the basic usage of echo in PHP
<?php $content = "PHP course"; $num=50; $num1=60; $char='c'; echo $content."<br>"; echo $num."+".$num1."="; echo $num + $num1."<br>"; echo "this is my statement"; ?>
Below example demonstrates the example of using HTML code using PHP
<?php echo "<h1>This is html.</h1>"; echo "<h2 style='color: blue;'>This is heading with style.</h4>"; ?>
Echo vs Print
echo is similar to print where both are used to output data to the screen. Some of main differences are:
- Echo can be used in expressions as it has no return value while print has a return value of 1.
- echo is marginally faster than print and can take multiple parameters while print can take only one argument.
echo | ||
Type | Echo is a type of output string | Print is also a type of output string |
Parenthesis | Basically, it is written with parenthesis | It can and cannot be written with parenthesis |
argument | echo ($arg1[,$arg2..]) | print($arg) |
Functionality | It is faster than print | It is slower than echo |
Strings | It can output one or more strings | It can output only one string at a time and returns 1 |
As we have already seen the demonstration of echo above, let us see an example of print in PHP
<?php $content = "PHP course"; $num=50; $num1=60; $char='c'; print $content."<br>"; print $num."+".$num1."="; print $num + $num1."<br>"; print "this is my statement"; ?>
Got a question for us? Please mention it in the comments section and I will get back to you.