PHP引用类型
PHP引用类型类似于C++语言中的“引用类型”,相当于是一个变量的别名,语法如下:
<code><span><span><?php </span><span>$a</span> = <span>100</span>; <span>$b</span> = &<span>$a</span>; <span>?></span></span></span></code>
其中变量b是变量a的引用。
注意:
<code><span><span><?php </span><span>$b</span> = &<span>100</span><span>//错误</span><span>?></span></span></span></code>
<code><span><span><?php </span><span>$a</span> = <span>100</span>; <span>$b</span> = &<span>$a</span>; <span>$b</span> = <span>50</span>; <span>echo</span><span>"a = $a<br>"</span> ; <span>echo</span><span>"b = $b"</span> ; <span>?></span></span></span></code>
unset()
函数时,如果有引用关系,只是解除了这种引用关系,删除了一个别名,而另一个还在<code><span><?php </span><span>$a</span> = <span>100</span>; <span>$b</span> = &<span>$a</span>; <span>unset</span>(a) <span>if</span>(<span>isset</span>(<span>$b</span>)) { <span>echo</span><span>"这个变量存在"</span>;<span>//输出</span> } <span>else</span> { <span>echo</span><span>"这个变量不存在"</span>; } <span>?></span></span></code>
<code><span><span><span><?php </span><span>$a</span> = <span>100</span>; <span>$b</span> = &<span>$a</span>; <span>$c</span> = <span>50</span>; <span>$b</span> = &<span>$c</span>; <span>$b</span> = <span>25</span>; <span>echo</span><span>"a = $a<br>"</span> ;<span>// a = 100</span><span>echo</span><span>"b = $b<br>"</span> ; <span>// b = 25</span><span>echo</span><span>"c = $c"</span> ; <span>// c = 25</span><span>?></span></span></span></span></code>
PHP布尔类型
boolean
型的变量值可以为true
、false
false
:0
0
""
、"0"
array()
Null
PHP字符串类型
字符串声明
使用""
和''
声明
"\"
。使用定界符
<code><span><span><?php </span><span>$var</span> = <span>100</span><span>$str</span> = //ss可以是任意字符串,后面紧跟回车符</span> Hello World! This is a <span>"Demo"</span>;\n The value of \<span>$var</span> is <span>$var</span>; ss;<span>//ss必须顶格写</span><span>echo</span><span>$str</span>; <span>?></span></span></code>
单引号和双引号的区别
<code><span><span><?php </span><span>$a</span> = <span>100</span>; <span>echo</span><span>"The value of a is $a."</span>;<span>//The Value of a is 100.</span><span>echo</span><span>"The value of a is{$a}."</span>;<span>//The Value of a is 100.</span><span>echo</span><span>'The value of a is $a.'</span>;<span>//The value of a is $a.</span><span>?></span></span></span></code>
<code><?php <span>$a = <span>100</span>; <span>echo</span><span>"The Value \$a = <span>$a</span>."</span>.;// The value <span>$a</span> = <span>100</span>. <span>echo</span><span>'The Value \$a = $a.'</span>;//The value \<span>$a</span> = <span>$a</span>. <span>echo</span><span>'This is a \'</span>string\<span>' demo.'</span>;//This is a <span>'string'</span> demo; ?></code>
PHP常量
常量的声明、命名
define(string name, mixedtype value)
<code><span><span><?php </span> define(<span>"CONSTANT"</span>, <span>100</span>); <span>//CONSTANT是一个值为100的整型常量</span><span>?></span></span></span></code>
常量的使用
<code><span><span><?php </span> define(<span>"CONSTANT"</span>, <span>100</span>); <span>if</span>(defined(<span>"CONSTANT"</span>)) { <span>echo</span> CONSTANT; <span>echo</span><span>"<br>"</span>; <span>echo</span> constant(<span>"CONSTANT"</span>); } <span>?></span></span></span></code>
注意
unset()
函数清除define()
函数的第三个参数决定这个常量的名称是否支持大小写**待续······**
版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了PHP基本数据类型,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。