Differences in echo and print in PHP

ADMEC Multimedia > Web Development > Differences in echo and print in PHP

This is the very first topic in PHP when I teach PHP in this institute. Mostly this question comes from one of or all the students ‘sir what is the difference between echo and print in PHP?’. Here I will explain this with all my efforts to make it clear to you for always. I will give some examples in PHP code so that you can copy and paste that code to check the results too.

First of all these both of the commands more or less are very same in nature and usability except on few points. Both echo and print in PHP are language construct not the PHP functions so you can use them without and with parenthesis ().

Examples:

echo ‘hello admec multimedia institute’;
echo (‘hello admec multimedia institute’);
print ‘hello admec multimedia institute’;
print (‘hello admec multimedia institute’);

All the above lines are correct and completely valid. Parenthesis are optional not compulsory for echo and print in PHP.

I am sure still you will be thinking what the language constructs is and what is the difference between language construct and functions in PHP? Isn’t it?

“A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language. The term Language Constructs is often used as a synonym for control structure, and should not be confused with a function.”

— Wikipedia

Let me make it easy to you; a language construct is something that a constructive programming language such as PHP uses out of token to make available flexibility of it to the hands of programmers. Suppose you make a recipe from some fixed ingredients but sometimes you use some other ingredients to make it more tasty so these extra gredients are construct for your dish in the same way echo and print are for PHP.

echo simply output a value to your browser while print returns a value and i.e. 1 so print can be used in expressions.

Example:

if(print(‘admec’)){
        $url = ‘www.admecindia.co.in’;
}else{
        $url = ‘www.google.co.in’;
}
echo $url; // result would be www.admecindia.co.in as it will return 1 and that is true

echo can print multiple values and variable values using comma while print can’t do that. Please see the below given example.

<?php
     $name = 'admec multimedia';
     $location = ' Rohini, Delhi';
     echo $name, $location; // admec multimedia Rohini, Delhi
     print $name, $location; // Parse error: syntax erorr, unexpected ','
?>

For more information please visit more PHP Tutorials.

Leave a Reply

Related Posts

Copy link
Powered by Social Snap