PHP Tutorials: Print and Echo Statement

PHP Echo

Now it’s time to display something on the webpage. Till now we know how PHP processes and executes, how the value gets stored in a variable and constant. Now it comes to display the value of the variables or display a string. It can display number, string, variable value, expression results, etc.

PHP has an echo statement to display anything on the webpage. Echo is a language constructor and not a function, it can be used with parentheses “echo()” or without parentheses “echo“. However, if you wanted to pass any parameter to the echo statement then you have to use echo(). Most of the time you will use echo without parentheses.

I am not going to complicate anything at this point because you are a newbie and you can get confused with long statements. Let us see some examples.

How to display a string using echo?

A string is basically a collection of the alphabet. If you write a single alphabet i.e. “a” it will be called a character but if you combine 2 or more alphabet then it will become a string i.e. “Hello World!”. I will explain the string in later lessons in more details. To display a string you just have to enclose the string inside Quotation Marks. Let us an example.

<?php
     echo "Hello World!";
     //OUTPUT Hello World!
?>

How to display HTML using echo?

There is not a big difference between displaying string and displaying HTML with echo statement because both are strings. The only difference is, the string, in this case, will have some HTML tags. Let us see how

<?php
     echo "<h2>Hello World!</h2>";
?>

The output will look like this

Hello World!

How to display variables value using echo?

Now we know that how we can display a string and HTML with echo statement. Here we are going to see how we can display the value of a variable. Honestly speaking there is no difference with any echo statement either you display string or a variable value. Let us see how we can do this

<?php
     $num = 10;
     $name = "Naresh Kumar";
     $website = "lakblog.com";
     echo $num;
     echo "<br>";
     echo $name;
     echo "<br>";
     echo $website;
?>

Output will be
10
Naresh Kumar
lakblog.com

PHP print statement

The print statement is very similar to the echo statement. It’s also a language construction and can be used with parentheses print() or without parentheses print.

Both statements echo and print works exactly similar except that the print statement can only output one string and always return 1. That’s the only reason echo is considered marginally faster than print.

How to display a string using echo?

<?php
     print "Hello World!";
     //OUTPUT Hello World!
?>

How to display HTML using echo?

<?php
     print "<h2>Hello World!</h2>";
?>

output will be

Hello World!

How to display variables value using echo?

<?php
     $num = 10;
     $name = "Naresh Kumar";
     $website = "lakblog.com";
     echo $num;
     echo "<br>";
     echo $name;
     echo "<br>";
     echo $website;
?>

Output will be
10
Naresh Kumar
lakblog.com

Leave a Reply

Your email address will not be published. Required fields are marked *