《经典PHP教程》

基本的 PHP 语法

<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>

PHP 中的注释

<html>
<body>
<?php
//This is a comment
/*
This is
a comment
block
*/
?>
</body>
</html>

PHP 变量

<?php
$txt = "Hello World!";
$number = 16;
?>

变量的命名规则
· 变量名必须以字母或下划线 “_” 开头。
· 变量名只能包含字母数字字符以及下划线。
· 变量名不能包含空格。如果变量名由多个单词组成,那么应该使用下划线进行分隔
(比如 mystring myString)。

并置运算

<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>

使用 strlen() 函数
strlen() 函数用于计算字符串的长度。

<?php
echo strlen("Hello world!");
?>

使用 strpos() 函数
strpos() 函数用于在字符串内检索一段字符串或一个字符。

<?php
echo strpos("Hello world!","world");
?>

以上代码的输出是:
6
正如您看到的,在我们的字符串中,字符串 “world” 的位置是 6。返回 6 而不是 7,是由于
字符串中的首个位置的 0,而不是 1。

PHP 运算符
本部分列出了在 PHP 中使用的各种运算符:
算数运算符
运算符说明例子结果
+ Addition x=2
x+2 4
- Subtraction x=2
5-x 3
* Multiplication x=4
x*5 20
/ Division 15/5
5/2
3
2.5
% Modulus (division remainder)
5%2
10%8
10%2
120
++ Increment x=5
x++ x=6
– Decrement x=5
x– x=4
赋值运算符
运算符说明例子
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
= x=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y
比较运算符
运算符说明例子
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true

is greater than 5>8 returns false
< is less than 5<8 returns true
= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true
逻辑运算符
运算符说明例子
&& and
x=6
y=3
(x < 10 && y > 1) returns true
|| or
x=6
y=3
(x==5 || y==5) returns false
! not
x=6
y=3
!(x==y) returns true

If…Else 语句

if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>

ElseIf 语句

if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>

Switch 语句

switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>

数值数组

例子 1
在这个例子中,会自动分配 ID 键:
$names = array(“Peter”,”Quagmire”,”Joe”);

例子 2
在这个例子中,我们人工分配的 ID 键:
names[0]=Peter; names[1] = “Quagmire”;
$names[2] = “Joe”;
可以在脚本中使用这些 ID 键:

<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors";
?>

以上代码的输出:
Quagmire and Joe are Peter’s neighbors

关联数组

例子 1
在本例中,我们使用一个数组把年龄分配给不同的人:
$ages = array(“Peter”=>32, “Quagmire”=>30, “Joe”=>34);

例子 2
本例与例子 1 相同,不过展示了另一种创建数组的方法:
ages[Peter]=32; ages[‘Quagmire’] = “30”;
$ages[‘Joe’] = “34”;
可以在脚本中使用 ID 键:

<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is " . $ages['Peter'] . " years old.";
?>

以上脚本的输出:
Peter is 32 years old.

多维数组
例子 1
在本例中,我们创建了一个带有自动分配的 ID 键的多维数组:
families=array(Griffin=>array(Peter,Lois,Megan),Quagmire=>array(Glenn),Brown=>array(Cleveland,Loretta,Junior));Array([Griffin]=>Array([0]=>Peter[1]=>Lois[2]=>Megan)[Quagmire]=>Array([0]=>Glenn)[Brown]=>Array([0]=>Cleveland[1]=>Loretta[2]=>Junior))2echoIs. families[‘Griffin’][2] .
” a part of the Griffin family?”;
以上代码的输出:
Is Megan a part of the Griffin family?

循环
while
只要指定的条件成立,则循环执行代码块
do…while
首先执行一次代码块,然后在指定的条件成立时重复这个循环
for
循环执行代码块指定的次数
foreach
根据数组中每个元素来循环代码块

while 语句

<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>

do…while 语句

<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>

for 语句

注释:for 语句有三个参数。第一个参数初始化变量,第二个参数保存条件,第三个参数包
含执行循环所需的增量。如果 initialization 或 increment 参数中包括了多个变量,需要用逗
号进行分隔。而条件必须计算为 true 或者 false。

<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>

foreach 语句

<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>

创建 PHP 函数:
1. 所有的函数都使用关键词 “function()” 来开始
2. 命名函数 - 函数的名称应该提示出它的功能。函数名称以字母或下划线开头。
3. 添加 “{” - 开口的花括号之后的部分是函数的代码。
4. 插入函数代码
5. 添加一个 “}” - 函数通过关闭花括号来结束。

<html>
<body>
<?php
function writeMyName()
{
echo "David Yang";
}
writeMyName();
?>
</body>
</html>
<html>
<body>
<?php
function writeMyName()
{
echo "David Yang";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";

PHP 函数 - 添加参数

<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Yang.<br />";
}
echo "My name is ";
writeMyName("David");
echo "My name is ";
writeMyName("Mike");
echo "My name is ";
writeMyName("John");
?>
</body>
</html>
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Yang" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("David",".");
echo "My name is ";
writeMyName("Mike","!");
echo "My name is ";
writeMyName("John","...");
?>
</body>
</html>

PHP 函数 - 返回值

<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>

PHP 表单和用户输入

<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>

表单验证
$_GET 变量
例子

<form action="welcome.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>

当用户点击提交按钮时,发送的 URL 会类似这样:
http://www.w3school.com.cn/welcome.php?name=Peter&age=37

“welcome.php” 文件现在可以通过 GET _GET 数组中的 ID 键):

Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

为什么使用 GET使 _GET 变量时,所有的变量名和值都会显示在 URL 中。所以在发送密码或
其他敏感信息时,不应该使用这个方法。不过,正因为变量显示在 URL 中,因此可以在收
藏夹中收藏该页面。在某些情况下,这是很有用的。
注释:HTTP GET 方法不适合大型的变量值;值是不能超过 100 个字符的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值