Home > Backend Development > PHP Problem > Examples of how to use echo to output strings in PHP

Examples of how to use echo to output strings in PHP

PHPz
Release: 2023-04-23 10:35:59
Original
1269 people have browsed it

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:

  1. Output a simple string:
<?php
echo "Hello World!";
?>
Copy after login

The output result is:

Hello World!
Copy after login
  1. Output a string containing HTML tags:
<?php
echo "<h1>Welcome to my website!</h1>";
?>
Copy after login

The output result is:

Welcome to my website!
Copy after login
  1. Output a string containing variables:
<?php
$name = "John";
echo "Hello " . $name . "!";
?>
Copy after login

The output result is:

Hello John!
Copy after login
  1. The output string containing the array:
<?php
$cars = array("Volvo", "BMW", "Toyota");
echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";
?>
Copy after login

The output result is:

I like Volvo, BMW and Toyota.
Copy after login
  1. Output a string containing objects:
<?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.";
?>
Copy after login

The output result is:

My name is John and I am 30 years old.
Copy after login

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:

  1. Herodoc syntax:
<?php
$name = "John";
echo <<<EOT
My name is $name. 
I like coding in PHP. 
EOT;
?>
Copy after login

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.
Copy after login
  1. Nowdoc syntax:
<?php
$name = "John";
echo <<<&#39;EOT&#39;
My name is $name. 
I like coding in PHP. 
EOT;
?>
Copy after login

In Nowdoc syntax, variables will not be parsed.

The output result is:

My name is $name. 
I like coding in PHP.
Copy after login

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.";
?>
Copy after login

The output result is:

is_true is 1. 
is_false is.
Copy after login

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(&#39;Content-Type: application/octet-stream&#39;);
header(&#39;Content-Disposition: attachment; filename="&#39; . basename($file) . &#39;"&#39;);
header(&#39;Content-Length: &#39; . filesize($file));
readfile($file);
exit;
?>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template