细说变量(1):
变量的命名:
1. 以$符号开始
2. 名称要有意义
3. 不能以数字开头,不能有运算符(关键字是可以作为php的变量名称的
4. 变量名区分大小写(驼峰式命名方式如: oneTwoThree)
5. 可以同时声明多个变量(如: $a=$b=$c=10;)
四个变量检测工具:
6. Var_dump( ) 查看内容类型
7. Isset( ) 判断变量是否存在(常常用于if判断;if( isset($a)) { }
8. unset() 删除变量
9. empty() 判断内容是否为空
可变变量:
一个可变变量获取了一个普通变量的值作为这个可变变量的变量名
如:
$hello = "world"; //普通变量
$$hello="name"; //可变变量
echo $world; //输出name
变量的引用:
$a = 10;
$b = 20;
$c=&$b; //变量$c作为$b的引用,相当于$b的别名
$b=100;
echo $c;
注意:
1. 只有变量才有地址,也就是只有变量才有引用
2. 一个变量变化,另一个变量的值也变化
3. 在使用unset()函数时,如果有引用关系,只是解除这种引用关系,只删除一个名字,另一个还存在,如以下两种情形输出结果都是存在:
$a = 10;
$b =&$a;
//unset($a);
unset($b);
//if (isset($b))
if (isset($a))
{
echo "This variable is exist";
}
else {
echo "This variable is not exist";
}
1. 如果两个变量是引用关系,一个变,另一个也变,但如果给其中一个引用——不是普通的值,而是一个新的引用,则原来的引用关系改变,使用新的引用关系;如下代码输出结果:
$a = 10;
$b =&$a;
$c=100;
$b=&$c;
echo $b; //output 100
echo "<br>";
$a=35;
echo $b; //output 100
echo "<br>";
$c=99;
echo $b; //output 99
变量类型:
Php支持8种原始变量类型 var_dump()
四种标量类型:
1. 布尔型boolean
2. 整型integer
3. 浮点型float
4. 字符型string
两种复合类型:
1. 数组array
2. 对象object
最后两种是特殊类型:
1. 资源resource
2. Null
echo "follows are 4 kinds of scalar type:";
$var = true; //boolean
var_dump($var);
echo "<br>";
$var=10;
var_dump($var);//int
echo "<br>";
$var=10.23; //float
var_dump($var);
echo "<br>";
$var="hello world "; //string
var_dump($var);
echo "<br>";
输出结果:
Output result:
follows are 4 kinds of scalar type:
F:\wamp\www\Demo\file01\index.php:71:boolean true
F:\wamp\www\Demo\file01\index.php:75:int 10
F:\wamp\www\Demo\file01\index.php:79:float 10.23
F:\wamp\www\Demo\file01\index.php:84:string 'hello world ' (length=12)
echo "follows are 2 kinds of conplex type:";
$var = array("one","two","three");
var_dump($var); //array
echo "<br>";
class Demo{
var $one=10;
var $two =20;
var $three=30;
}
$var = new Demo;
var_dump($var); //object
echo "<br>";
输出:
Output result:
follows are 2 kinds of conplex type:
F:\wamp\www\Demo\file01\index.php:91:
array (size=3)
0 => string 'one' (length=3)
1 => string 'two' (length=3)
2 => string 'three' (length=5)
F:\wamp\www\Demo\file01\index.php:101:
object(Demo)[1]
public 'one' => int 10
public 'two' => int 20
public 'three' => int 30