```The output result is: ```Hello World!```2 . Output a string containing HTML tags: ``"/> ```The output result is: ```Hello World!```2 . Output a string containing HTML tags: ``">
Home > Article > Backend Development > Examples of how to use echo to output strings in PHP
In php, echo is an output statement used to output text in HTML pages.
The echo statement can be used anywhere in a PHP script and can be used to output strings, variables, arrays, objects, Boolean values, etc.
The following are some examples of using echo to output strings:
<?php echo "Hello World!"; ?>
The output result is:
Hello World!
<?php echo "<h1>Welcome to my website!</h1>"; ?>
The output result is:
Welcome to my website!
<?php $name = "John"; echo "Hello " . $name . "!"; ?>
The output result is:
Hello John!
<?php $cars = array("Volvo", "BMW", "Toyota"); echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; ?>
The output result is:
I like Volvo, BMW and Toyota.
<?php class Person { public $name; public $age; function __construct($name, $age) { $this->name = $name; $this->age = $age; } } $person = new Person("John", 30); echo "My name is " . $person->name . " and I am " . $person->age . " years old."; ?>
The output result is:
My name is John and I am 30 years old.
In addition to using single quotes and double quotes to define strings, you can also use Herodoc and Nowdoc syntax to output String. The following is an example of outputting a string using Herodoc and Nowdoc syntax:
<?php $name = "John"; echo <<<EOT My name is $name. I like coding in PHP. EOT; ?>
Note that in Herodoc syntax, it needs to be before the line ending with EOT Add a semicolon.
The output result is:
My name is John. I like coding in PHP.
<?php $name = "John"; echo <<<'EOT' My name is $name. I like coding in PHP. EOT; ?>
In Nowdoc syntax, variables will not be parsed.
The output result is:
My name is $name. I like coding in PHP.
In PHP, the echo statement can also be used to output Boolean values. The following is an example of using echo to output a Boolean value:
<?php $is_true = true; $is_false = false; echo "is_true is $is_true.<br>"; echo "is_false is $is_false."; ?>
The output result is:
is_true is 1. is_false is.
Note that in PHP, true will be interpreted as 1, and false will not output anything.
In addition to regular output statements, the echo statement can also be used to output files to the browser. At this point, you need to set output caching to off to avoid outputting other content before the file is output. The following is an example of an output file:
<?php $file = "example.pdf"; header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="' . basename($file) . '"'); header('Content-Length: ' . filesize($file)); readfile($file); exit; ?>
In this example, the header function is used to set the response header information, the readfile function is used to output the file content, and the exit function is used to terminate the execution of the script.
In short, the echo statement is one of the most commonly used output statements in PHP, which can be used to output text, variables, arrays, objects, Boolean values, etc. in Web pages. If necessary, you can also use Herodoc and Nowdoc syntax to define strings, or output files to the browser by setting response header information and output file content.
The above is the detailed content of Examples of how to use echo to output strings in PHP. For more information, please follow other related articles on the PHP Chinese website!