Home >Backend Development >PHP Tutorial >Your First PHP Code
Quick review of PHP core concepts
A preliminary exploration of PHP: Write your first PHP script
After building a virtual server, let's start your first PHP scripting journey. As a server-side language, PHP may be different from client languages you are familiar with (such as HTML, CSS, JavaScript).
The server-side language is similar to JavaScript, allowing you to embed applets (scripts) into the HTML code of a web page. After executing these programs, you can gain more control over what is displayed in the browser window than using only HTML. The key difference between JavaScript and PHP is the web page loading phase that executes these embedded programs.
Client languages (such as JavaScript) are read and executed by the web browser after downloading web pages (including embedded programs) from the web server. Instead, a server-side language (such as PHP) is run by a Web server server before sending a web page to the browser. The client language allows you to control how the page behaves after the browser displays, while the server language allows you to dynamically generate custom pages before the page is sent to the browser.
After the web server executes the PHP code embedded in the web page, the result will replace the PHP code in the page. When receiving pages, all browsers see standard HTML code, so it is called a "server-side language". Let's look at a simple PHP example that generates a random number between 1 and 10 and displays it on the screen:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>随机数</title> <p>生成1到10之间的随机数:</p> <?php echo rand(1, 10); ?> </code>
Most of the code is pure HTML. Only the lines between <code><?php and <code>?> are PHP code. <code><?php marks the beginning of an embedded PHP script, <code>?> marks its end. The web server is asked to interpret everything between these two delimiters and convert it into regular HTML code before sending it to the web page that requests the browser. If you right-click in your browser and select "View Source Code" (the text may vary depending on the browser you are using), you can see that the browser displays the following:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>随机数</title> <p>生成1到10之间的随机数:</p> <?php echo rand(1, 10); ?> </code>
Please note that all traces of PHP code have disappeared. Instead, the output of the script is what it looks like standard HTML. This example demonstrates several advantages of server-side scripting...
Basic grammar and statements
If you know JavaScript, C, C, C, C#, Objective-C, Java, Perl, or any other C-derived language, then PHP syntax will be very familiar. But if you are not familiar with these languages, or if you are new to programming, you don't need to worry.
PHP script consists of a series of commands or statements. Each statement is a directive that the web server must follow before continuing to execute the next directive. Like the statements in the above languages, PHP statements always end with a semicolon (;).
This is a typical PHP statement:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>随机数</title> <p>生成1到10之间的随机数:</p> 7 </code>
This is an echo statement used to generate content (usually HTML code) to send to the browser. The echo statement simply takes the given text and inserts it into the page HTML code containing its PHP script location.
In this case, we provide a text string to output: this is a test! . Note that the text string contains HTML tags (<code><code><strong></strong> and <code><code>), which is perfectly acceptable.
So if we put this statement into the full webpage, the generated code is as follows:
<code class="language-php">echo '这是一个<strong>测试</strong>!';</code>
If you place this file on a web server and then request it using a web browser, your browser will receive this HTML code:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>测试页面</title> <p><?php echo '这是一个<strong>测试</strong>!'; ?></p> </code>
The random.php example we saw before contains a slightly more complex echo statement:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>随机数</title> <p>生成1到10之间的随机数:</p> <?php echo rand(1, 10); ?> </code>
You will notice that in the first example, PHP is given some direct print text, and in the second example, PHP is given a directive to follow. PHP tries to read anything that exists outside of quotes as directives it must follow. Anything inside quotes is treated as a string, which means PHP doesn't handle it at all, but just pass it to the command you call. Therefore, the following code passes the string "This is a test!" directly to the echo command:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>随机数</title> <p>生成1到10之间的随机数:</p> 7 </code>
Strings are represented by starting and ending quotes. PHP will see the first <code>' as the beginning of the string and find the next <code>' and use it as the end of the string.
Instead, the following code will first run the built-in function rand to generate a random number, and then pass the result to the echo command:
<code class="language-php">echo '这是一个<strong>测试</strong>!';</code>
You can think of built-in functions as tasks that PHP can perform without you specifying. PHP has many built-in functions that allow you to perform everything from sending emails to using information stored in various types of databases.
PHP does not try to understand strings. They can contain any characters, arranged in any order. But the code—essentially a series of instructions—must follow a strict structure in order for the computer to understand it.
When you call a function in PHP—i.e., when you ask it to do its work—you are said to be calling the function. Most functions return values when called; then PHP behaves like you're actually just typing that return value into your code. In the <code>echo 'rand(1, 10)'; example, our echo statement contains a call to the rand function that returns a random number as a text string. Then, the echo statement outputs the value returned by the function call.
Each function in PHP can have one or more parameters that allow you to make the function run in a slightly different way. The rand function takes two parameters: the minimum random number and the maximum random number. By changing the value passed to the function, you can change how it works. For example, if you want a random number between 1 and 50, you can use the following code:
<code class="language-html"><!DOCTYPE html> <meta charset="utf-8"> <title>测试页面</title> <p><?php echo '这是一个<strong>测试</strong>!'; ?></p> </code>
You may be wondering why we need to enclose the parameters in brackets ((1, 50)). Brackets have two functions. First, they mean that rand is a function you want to call. Second, they mark the beginning and end of the parameter list—the PHP statement you wish to provide—to tell the function what you want it to do. For rand function you need to provide the minimum and maximum values. These values are separated by commas.
Later, we will look at functions that take different types of parameters. We will also consider functions that do not take any parameters at all. Even if there is nothing between them, these functions still require parentheses.
(The following is the FAQ part. Due to space limitations, I will only keep the question and a brief summary of the answers. Please refer to the original text for the complete FAQ answer.)
PHP Code FAQ (FAQ)
What is the meaning of PHP delimiter? <code><?php and <code>?> are used to mark the beginning and end of the PHP code block, and the server parses the PHP code accordingly.
How to write my first PHP program? Create a <code>.php file and write PHP code inside <code><?php ?>, such as <code>echo "Hello, World!";.
What is the role of PHP in web development? PHP is used to create dynamic interactive web pages, process databases, user sessions, forms, etc.
How does PHP interact with HTML? PHP code is embedded in HTML <code><?php ?>, and the server outputs the result as HTML after processing.
What are the common mistakes that beginners in PHP code make? Forgot to close strings or brackets, improper use of semicolons, wrong function syntax, etc.
How to debug PHP code? Use <code>echo or <code>print to output variable values, or use debugging tools such as Xdebug.
How to protect PHP code security? Verify and clean user input, use secure hashing algorithm to store passwords, update PHP version, use HTTPS connections, etc.
Can PHP be used with other programming languages? Can be, for example, with HTML, CSS, JavaScript and SQL databases.
How to improve the performance of PHP code? Use efficient algorithms and data structures, minimize database queries, use caching technology, etc.
What are the resources to learn PHP? PHP official website (php.net), online courses, books and tutorials, etc.
The above is the detailed content of Your First PHP Code. For more information, please follow other related articles on the PHP Chinese website!