第一个代码_您的第一个PHP代码

第一个代码

The following is a short extract from our new book, PHP & MySQL: Novice to Ninja, 6th Edition, written by Tom Butler and Kevin Yank. It’s the ultimate beginner’s guide to PHP. SitePoint Premium members get access with their membership, or you can buy a copy in stores worldwide.

以下是汤姆·巴特勒(Tom Butler)和凯文·扬克(Kevin Yank)撰写的新书《 PHP和MySQL:从新手到忍者》第六版的简短摘录。 这是PHP的终极入门指南。 SitePoint Premium成员可以使用其成员身份进行访问,或者您可以在世界各地的商店中购买副本。

Now that you have your virtual server up and running, it’s time to write your first PHP script. PHP is a server-side language. This concept may be a little difficult to grasp, especially if you’ve only ever designed websites using client-side languages like HTML, CSS, and JavaScript.

现在您已经启动并运行了虚拟服务器,是时候编写第一个PHP脚本了。 PHP是一种服务器端语言。 可能很难理解这个概念,特别是如果您仅使用HTML,CSS和JavaScript等客户端语言设计网站。

A server-side language is similar to JavaScript in that it allows you to embed little programs (scripts) into the HTML code of a web page. When executed, these programs give you greater control over what appears in the browser window than HTML alone can provide. The key difference between JavaScript and PHP is the stage of loading the web page at which these embedded programs are executed.

服务器端语言类似于JavaScript,因为它允许您将小的程序(脚本)嵌入到网页HTML代码中。 执行这些程序后,与单独HTML相比,这些程序使您可以更好地控制浏览器窗口中显示的内容。 JavaScript和PHP之间的主要区别在于加载网页的阶段,在这些网页上执行这些嵌入式程序。

Client-side languages like JavaScript are read and executed by the web browser after downloading the web page (embedded programs and all) from the web server. In contrast, server-side languages like PHP are run by the web server, before sending the web page to the browser. Whereas client-side languages give you control over how a page behaves once it’s displayed by the browser, server-side languages let you generate customized pages on the fly before they’re even sent to the browser.

从Web服务器下载网页(嵌入式程序和全部)后,Web浏览器将读取并执行JavaScript之类的客户端语言。 相反,在将网页发送到浏览器之前,Web 服务器运行诸如PHP之类的服务器端语言。 客户端语言可让您控制页面在浏览器显示后的行为,而服务器端语言可让您在将自定义页面发送到浏览器之前即时生成自定义页面。

Once the web server has executed the PHP code embedded in a web page, the result takes the place of the PHP code in the page. All the browser sees is standard HTML code when it receives the page, hence the name “server-side language.” Let’s look at simple example of some PHP that generates a random number between 1 and 10 and then displays it on the screen:

Web服务器执行了嵌入在网页中PHP代码后,结果将替换页面中PHP代码。 浏览器在接收页面时所看到的只是标准HTML代码,因此名称为“服务器端语言”。 让我们看一些简单PHP示例,该示例生成1到10之间的随机数,然后将其显示在屏幕上:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Random Number</title>
    </head>
    <body>
        <p>Generating a random number between 1 and 10:
            <?php

            echo rand(1, 10);

            ?>
        </p>
    </body>
</html>

Most of this is plain HTML. Only the line between <?php and ?> is PHP code. <?php marks the start of an embedded PHP script and ?> marks its end. The web server is asked to interpret everything between these two delimiters and convert it to regular HTML code before it sends the web page to the requesting browser. If you right-click inside your browser and choose View Source (the text may be different depending on the browser you’re using) you can see that the browser is presented with the following:

其中大多数是纯HTML。 只有<?php?>之间的行是PHP代码。 <?php表示嵌入式PHP脚本的开始,而?>表示结束。 在将网页发送到请求的浏览器之前,要求网络服务器解释这两个定界符之间的所有内容并将其转换为常规HTML代码。 如果在浏览器内部单击鼠标右键,然后选择“ 查看源代码” (文本可能会有所不同,具体取决于您使用的浏览器),则可以看到该浏览器显示以下内容:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Random Number</title>
    </head>
    <body>
        <p>Generating a random number between 1 and 10:
            5
        </p>
    </body>
</html>

Notice that all signs of the PHP code have disappeared. In its place the output of the script has appeared, and it looks just like standard HTML. This example demonstrates several advantages of server-side scripting …

请注意,PHP代码的所有迹象均已消失。 脚本的输出已出现在它的位置,看起来就像标准HTML。 这个例子展示了服务器端脚本的几个优点……

  • No browser compatibility issues. PHP scripts are interpreted by the web server alone, so there’s no need to worry about whether the language features you’re using are supported by the visitor’s browser.

    没有浏览器兼容性问题 。 PHP脚本仅由Web服务器解释,因此无需担心访问者的浏览器是否支持您所使用的语言功能。

  • Access to server-side resources. In the example above, we placed a random number generated by the web server into the web page. If we had inserted the number using JavaScript, the number would be generated in the browser and someone could potentially amend the code to insert a specific number. Granted, there are more impressive examples of the exploitation of server-side resources, such as inserting content pulled out of a MySQL database.

    访问服务器端资源 。 在上面的示例中,我们将Web服务器生成的随机数放入了网页中。 如果我们使用JavaScript插入了数字,则该数字将在浏览器中生成,并且有人可能会修改代码以插入特定的数字。 当然,还有更多利用服务器端资源的令人印象深刻的示例,例如插入从MySQL数据库中提取的内容。

  • Reduced load on the client. JavaScript can delay the display of a web page significantly (especially on mobile devices!) as the browser must run the script before it can display the web page. With server-side code, this burden is passed to the web server, which you can make as beefy as your application requires (and your wallet can afford).

    减轻了客户端的负担 。 JavaScript可能会严重延迟网页的显示(尤其是在移动设备上!),因为浏览器必须先运行脚本才能显示网页。 通过服务器端代码,此负担将传递到Web服务器,您可以根据应用程序的需求(并且您的钱包负担得起)使它变得更加强大。

  • Choice. When writing code that’s run in the browser, the browser has to understand how to run the code given to it. All modern browsers understand HTML, CSS and JavaScript. To write some code that’s run in the browser, you must use one of these languages. By running code on the server that generates HTML, you have a choice of many languages—one of which is PHP.

    选择 。 编写在浏览器中运行的代码时,浏览器必须了解如何运行提供给它的代码。 所有现代浏览器都了解HTML,CSS和JavaScript。 要编写一些在浏览器中运行的代码,您必须使用以下一种语言。 通过在生成HTML的服务器上运行代码,您可以选择多种语言,其中一种是PHP。

基本语法和语句 (Basic Syntax and Statements)

PHP syntax will be very familiar to anyone with an understanding of JavaScript, C, C++, C#, Objective-C, Java, Perl, or any other C-derived language. But if these languages are unfamiliar to you, or if you’re new to programming in general, there’s no need to worry about it.

了解JavaScript,C,C ++,C#,Objective-C,Java,Perl或任何其他C衍生语言的人都将非常熟悉PHP语法。 但是,如果您不熟悉这些语言,或者如果您一般不熟悉编程,则无需担心。

A PHP script consists of a series of commands, or statements. Each statement is an instruction that must be followed by the web server before it can proceed to the next instruction. PHP statements, like those in the aforementioned languages, are always terminated by a semicolon (;).

PHP脚本由一系列命令或语句组成。 每个语句都是Web服务器必须遵循的一条指令,然后它才能继续执行下一条指令。 与上述语言一样,PHP语句始终以分号( ; )终止。

This is a typical PHP statement:

这是一个典型PHP语句:

echo 'This is a <strong>test</strong>!';

This is an echo statement, which is used to generate content (usually HTML code) to send to the browser. An echo statement simply takes the text it’s given and inserts it into the page’s HTML code at the position of the PHP script where it was contained.

这是一个echo语句,用于生成要发送到浏览器的内容(通常是HTML代码)。 echo语句只是获取给出的文本,然后将其插入到包含HTML脚本的页面HTML代码中。

In this case, we’ve supplied a string of text to be output: This is a <strong>test</strong>!. Notice that the string of text contains HTML tags (<strong> and </strong>), which is perfectly acceptable.

在这种情况下,我们提供了要输出的文本字符串: This is a <strong>test</strong>! 。 请注意,文本字符串包含HTML标记( <strong></strong> ),这是完全可以接受的。

So, if we take this statement and put it into a complete web page, here’s the resulting code:

因此,如果我们将此语句放入完整的网页中,则结果代码如下:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test page</title>
    </head>
    <body>
        <p><?php echo 'This is a <strong>test</strong>!'; ?></p>
    </body>
</html>

If you place this file on your web server and then request it using a web browser, your browser will receive this HTML code:

如果将此文件放置在Web服务器上,然后使用Web浏览器进行请求,则浏览器将收到以下HTML代码:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Test page</title>
    </head>
    <body>
        <p>This is a <strong>test</strong>!</p>
    </body>
</html>

The random.php example we looked at earlier contained a slightly more complex echo statement:

我们前面看过的random.php示例包含一个稍微复杂一些的echo语句:

echo rand(1, 10);

You’ll notice that, in the first example, PHP is given some text to print directly, and in the second, PHP is given an instruction to follow. PHP tries to read anything that exists outside quotes as an instruction it must follow. Anything inside quotes is treated as a string, which means PHP doesn’t process it at all but just passes it to the command you called. So the following code will pass the string This is a <strong>test</strong>! directly to the echo command:

您会注意到,在第一个示例中,为PHP提供了一些可直接打印的文本,在第二个示例中,为PHP提供了遵循的指令。 PHP尝试读取引号之外的任何内容,作为它必须遵循的指令。 引号内的所有内容均视为字符串 ,这意味着PHP根本不处理它,而只是将其传递给您调用的命令。 因此,以下代码将通过字符串“ This is a <strong>test</strong>! 直接使用echo命令:

echo 'This is a <strong>test</strong>!';

A string is signified using a start quote and an end quote. PHP will see the first ' as the start of the string and find the next ' and use that as the end of the string.

使用开始引号和结束引号表示字符串。 PHP将第一个'作为字符串的开头,然后查找下一个'并将其用作字符串的结尾。

In contrast, 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:

相反,以下代码将首先运行内置函数rand来生成随机数,然后将结果传递给echo命令:

echo rand(1, 10);

You can think of built-in functions as tasks that PHP knows how to do without you needing to spell out the details. PHP has many built-in functions that let you do everything, from sending email to working with information stored in various types of databases.

您可以将内置函数视为PHP知道如何执行的任务,而无需详细说明。 PHP具有许多内置函数,可让您做所有事情,从发送电子邮件到处理存储在各种类型数据库中的信息。

PHP won’t try to run anything that’s inside a string. The following code won’t have the result you may be expecting:

PHP不会尝试运行字符串中的任何内容。 以下代码不会得到您期望的结果:

echo 'rand(1, 10)';

Instead of running the inbuilt function rand, PHP will see it as a string, and rather than printing out a random number, it will actually send the text rand(1, 10) to the browser, which probably isn’t what you wanted to do. It’s important to understand the difference between a string and code. PHP will see any text outside quotes as a series of commands it should follow. Anything inside quotes is a string and is data that PHP will work with.

PHP不会运行内置函数rand ,而是将其视为字符串,而不是输出随机数,而是将文本rand(1, 10)发送到浏览器,这可能不是您想要的做。 了解字符串和代码之间的区别很重要。 PHP会将引号以外的任何文本视为应遵循的一系列命令。 里面引号里的东西是一个字符串,是PHP会与数据

PHP doesn’t try to understand strings. They can contain any characters in any order. But code—which is essentially a series of instructions—must follow a rigid structure for a computer to understand it.

PHP不会尝试理解字符串。 它们可以按任何顺序包含任何字符。 但是代码(本质上是一系列指令)必须遵循严格的结构,计算机才能理解。

When you invoke a function in PHP—that is, ask it to do its job—you’re said to be calling that function. Most functions return a value when they’re called; PHP then behaves as if you’d actually just typed that returned value in your code instead. In the echo 'rand(1, 10)'; example, our echo statement contains a call to the rand function, which returns a random number as a string of text. The echo statement then outputs the value returned by the function call.

当您在PHP中调用一个函数(即要求它完成其工作)时,您会说正在调用该函数。 大多数函数在被调用时都会返回一个值。 然后,PHP的行为就好像您实际上只是在代码中键入了返回的值一样。 在echo 'rand(1, 10)'; 例如,我们的echo语句包含对rand函数的调用,该函数返回一个随机数作为文本字符串。 然后, echo语句输出函数调用返回的值。

Every function in PHP can have one or more arguments that allow you to make the function behave in a slightly different way. The rand function takes two arguments: the minimum random number and the maximum. By changing the values that are passed to the function, you’re able to change the way it works. For example, if you wanted a random number between 1 and 50, you could use the code:

PHP中的每个函数都可以具有一个或多个参数 ,使您可以使函数的行为略有不同。 rand函数采用两个参数:最小随机数和最大随机数。 通过更改传递给函数的值,您可以更改其工作方式。 例如,如果您想要一个介于1到50之间的随机数,则可以使用以下代码:

echo rand(1, 50);

You may wonder why we need to surround the arguments with parentheses ((1, 50)). The parentheses serve two purposes. First, they indicate that rand is a function that you want to call. Second, they mark the beginning and end of a list of arguments—PHP statements that you wish to provide—in order to tell the function what you want it to do. In the case of the rand function, you need to provide a minimum and a maximum value. Those values are separated by a comma.

您可能想知道为什么我们需要用括号( (1, 50) )括住参数。 括号有两个目的。 首先,它们表明rand是您要调用的函数。 其次,它们标记了参数列表(您希望提供PHP语句)的开头和结尾,以便告诉函数您想要它做什么。 对于rand函数,您需要提供一个最小值和一个最大值。 这些值用逗号分隔。

Later on, we’ll look at functions that take different kinds of arguments. We’ll also consider functions that take no arguments at all. These functions will still need the parentheses, even though there will be nothing to type between them.

稍后,我们将研究采用不同类型参数的函数。 我们还将考虑根本不带参数的函数。 这些函数将仍然需要括号,即使在它们之间没有任何可键入的内容。

翻译自: https://www.sitepoint.com/first-php-code/

第一个代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值