Echo and Print is a statement in PHP programming language. PHP echo statement and print statement is used for displaying a program output of a specific block of code. Any function result, Strings, conditions etc can be displayed on the screen by echo statement. In the previous article, we have already used this statement in examples. In this article, we will learn about PHP Echo and Print statement in details.
PHP Echo Statement
Echo is not a function in PHP scripting language. Because it can be used without parenthesis. But parentheses can be used with Echo this statement. Echo is just used for output data on the screen. You can you a different kind of things using echo. But it has a basic implementation for getting an output. Let’s see that.
echo/Echo/ECHO “Output data here”;
You can use the echo statement to display program data by one of the three above them. Echo is not case sensitive. But official recommendation of using Echo statement is
echo. Every echo statement ends with a semi-colon (
The string is a plain word or sentence in programming languages. When a programmer wants to display a word or sentence on the screen, he has to write the sentence as a string and put it in the echo statement. But how can we implement the echo statement for getting an output of the string? Remember:
After defining the echo statement, you have to put the string in a quotation mark
"string goes here" and you also have to remember to put the semi-colon at the end of the echo statement. So the implementation of displaying string will be
echo "string goes here";.
<?php echo "Hey! it is a string and will be displayed for echo statement"; ?>
Hey! it is a string and will be displayed for echo statement
Displaying multiple arguments string
If you have some different string from the different location then you can use those string together to get output in a single line. Alternatively, you can break down a long sentence string or multiple arguments to the echo statement instead of using single argument. In the same way, you have to put the string argument in the quote mark
"". But for separating the multiple arguments you have to use,
","operator between two string argument.
Suppose you have two argument one is “Program” and another is “is fun!”. Now see the example below:
<?php echo "Program ","is fun!"; ?>
The output will be:
Program is fun!
Now you should clear the multiple arguments string.
Variable is a most common feature in all the modern programming language. If you don’t know yet what is variable. Don’t worry, we will learn in the later tutorial. But for now, we will learn how to echo a variable. Echoing a variable also easy as the normal string. Let’s see an example, It will be clear:
<?php $text = "Hello, World!"; #It is the way of defining a variable in PHP $num1 = 10; #It's also a variable, but it is an integer variable and that's why no quote mark used here. $num2 = 20; #we will learn about variable and its types in the later article. //now let's echo the variable echo $text; // Be mindful, there is no qoutation mark here. ?>
In the above example, you can see still we have used the semi-colon at the end of the echo statement. But see carefully, there is no quotation mark given around while defining the variable by its name. Even we didn’t use the quotation mark in the echo statement. It’s the basic difference between echoing string and variable.
The output will be:
PHP Print Statement
PHP print statement is much similar to the echo statement. You can use the print statement as the alternatives of the echo statement. But there are some limitations in the print statement will see. The print statement can’t display one more argument at a time. It can only print one string per print statement. One more characterize of print is always returns a value 1. But like echo, print can be used for variable printing.
We can display the output of a particular program using print statement just like in echo statement we did. They are almost the same. But there is a limitation in the print statement. You can’t use
"," operator for printing multiple strings or variables at a time. Let’s see an example:
<?php print "Hello, world!"; //It will output: Hello, world! print "Hello, ","world!"; //It will give an error because print does not support it. ?>
Displaying the variable using print statement is the same as the echo. Just replace the echo with print.
Note: Always use your localhost means XAMPP or use your live hosting server to test code. It will make the habit of development services/application. So don’t use any kinds of online IDE. It will make a big practicing problem