四种标量
整型:int,integer
布尔型:bool,Boolean
浮点型:float,double,real
字符型:string(单引号和双引号都可以声明字符串,但在双引号内不能使用双引号单引号内不能使用单引号)
两种复合类型
数组:array
对象:object
两种特殊类型
资源类型:resource
空类型:null
几种常用的PHP常量
_FILE_:当前文件在服务器中的绝对路径.如D\PHP\helloworld.php
PHP_VERSION:服务器的版本号.如5.2.4
PHP_OS:服务器操作系统.如WINNT
TRUE/FALSE:代表true/false值
静态变量其实就是不能在外部访问的全局变量
声明变量前加$,PHP中变量都以$开头,而函数名和常量则都是直接使用字符串或者下划线开头
函数内调用函数外部变量,需要先用global声明
<?php
$a=1;
function helloWorld1(){
echo $a;
}
test(); //这里将不能输出结果“1”。
function helloWorld2(){
global $a;
echo $a; //这里将输出结果“1”。
}
?>
算术运算符
加+减-乘*除/求余%
比较运算符
等于==全等于===不等!=或者<>非全等!==小于<大于>小于等于<=大于等于>=
如果比较成立返回1,否则返回空
特殊字符意义
变量 $
变量的地址(加在变量前) &
不显示错误信息(加变量前) @
类的方法或者属性 ->
数组的元素值 =>
三元运算符 ?:
<?php
$helloworld="hello,word!";
function helloWorld()
{
$a=4;
$b=4;
$c=10;
global $helloworld;
echo $helloworld;
echo "<br/>";
echo "\$a+\$b=".($a+$b)."<br/>";//求和,说明:.是连接符
echo "\$a-\$b=".($a-$b)."<br/>";//求差
echo "\$a*\$b=".($a*$b)."<br/>";//求积
echo "\$a/\$b=".($a/$b)."<br/>";//求商
echo "\$a%\$b=".($a%$b)."<br/>";//求余数
echo "\$a==\$b,the value is ".($a==$b)."<br>";
echo "\$a===\$b,the value is ".($a===$b)."<br>";
echo "\$a<\$c,the value is ".($a<$c)."<br>";
echo "\$a!==\$b,the value is ".($a!==$b)."<br>";
}
helloWorld();
?>
逻辑判断符
与 &&
或 ||
非 !
include()语句require()语句
如果要根据条件或者循环包含文件,需要使用include(),require()语句只是被简单的包含一次,任何的条件语句或者循环等对其无效.由于include()是一个特殊的语句结构,银次若语句在一份语句块中,则必须把他包含在一个语句块中.
<?php
//下面为错误语句
if($condition)
include($file);
else
include($other);
//下面为正确语句
if($condition){
include($file);
}else
{
include($other);
}
?>
关联参数传递(&的使用)
<?php
//方法一:
function foo(&$bar){
$bar.=" and something extra";
}
$str="This is a String,";
foo($str);
echo $str; //output:This is a String, and something extra
echo "<br>";
//方法二:
function foo1($bar){
$bar.=" and something extra";
}
$str="This is a String,";
foo1($str);
echo $str; //output:This is a String,
echo "<br>";
foo1(&$str);
echo $str; //output:This is a String, and something extra
?>