Home > Backend Development > PHP Tutorial > Detailed explanation and example analysis of PHP writing format

Detailed explanation and example analysis of PHP writing format

墨辰丷
Release: 2023-03-29 12:58:01
Original
1677 people have browsed it

This article mainly introduces the detailed explanation and example analysis of PHP writing format. Interested friends can refer to it. I hope it will be helpful to everyone.

Start with an example.

Start the editor, create a php file and type the following code:

<?php 
echo "你好!"; 
?>
Copy after login

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:

Hello! In this example, we use the echo command to output a string "Hello!".

As can be seen from this example:

•The PHP file or PHP code segment starts with "" ends with PHP code in the middle

•Each instruction set ends with; symbol

•What the browser gets is the result after the PHP parser executes the PHP code. Website viewers The original code of the php file cannot be viewed

Of course, to output standard html code, the above example can be improved to:

 
  
 PHP 测试 
  
  
 

<?php echo "你好!"; ?>

Copy after login

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.

Let’s introduce the output function of php.

PHP echo and print statements

The difference between echo and print:

•echo - Yes Output one or more strings

•print - Only one string is allowed to be output, and the return value is always 1

Tip: echo outputs faster than print, echo has no return value, and print There is a return value of 1.

PHP echo statement

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&#39;m about to learn PHP!<br>"; 
echo "This", " string", " was", " made", " with multiple parameters."; 
?>
Copy after login

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

PHP print statement

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&#39;m about to learn PHP!"; 
?>
Copy after login

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

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP uses the PDO abstraction layer to obtain query results

PHP compresses image size and converts it to jpg format Method example

PHP Method example of remembering status when searching

The above is the detailed content of Detailed explanation and example analysis of PHP writing format. 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