We have used
'echo()' language construct in the tutorials of PHP and now we will elaborate it.
In the heading, there is another contruct listed which is
A Language Construct is similar to a PHP function in a programming language but with some differences.
In terms of syntax a function will always use parenthesis(), but a language construct doesn't necessarily requires it.
Print and Echo are Language Constructs in PHP, there are many more and there are other differences also between a function and a Construct
but it is a bit deeper and we will talk about it later. For now just keep it in mind that these
both are 'Language Constructs' and not 'functions' or 'statements'.
Let's continue with Echo!
The PHP ECHO construct is used in PHP to display content on the screen, i.e. to display output.
It can be
used to display a normal string of characters, variable's value, any expression's result(like $a+$b), etc.
As we have told you earlier that because Echo is a language construct and really an in-built function so it can be used with or without parenthesis().
But there is a drawback of using parenthesis that you can only use single parameter with it, if you want to display multiple parameters' values then you have to use those within double quotes
String can be displayed by enclosing it with single('') or double quotes(""). Both type of quotes will give different results when using them with varibales. Single quotes('') will treat variables as string and instead of displaying their value, variables name will be displayed as written. But if you use double quotes("") then variable's values will be displayed with the string.
For variables, you can use parenthesis or can choose not to use it at all. For directly displaying expressions(say, $x+$y) value, you can choose to enclose the expression within parenthesis or can choose not to, both will give the same result. See the example below and look at all the possible ways to use PHP ECHO.
You can see in the example above, how you can display string using ECHO and combine strings with variables. We hope you have understood the difference between using different quotes and how you can store and display strings using variables with echo.
The difference between ECHO and PRINT is that
PRINT always returns '1' which can be used in any expression but echo doesn't return any value and that's why ECHO is slightly faster.
How to display String and variables with PRINT? In case of PRINT, we cannot pass multiple parameters at once unlike ECHO, for each parameter we has to write another PRINT() Construct. Also in ECHO we used comma(,) to separate strings but we cannot do it with PRINT().
For variables, you can enclose them with parenthesis or double quotes but if you want to use a variable's value with a string then you have to dot(.) operator. Also to directly dispaly an output of any expression(say, $a+$b) you can choose to enclose the expression with parenthesis or you can also choose not to, it will work. Look at the example below to see all the possible ways of displaying output with PHP PRINT:
Let's take one more example:
|Difference||PHP Echo||PHP Print|
|type||It is a Language Construct.||It is also a Language Construct.|
|Parenthesis||It can or can't be written with parenthesis.||It can or can't be written with Parenthesis.|
|Strings||Can take multiple strings at a time.||Can only take one string at a time.|
|Speed||It is faster.||Slower than ECHO.|
|Return Value||It doesn't return any value.||It returns 1 always.|