Definition and usage
The Echo () function outputs one or more strings.
Note: the Echo () function is not actually a function, so you do not have to use parentheses on it. However, if you want to pass more than one parameter to echo (), using parentheses will generate a parse error.
Tip: the Echo () function is slightly faster than print ().
Tip: the Echo () function also has a simplified syntax. Prior to PHP 5.4.0, this syntax only applies if the Short_open_tag configuration setting is enabled.
Grammar
Echo (Strings)
We all know that. In Echo, you can concatenate strings with commas. and tested. Such a connection string is faster than using the dot number directly. 12
Like what:
Echo ' abc '. Def '; connect string Echo ' abc ' with Dot, ' def '; Concatenate strings with commas 12
Maybe a lot of people know that commas are faster than dots. But I don't know why. I don't know what the difference is.
So here are some examples. To recognize the difference before them. 123
Echo ' 1+5= '. 1+5;1
Look at the above. The result of the output is 6. Not 1+5=6. It's kind of magical, isn't it?
More amazing is the example you see below. 123
Echo ' 1+5= '. 5+1; Output 2 Results 1
Very strange. We saw. We changed the position of 5 and 1. The result becomes 2.
Why is this so? Is there no exchange law for addition in PHP? Of course not.
Let's not think about why. If I replace the dot above with a comma, try it. 12345
Echo ' 1+5= ', 5+1; Output 1+5=6echo ' 1+5= ', 1+5; Output 1+5=612
can be seen. Only by using commas can we get the expected results.
Then why not order it? Why would a comma be OK? 123
Echo (' 1+5 '. 5) +1; Output 2 1
We give the previous parentheses. The result is the same.
Prove that PHP is connected to the string before the addition calculation. In the direction from left to right.
So good. Since it is a string that is connected first. Then it should be "1+55". Then use this string to add 1. So why would it output 2?
This is related to the mechanism in PHP where strings become numbers. Let's take a look at the following example 123456
echo (int) ' ABC1 '; Output 0echo (int) ' 1ABC '; Output 1echo (int) ' 2ABC '; Output 2echo (int) ' 22ABC '; Output 221234
As we can see from the example above. If you cast a string to a number. PHP will search for the beginning of the string. If it is not, it will return 0 directly.
Go back to the 1+55. Since this string is 1+55, it should be 1 after forcing type conversion. On this basis add 1. Of course 2.
In order to prove our conjecture. Let's check it out. 1234
Echo ' 5+1= '. 1+5; Output 10echo ' 5+1= '. 5+1; Output 6echo ' 1+5= '. 1+5; Output 6echo ' 1+5= '. 5+1; Output 21234
The result proves that our vision is correct.
So why do you use commas without the above problem?
* * It says in the manual. The comma is multiple parameters. That is, multiple parameters. In other words, the comma is separated by the equivalent of n parameters. That is, ECHO is used as a function. So. Echo will
Parameter description
Strings required. One or more strings to send to the output.
Technical details return value:
Example 1
The value of the output string variable ($STR):
<?php$str = "Hello world!"; Echo $str;? >
Example 2
The value of the output string variable ($STR) that contains the HTML tag:
<?php$str = "Hello world!"; echo $str; echo "<br>what a nice day!";? >
Example 3
Connect two string variables:
<?php$str1= "Hello world!"; $str 2= "What a nice day!"; echo $str 1. " " . $str 2;? >
Example 4
The value of the output array:
<?php$age=array ("Peter" = "+"); echo "Peter is". $age [' Peter ']. "Years old.";? >
Example 5
Output some text:
<?phpecho "This Textspans multiplelines.";? >
Example 6
How to use multiple parameters:
<?phpecho ' This ', ' string ', ' is ', ' made ', ' with multiple parameters. '? >
Example 7
The difference between single and double quotation marks. Single quotes will output the variable name instead of the value:
<?php$color = "Red", echo "Roses is $color", echo "<br>"; Echo ' Roses is $color ';? >
Example 8
Simplified syntax (only for cases where Short_open_tag configuration settings are enabled):
<?php$color = "Red";? ><p>roses is <?= $color?></p>