Example explains that php outputs strings to HTML

  • 2021-11-13 06:58:11
  • OfStack

Let's look at an example first

Output HTML tag


<?php
$name = " Zhang 3";
?>
<html>
<head></head>
<body>
<p> Hello, <?php echo $name; ?> Sir. </p>
</body>
</html>

The output is as follows


 Hello, Zhang 3 Sir. 

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

You can also assign HTML tags to variables and display them.


<?php
$name = " Zhang 3";
?>
<html>
<head></head>
<body>
<p> Hello, 
<?php
$span = "<span style='color:red'> $name  Sir. </span>";
echo $span;
?>
</p>
</body>
</html>

The output is as follows:


 Hello, Zhang 3 Sir. 

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

The variable $span contains the HTML tag. With echo output, the part of the tag is identified as a normal HTML tag and displayed.

Table processing

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

Create a form with HTML.


<html>
<head></head>
<body>
<form action="form.php" method="post">
   Name : <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.

Outputting data from a form

I will output the data sent from the table 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.


 Hello, <?php echo $_POST['name']; ?> Sir. 

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


 Hello, Zhang 3 Sir. 


Related articles: