Welcome to PHPBash. In this PHP Tutorial for Beginners , we will learn about PHP echo and print Statement. PHP print return 1 value where as PHP echo do not return any value. These both statements are used for printing output in PHP. The PHP echo or PHP print statement can be used for printing variables, strings, escaping characters etc.
PHP echo and print Statements | PHP Echo Vs Print
This PHP tutorial covers all the topics of Basics of PHP Variable like
1. PHP Echo Statement
1. PHP Echo Statement
2. PHP Echo Statement Example
3. PHP print Statement
4. PHP print Statement Example
4. PHP print Statement Example
5. PHP Echo Vs Print
Now let's start this tutorial with understanding PHP Echo Statement with its Examples.
In PHP, The output is displayed using either print or echo statements. For printing data, In PHP we can used print or echo statement. There is very small difference between print and echo statement. The print statement return value 1 where as echo do not return any value.
PHP Echo Statement
Basically echo is not a PHP function, but it is language construct. It does not Behave like function. So you don't have to pass any parameter to it. but, You can pass arguments to echo language construct as well if required. If you want to pass more than one parameter, there should not be any parentheses.
IMPORTANT:
1.PHP echo must be ended with semicolon(;)
2.PHP echo does not return any value.
3.PHP echo can be used with or without parenthesis i.e echo or echo()
4.In case of performance comparison, PHP echo is faster than PHP print statement.
Let's look at its syntax:
Syntax:
<?php
void echo ( string $arg1 [, string $... ] );
?>
PHP echo Statement Examples
PHP echo Example: String Message Without parentheses
<?php
echo "Hello Message!!";
?>
PHP echo Example: String Message With parentheses
<?php
echo ("Hello Message!!");
?>
PHP echo Example: Multiline String Message
<?php
echo "Hello Message!!
This is Multiline Message
Printed";
?>
PHP echo Example: Printing variable value
<?php
$str ="Hello Message!!";
echo "The Message is".$str;
?>
PHP echo Example: Printing multiple variable value
<?php
$str ="Hello Message!!";
$str1="Welcome to PHP";
echo "The Message is".$str.$str1;
?>
PHP echo Example: Printing escaping characters
<?php
echo "The Message is \"IMPORTANT\" for PHP";
?>
PHP echo Example: Printing Strings as multiple arguments
<?php
echo "The Message 1","The Message 2","The Message 3";
?>
PHP echo Example: using HTML tags
<?php
print "<h1>The Message is IMPORTANT for PHP</h1>";
?>
IMPORTANT:
1. In PHP, The dot(.) is used for concatenating two Strings.
2. You can use different HTML tags in echo statement like for breaking line you can use <br/>. You can also use "\n".
Another syntax to use echo statement is with short_open_tag. For this, short_open_tag setting from php.in must be enabled. You can check more details about how to configure php.in in our previous tutorial.
Example:
<?php
$msg="Hello";
This is <?=$msg?> Message.
?>
In PHP, the string,strings with multi-line, variable,constants, array etc can be printed or displayed using echo statement.
PHP print Statement
As discussed earlier, the PHP print statement is also used for printing or displaying data as output alternative to PHP echo statement. You can use PHP print statement as print or print().
PHP print statement can also be used to print strings and variables. Below are some examples of using print statement in PHP:
Syntax:
<?php
print "Message";
?>
PHP print Statement Examples
PHP print Example: String Message
<?php
print "Hello Message!!";
?>
PHP print Example: Printing variable value
<?php
$str ="Hello Message!!";
print "The Message is".$str;
?>
PHP print Example: Printing multiple variable value
<?php
$str ="Hello Message!!";
$str1="Welcome to PHP";
print "The Message is".$str.$str1;
?>
PHP print Example: Printing escaping characters
<?php
print "The Message is \"IMPORTANT\" for PHP";
?>
PHP print Example: using HTML tags
<?php
print "<h1>The Message is \"IMPORTANT\" for PHP</h1>";
?>
PHP Echo Vs Print
There is following difference between PHP echo and print statement:
1. PHP echo statement do not return any value where as PHP print statement return value 1.
2. PHP echo statement can accept multiple parameter where as PHP print statement do not accept more than 1 parameter.
3. PHP echo statement is slower as compare to PHP print statement.
I would acknowledge you sharing your individual experiences about "PHP echo and print statement and their difference". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section.
I will certainly take care that, I should give reply for every single comment on my blog posts. So, let’s fire the discussion up! See you in the comments section below..
Sharing is caring ❤️
Thank you for Visiting PHPBash. Keep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced. As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project.
Comments
Post a Comment
Thank you so much.