Start with an example.
Launch the editor, create a php file and type the following code:
<?php echo "你好!"; ?>
Run
Name the file test.php and store it in the E:html directory.
Access the php file in the browser address bar: http://127.0.0.1/test.php, the output result is as follows:
<code class="language-php hljs">你好!</code>
In this example, we use the echo command to output a string "Hello!".
As can be seen from this example:
Of course, to output standard html code, you can improve the above example to:
PHP 测试 <?php echo "你好!"; ?>
It can be seen that PHP code can be mixed with XHTML code.
echo is a built-in instruction in PHP, used to output one or more strings. Similar to echo, there is print.
Below we will introduce the output function of php.
The difference between echo and print:
Tip: echo outputs faster than print, echo has no return value, and print has a return value of 1.
echo is a language structure, which can be used without parentheses or with parentheses: echo or echo().
1. Display string
The following example demonstrates how to use the echo command to output a string (the string can contain HTML tags):
<?php echo "<h2>PHP is fun!</h2>"; echo "Hello world!<br>"; echo "I'm about to learn PHP!<br>"; echo "This", " string", " was", " made", " with multiple parameters."; ?>
Run
2. Display variables
The following example demonstrates how to use the echo command to output variables and strings.
<?php $txt1="Learn PHP"; $txt2="manongjc.com"; $cars=array("Volvo","BMW","Toyota"); echo $txt1; echo "<br>"; echo "Study PHP at $txt2"; echo "My car is a {$cars[0]}"; ?>
Run
Print is also a language structure that can use parentheses or not: print or print().
1. Display string
The following example demonstrates how to use the print command to output a string (the string can contain HTML tags):
<?php print "<h2>PHP is fun!</h2>"; print "Hello world!<br>"; print "I'm about to learn PHP!"; ?>
Run
2. Display variables
The following example demonstrates how to use the print command to output variables and strings:
<?php $txt1="Learn PHP"; $txt2="manongjc.com"; $cars=array("Volvo","BMW","Toyota"); print $txt1; print "<br>"; print "Study PHP at $txt2"; print "My car is a {$cars[0]}"; ?>
Run
Original address: http://www.manongjc.com/php/php_format.html
Related reading:
php uses the filter_var() function to detect whether the url contains the query string
php detects whether it is an IPv6 address
php uses the filter_var() function to detect whether a number is within a range
How does php ajax determine that the network connection is disconnected