Home  >  Article  >  Backend Development  >  How to output string to html in php

How to output string to html in php

(*-*)浩
(*-*)浩Original
2019-09-30 11:20:383093browse

Let’s take a look at an example first

How to output string to html in php

Output HTML tag (Recommended learning: PHP video tutorial)

<?php
$name = "张三";
?>
<html>
<head></head>
<body>
<p>你好,<?php echo $name; ?>先生。</p>
</body>
</html>

The output is as follows

你好,张三先生。

The value assigned to the variable $name will be expanded and displayed as part of the HTML.

It is also possible to assign HTML tags to variables and display them.

<?php
$name = "张三";
?>
<html>
<head></head>
<body>
<p>你好,
<?php
$span = "<span style=&#39;color:red&#39;> $name 先生。</span>";
echo $span;
?>
</p>
</body>
</html>

The output results are as follows:

你好,张三先生。

In the above results, Mr. Zhang San will be displayed in red.

The variable $span contains HTML tags. When echo is used to output, the label part is recognized as a normal HTML markup and displayed.

Form processing

By making the target of an HTML form a PHP file, you can use that PHP file to process the data sent from the form.

Create a form using HTML.

<html>
<head></head>
<body>
<form action="form.php" method="post">
  名称: <input type="text" name="name" /><br>
  <input type="submit" />
</form>
</body>
</html>

Fill out this form and press the submit button to send the form data to form.php.

Output data from form

I will output the data sent from the form above.

For data sent using POST, you can get $_POST['Element Name'], and for data sent using GET, you can get $_GET['Element Name'].

Use echo output.

你好,<?php echo $_POST[&#39;name&#39;]; ?>先生。

Enter "Zhang San" in the above form and press the send button, it will be displayed as follows.

你好,张三先生。

The above is the detailed content of How to output string to html in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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