主要内容:
PHP 教程
PHP 简介
PHP 安装
PHP 语法
PHP 变量
PHP 字符串
PHP 运算符
关于 PHP的基本知识,这里不再重复了。我的理解是:
1. PHP 是脚本语言
2. PHP是内嵌在HTML中的, 文件扩展名一般是php
3. PHP在服务器端运行,返回可定制的HTML给客户端。 所以在客户端是不能看到源码的
-----------------------------------------------
这是一个最简单的PHP例子
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
把这个文件存为test.php, 并且放入到相应的服务器主目录中,例如EasyPHP 的www目录
运行得到结果:显示Hello World
----------------------------------------------------------
下面添加变量吧:
<?php
$txt = "Hello World!";
$number = 16;
echo $txt;
echo $number;
?>
显示Hello World!16
正如你所看到的,PHP是松散类型的语言,不必向 PHP 声明该变量的数据类型
------------------------------------------------------------
并置运算符(Concatenation Operator)
<?php
$txt = "Hello World!";
$number = 16;
echo $txt." ".$number;
?>
以上我们把字符串联接了起来,并且在中间加入了一个空格
显示 Hello World! 16
--------------------------------------------------------------
字符串的2个函数:
strlen() 和 strpos()
<?php
$txt = "Hello World!";
echo "strlen(txt)=".strlen($txt).", ";
echo "strpos($txt,\"World\")=".strpos($txt,"World");
?>
运行结果是strlen(txt)=12, strpos(Hello World!,"World")=6
请注意为什么strpos(Hello World!,"World")=6 而不是5, 因为字符串起始位置是从0开始的,而不是1
另外的用了转义操作符 \ , 这真的是我猜出来的,胡乱用着试的,看来和C语言很相近,赞一个!
-----------------------------------------------------------------
PHP 运算符
很庆幸的是,PHP的运算符几乎和C语言一模一样,请看:
算数运算符
运算符 | 说明 | 例子 | 结果 |
---|---|---|---|
+ | 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 | 1 2 0 |
++ | 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 |