python编程初学者指南_学习PHP-PHP编程初学者指南

python编程初学者指南

基本PHP语法 ( Basic PHP Syntax )

PHP is a server side scripting language used on the Internet to create dynamic web pages. It is often coupled with MySQL, a relational database server that can store the information and variables the PHP files may use. Together they can create everything from the simplest web site to a full blown business web site, an interactive web forum, or even an online role playing game.

PHP是一种服务器端脚本语言,在Internet上用于创建动态网页。 它通常与关系数据库服务器MySQL结合使用,该数据库服务器可以存储PHP文件可能使用的信息和变量。 他们可以共同创建从最简单的网站到功能完善的商务网站,交互式网站论坛甚至在线角色扮演游戏的所有内容。

Before we can do the big fancy stuff we must first learn the basics from which we build on.

在开始做大手笔的东西之前,我们必须首先学习基础知识。

  1. Start by creating a blank file using any program that can save in plain text format.

    首先使用可以以纯文本格式保存的任何程序创建一个空白文件。
  2. Save your file as a .PHP file, for example mypage.php. Saving a page with the .php extension tells your server that it will need to execute the PHP code.

    将文件另存为.PHP文件 ,例如mypage.php。 保存带有.php扩展名的页面会告诉您的服务器它将需要执行PHP代码。

  3. Enter the statement to let the server know that there is PHP code coming up.

    输入语句以使服务器知道即将出现PHP代码。

  4. After this we would enter the body of our PHP program.

    之后,我们将进入我们PHP程序的主体。

  5. Enter the statement ?> to let the browser know the PHP code is done.

    输入语句?>,以使浏览器知道PHP代码已完成。

Every section of PHP code starts and ends by turning on and off PHP tags to let the server know that it needs to execute the PHP in between them. Here is an example:

PHP代码的每个部分的开头和结尾都是打开和关闭PHP标记,以使服务器知道它们之间需要执行PHP。 这是一个例子:


 //on

//and

//and

//off ?>

//off ?>

Everything between the is read as PHP code. The statement can also be phrased as simply if desired. Anything outside of these PHP tags is read as HTML, so you can easily switch between PHP and HTML as needed. This will come in handy later in our lessons.

之间的所有内容均读取为PHP代码。 如果需要,语句也可以用简单的措词表达 这些PHP标记之外的所有内容都将读取为HTML,因此您可以根据需要轻松地在PHP和HTML之间切换。 这将在稍后的课程中派上用场。

Comments

//A comment on a single line

#Another single line comment

/* Using this method you can create a larger block of text and it will all be commented out */

?>

One reason you may want to put a comment in your code is to make a note to yourself about what the code is doing for reference when you edit it later. You may also want to put comments in your code if you plan on sharing it with others and want them to understand what it does, or to include your name and terms of use within the script.

//单行注释

#另一个单行注释

/ *使用此方法,您可以创建更大的文本块,所有文本都将被注释掉* /

?>

您可能想在代码中添加注释的原因之一是要对自己做个注释,以供以后编辑时参考该代码的参考。 如果您打算与他人共享代码,并希望他们了解其功能,或者希望在脚本中包含您的姓名和使用条款,则可能还需要在代码中添加注释。

PRINT和ECHO语句 ( PRINT and ECHO Statements )

First we are going to learn about the echo statement, the most basic statement in PHP. What this does is output whatever you tell it to echo. For example:

首先,我们将学习echo语句,这是PHP中最基本的语句。 无论您告诉它如何回显,这都会输出。 例如:

This would return the statement I like About. Notice when we echo a statement, it is contained within quotation marks [“”].

这将返回我喜欢About的语句。 注意,当我们回显一条语句时,该语句包含在引号[“â€?]中。

Another way to do this is to use the print function. An example of that would be:

另一种方法是使用打印功能。 一个例子是:

There is a lot of debate about which is better to use or if there is any difference at all. Apparently in very large programs that are simply outputting text the ECHO statement will run slightly faster, but for the purposes of a beginner they are interchangeable.

关于使用哪种更好或根本没有区别存在很多争论。 显然,在仅输出文本的超大型程序中, ECHO语句的运行速度会稍快一些,但对于初学者而言,它们是可以互换的。

Another thing to keep in mind is that all of your print/echoing is contained between quotation marks. If you want to use a quotation mark inside of the code, you must use a backslash:

要记住的另一件事是,所有打印/回显都包含在引号之间。 如果要在代码内部使用引号,则必须使用反斜杠:

\"I like About too\"" ?>
PHP Test Page
";
print "Billy said \"I like About too\""
?>

As you can see, you can insert HTML right into your php print line. You can format the HTML in the rest of the document as you please, but remember to save it as a .php file.

如您所见,您可以在HTML打印行中插入HTML。 您可以随意在文档的其余部分中设置HTML格式,但请记住将其另存为.php文件。

Do you use PRINT or ECHO? Share your answer!

您使用PRINT还是ECHO? 分享您的答案!

变数 ( Variables )

The next basic thing you need to learn how to do is to set a variable. A variable is something that represents another value.

您需要学习的下一个基本操作是设置变量。 变量是代表另一个值的东西。

This sets our variable, $like, to our previous I like About statement. Notice again the quotation marks [“”] used, as well as the semicolon [;] to show the end of the statement. The second variable $num is an integer and therefore does not use the quotation marks. The next line prints out the variable $like and $num respectively. You can print more than one variable on a line using a period [.], for example:

这会将我们的变量$ like设置为我们之前的About语句。 再次注意使用引号[?]以及分号[;]来显示语句的结尾。 第二个变量$ num是一个整数,因此不使用引号。 下一行分别打印出变量$ like和$ num。 您可以使用句点[。]在一行上打印多个变量,例如:

";
print $like . " " . $num;
print "

"; print "My favorite number is $num"; ?>

"; print "My favorite number is $num"; ?>

This shows two examples of printing more than one thing. The first print line prints the $like and $num variables, with the period [.] to separate them. The third print line prints the $like a variable, a blank space, and the $num variable, all separated by periods. The fifth line also demonstrates how a variable can be used within the quotation marks [""].

这显示了两个打印多个项目的示例。 第一行打印$ like和$ num变量,并用句点[。]隔开。 第三行打印$ like变量,空格和$ num变量,均用句点分隔。 第五行还演示了如何在引号[“”]中使用变量。

A few things to remember when working with variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.) Also, note that if needed is possible to dynamically build variables. 

使用变量时要记住的一些事情:它们是CaSe SeNsitiVe,它们始终以$定义,并且它们必须以字母或下划线(而不是数字)开头。此外,请注意,如有可能,可以动态生成变量。

Arrays

$age["Justin"] = 45; $age["Lloyd"] = 32; $age["Alexa"] = 26; $age["Devron"] = 15;

print "My friends names are " . $friend[0] . ", " . $friend[1] . ", " . $friend[2] . ", and " . $friend[3];

print "

";

print "Alexa is " . $age["Alexa"] . " years old"; ?>

The first array ($friend) is arranged using integers as the key (the key is the information between the [brackets]) which is handy when using loops. The second array ($age) shows that you can also use a string (text) as the key. As demonstrated the values are called by print in the same way a regular variable would be.

The same principals apply to arrays as variables: they are CaSe SeNsitiVe, they are always defined with a $, and they must start with a letter or an underscore (not a number.)

$ age [“ Justin”] = 45; $ age [“ Lloyd”] = 32; $ age [“ Alexa”] = 26; $ age [“ Devron”] = 15;

打印“我的朋友的名字是”。 $ friend [0]。 “,”。 $ friend [1]。 “,”。 $ friend [2]。 “和”。 $ friend [3];

打印“

“;

打印“ Alexa是”。 $ age [“ Alexa”]。 “岁”; ?>

第一个数组($ friend)使用整数作为键(键是[括号之间的信息])进行排列,这在使用循环时很方便。 第二个数组($ age)显示您还可以使用字符串(文本)作为键。 如演示的那样,值通过print调用的方式与常规变量相同。

相同的原理作为变量应用于数组:它们是CaSe SeNsitiVe,它们始终以$定义,并且它们必须以字母或下划线(而不是数字)开头。

操作数 ( Operands )

You have probably all heard the term expression used in mathematics. We use expressions in PHP to preform operations and give an answer to a single value. These expressions are made up of two parts, the operators and the operands. The operands can be variables, numbers, strings, boolean values, or other expressions. Here is an example:

大家可能都听说过数学中使用的术语“表达”。 我们使用PHP中的表达式执行操作并给出单个值的答案。 这些表达式由两部分组成: 运算符操作数 。 操作数可以是变量,数字,字符串,布尔值或其他表达式。 这是一个例子:

a = 3 + 4

a = 3 + 4

In this expression the operands are a, 3 and 4

在此表达式中,操作数为a,3和4

b = (3 + 4) / 2

b = (3 + 4) / 2

In this expression the expression (3+4) is used as an operand along with b and 2.

在此表达式中,表达式(3 + 4)与b和2一起用作操作数。

经营者 ( Operators )

Now that you understand what an operand is we can go into more detail about what operators are. Operators tell us what to do with operands, and they fall into three major categories:

既然您了解了什么是操作数 ,我们就可以更详细地了解什么是运算符 。 运算符告诉我们如何处理操作数,它们分为三大类:

Mathematical:+(plus), - (minus), / (divided by), and * (multiplied by)

数学: +(加号),-(减号),/(除以)和*(乘以)

Comparison:> (greater than), < (less than), == (equal to), and != (not equal to)

比较: >(大于),<(小于),==(等于)和!=(不等于)

Boolean:&& (true if both operands are true), || (true if at least one operand is true), xor (true if ONLY one operand is true), and ! (true if a single operand is false)

布尔值: &&(如果两个操作数均为true,则为true),|| (如果至少一个操作数为true,则为true),xor(如果只有一个操作数为true,则为true)和! (如果单个操作数为false,则为true)

Mathematical operators are exactly what they are called, they apply mathematical functions to the operands. Comparison is also pretty straight forward, they compare one operand to another operand. Boolean however may need a little more explaining.

数学运算符正是他们所称的名称,它们将数学函数应用于操作数。 比较也很简单,他们将一个操作数与另一个操作数进行比较。 但是,布尔值可能需要更多说明。

Boolean is an extremely simple form of logic. In Boolean every statement is either True or False. Think of a light switch, it must either be turned on or off, there is no in between. Let me give you an example:

布尔是一种非常简单的逻辑形式。 在布尔值中,每个语句都是True或False。 想想电灯开关,它必须是打开或关闭的,两者之间没有开关。 让我举一个例子:

$a = true;$b = true;$c = false;

$ a = true; $ b = true; $ c = false;

$a && $b;This is asking for $a and $b to both be true, since they are both true, this expression is TRUE

$ a && $ b;这要求$ a和$ b都为真,因为它们都是真,所以此表达式为TRUE

$a || $b;This is asking for $a or $b to be true. Again this is a TRUE expression

$ a || $ b;这要求$ a或$ b为真。 同样,这是一个TRUE表达式

$a xor $b;This is asking for $a or $b, but not both, to be true. Since they are both true, this expression is FALSE

$ a xor $ b;这是要求$ a或$ b,但不能同时要求两个。 由于它们都是真实的,因此此表达式为FALSE

! $a;This is asking for $a to be false. Since $a is true, this expression is FALSE

! $ a;这要求$ a为假。 由于$ a为真,因此此表达式为FALSE

! $c;This is asking for $c to be false. Since that is the case, this expression is TRUE

! $ c;这要求$ c为假。 既然是这种情况,则此表达式为TRUE

条件语句 ( Conditional Statements )

Conditionals allow your program to make choices. Following the same sort of boolean logic you just learned about, the computer can only make two choices; true or false. In the case of PHP this is accomplished using IF : ELSE statements. Below is an example of an IF statement that would apply a senior's discount. If $over65 is false, everything within the {brackets} is simply ignored.

条件允许您的程序做出选择。 按照您刚刚了解的相同的布尔逻辑,计算机只能做出两种选择。 对或错。 对于PHP,这是使用IF:ELSE语句完成的。 以下是将采用老年人折扣的IF语句示例。 如果$ over65为false,则{brackets}中的所有内容都将被忽略。

However, sometimes just the IF statement isn't enough, you need the ELSE statement as well. When using just the IF statement the code within the brackets either will (true) or will not (false) be executed before carrying on with the rest of the program. When we add in the ELSE statement, if the statement is true it will execute the first set of code and if it is false it will execute the second (ELSE) set of code. Here is an example:

但是,有时仅IF语句还不够,您还需要ELSE语句。 如果仅使用IF语句,则在继续执行程序的其余部分之前,将执行括号内的代码(true)或不执行(false)。 当我们添加ELSE语句时,如果该语句为true,则将执行第一组代码;如果为false,则将执行第二(ELSE)组代码。 这是一个例子:

嵌套条件 ( Nested Conditionals )

One useful thing to remember about conditional statements is that they can be nested within each other. Below is an example of how the discount program from our example could be written to use nested IF:ELSE statements. There are other ways of doing this - such as using elseif () or switch () but this demonstrates how statements can be nested.

关于条件语句,要记住的一件有用的事情是它们可以相互嵌套。 下面是一个示例,说明如何编写示例中的折扣程序以使用嵌套的IF:ELSE语句。 还有其他方法可以做到这一点-例如使用elseif()或switch(),但这演示了如何嵌套语句。

65)
{
$discount =.90;
print "You have received our senior's discount, your price is $" . $price*$discount;
}
else
{
if ($age

This program will first check if they are eligible for the senior's discount. If they are not, it will then check if they are eligible for a student discount, before returning the non-discounted price.

该计划将首先检查他们是否有资格享受老年人的折扣。 如果不是,那么它将在返回非折扣价格之前检查他们是否有资格获得学生折扣。

翻译自: https://www.thoughtco.com/beginners-guide-to-php-programing-2693961

python编程初学者指南

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值