变量是存储信息的容器,以$符号开头,其后是变量名称。变量名称必须以字母或下划线开头,对大小写敏感
<?<span>php </span><span>$x</span>=5<span>; </span><span>//</span><span>5</span> <span>echo</span> <span>$x</span><span>; </span><span>//</span><span>什么都不输出</span> <span>echo</span> <span>$X</span><span>; </span>?>
PHP没有创建变量的命令,变量会在首次为其赋值时被创建
<?<span>php </span><span>$txt</span>="Hello world!"<span>; </span><span>$x</span>=5<span>; </span><span>$y</span>=10.5<span>; </span>?>
PHP是一门类型松散的语言,不必告知PHP变量的数据类型,PHP会根据它的值,自动把变量转换成正确的数据类型。PHP数据类型包括8种:其中包括四种标量类型、两种复合类型和两种特殊类型。具体是:字符串、整数、浮点数、布尔、数组、对象、NULL、资源
[注意]var_dump() 会返回变量的数据类型和值
字符串可以是引号内的任何文本,引号可以是单引号或双引号
<?<span>php </span><span>$x</span> = "Hello world!"<span>; </span><span>//</span><span>Hello world!</span> <span>echo</span> <span>$x</span><span>; </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = 'Hello world!'<span>; </span><span>//</span><span>Hello world!</span> <span>echo</span> <span>$x</span><span>; </span>?>
<情况1>当字符串中包含引号,有三种解决方案:
[1]单引号中嵌入双引号
[2]双引号中嵌入单引号
[3]使用转义符"\"
<?<span>php </span><span>$str_string1</span> = '"test"'<span>; </span><span>$str_string2</span> = "'test'"<span>; </span><span>$str_string3</span> = '\'test\''<span>; </span><span>echo</span> <span>$str_string1</span><span>; </span><span>echo</span> "<br />"<span>; </span><span>echo</span> <span>$str_string2</span><span>; </span><span>echo</span> "<br />"<span>; </span><span>echo</span> <span>$str_string3</span><span>; </span>?>
<情况2>当字符串的引号遇到变量,分为两种情况:
[1]当双引号中包含变量时,变量会与双引号中的内容连接在一起
[2]当单引号中包含变量时,变量会被当作字符串输出
<?<span>php </span><span>$test</span> = 1<span>; </span><span>$str_string1</span> = ' $test '<span>; </span><span>$str_string2</span> = " <span>$test</span> "<span>; </span><span>$str_string3</span> = ' "$test" '<span>; </span><span>$str_string4</span> = " '<span>$test</span>' "<span>; </span><span>echo</span> <span>$str_string1</span>;<span>//</span><span>$test</span> <span>echo</span> "<br />"<span>; </span><span>echo</span> <span>$str_string2</span>;<span>//</span><span>1</span> <span>echo</span> "<br />"<span>; </span><span>echo</span> <span>$str_string3</span>;<span>//</span><span>"$test"</span> <span>echo</span> "<br />"<span>; </span><span>echo</span> <span>$str_string4</span>;<span>//</span><span>'1'</span> ?>
<情况3>当字符串很长时,使用Heredoc结构形式的方法,首先定界符表示字符串(<<<),接着在(<<<)之后提供一个标识符(任意名称),换行后是字符串,最后以这个标识符结束字符串。注意标识符左右不要有多余的空格
<?<span>php </span><span>$str</span> = <<<<span> G </span>123<span> G; </span><span>echo</span> <span>$str</span>;<span>//</span><span>123</span> ?>
PHP整数必须至少有一个数字,不能包含逗号或空格,不能有小数点,正负均可,可以用三种格式规定整数:十进制、十六进制(前缀是0x)或八进制(前缀是0)
<?<span>php </span><span>$x</span> = 5985<span>; </span><span>//</span><span>int(5985)</span> <span>var_dump</span>(<span>$x</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = -345<span>; </span><span>//</span><span> int(-345)</span> <span>var_dump</span>(<span>$x</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = 0x11<span>; </span><span>//</span><span> int(17)</span> <span>var_dump</span>(<span>$x</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = 011<span>; </span><span>//</span><span> int(9)</span> <span>var_dump</span>(<span>$x</span><span>); </span>?>
PHP浮点数是有小数点或指数形式的数字
<?<span>php </span><span>$x</span> = 10.365<span>; </span><span>//</span><span>float(10.365)</span> <span>var_dump</span>(<span>$x</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = 2.4e3<span>; </span><span>//</span><span>float(2400)</span> <span>var_dump</span>(<span>$x</span><span>); </span><span>echo</span> "<br>"<span>; </span><span>$x</span> = 8E-1<span>; </span><span>//</span><span>float(0.8)</span> <span>var_dump</span>(<span>$x</span><span>); </span>?>
PHP布尔型只有两个值:true或false(不区分大小写),常用于条件测试。当用echo指令输出布尔类型时,如果是true,则输出"1",false则什么都不输出
<?<span>php </span><span>$man</span> = "男"<span>; </span><span>$flag</span> = <span>$man</span> == "男";<span>//</span><span>输出1</span> <span>echo</span> <span>$flag</span><span> ; </span><span>echo</span> "<br />"<span> ; </span><span>$flag</span> = <span>$man</span> == "女";<span>//</span><span>什么都不输出</span> <span>echo</span> <span>$flag</span><span>; </span><span>var_dump</span>(<span>$flag</span>);<span>//</span><span>bool(false)</span> ?>
PHP数组可以在一个变量中存储多个值
<?<span>php </span><span>$cars</span>=<span>array</span>("Volvo","BMW","SAAB"<span>); </span><span>//</span><span>array(3) { [0]=> string(5) "Volvo" [1]=> string(3) "BMW" [2]=> string(4) "SAAB" }</span> <span>var_dump</span>(<span>$cars</span><span>); </span>?>
PHP对象是存储数据和有关如何处理数据的信息的数据类型。在PHP中,必须明确地声明对象,但首先必须声明对象的类。对此,使用class关键词,类是包含属性和方法的结构。然后在对象类中定义数据类型,然后在该类的实例中使用此数据类型
<?<span>php </span><span>class</span><span> Car { </span><span>var</span> <span>$color</span><span>; </span><span>function</span> Car(<span>$color</span>="green"<span>) { </span><span>$this</span>->color = <span>$color</span><span>; } </span><span>function</span><span> what_color() { </span><span>return</span> <span>$this</span>-><span>color; } } </span><span>function</span> print_vars(<span>$obj</span><span>) { </span><span>foreach</span> (<span>get_object_vars</span>(<span>$obj</span>) <span>as</span> <span>$prop</span> => <span>$val</span><span>) { </span><span>echo</span> "\t<span>$prop</span> = <span>$val</span>\n"<span>; } } </span><span>$herbie</span> = <span>new</span> Car("white"<span>); </span><span>echo</span> "\herbie: Properties\n"<span>; print_vars(</span><span>$herbie</span><span>); </span>?>
PHP中的NULL是空类型,对大小写不敏感,NULL类型只有一个取值,表示一个变量没有值,当被赋值为NULL,或者尚未被赋值,或者被unset(),这三种情况下变量被认为为NULL
<?<span>php </span><span>error_reporting</span>(0); <span>//</span><span>禁止显示PHP警告提示</span> <span>$var</span><span>; </span><span>var_dump</span>(<span>$var</span>);<span>//</span><span>NULL</span> <span>$var1</span> = <span>null</span><span>; </span><span>var_dump</span>(<span>$var1</span>);<span>//</span><span>NULL</span> <span>$var2</span> = <span>NULL</span><span>; </span><span>var_dump</span>( <span>$var2</span>);<span>//</span><span>NULL</span> <span>$var3</span> = "节日快乐!"<span>; </span><span>unset</span>(<span>$var3</span><span>); </span><span>var_dump</span>(<span>$var3</span>);<span>//</span><span>NULL</span> ?>
PHP资源是由专门的函数来建立和使用的,例如打开文件、数据连接、图形画布。可以对资源进行操作(创建、使用和释放)。任何资源,在不需要的时候应该被及时释放。如果我们忘记了释放资源,系统自动启用垃圾回收机制,在页面执行完毕后回收资源,以避免内存被消耗殆尽
<?<span>php </span><span>$file</span>=<span>fopen</span>("data/webroot/resource/f.txt","r"); <span>//</span><span>打开文件</span> <span>$con</span>=<span>mysql_connect</span>("127.0.0.1","root","root"); <span>//</span><span>连接数据库</span> <span>if</span> (<span>$file_handle</span><span>){ </span><span>//</span><span>接着采用while循环(后面语言结构语句中的循环结构会详细介绍)一行行地读取文件,然后输出每行的文字</span> <span>while</span> (!<span>feof</span>(<span>$file_handle</span>)) { <span>//</span><span>判断是否到最后一行</span> <span>$line</span> = <span>fgets</span>(<span>$file_handle</span>); <span>//</span><span>读取一行文本</span> <span>echo</span> <span>$line</span>; <span>//</span><span>输出一行文本</span> <span>echo</span> "<br />"; <span>//</span><span>换行</span> <span> } } </span><span>fclose</span>(<span>$file_handle</span>);<span>//</span><span>关闭文件</span> ?>
[注意]memory_get_usage() 会获取当前PHP消耗的内存,单位为byte
<?<span>php </span><span>echo</span> <span>$m1</span> =<span> memory_get_usage(); </span><span>echo</span> "<br />"<span>; </span><span>$var_string</span> = '123';<span>//</span><span>320</span> <span>echo</span> <span>$m2</span> = memory_get_usage()-<span>$m1</span><span>; </span><span>echo</span> "<br />"<span>; </span><span>$n</span> = 123;<span>//</span><span>272</span> <span>echo</span> <span>$m3</span> = memory_get_usage()-<span>$m1</span>-<span>$m2</span><span>; </span><span>echo</span> "<br />"<span>; </span><span>$f</span> = 123.00;<span>//</span><span>272</span> <span>echo</span> <span>$m4</span> = memory_get_usage()-<span>$m1</span>-<span>$m2</span>-<span>$m3</span><span>; </span><span>echo</span> "<br />"<span>; </span><span>$var_array</span> = <span>array</span>('123');<span>//</span><span>576</span> <span>echo</span> <span>$m5</span> = memory_get_usage()-<span>$m1</span>-<span>$m2</span>-<span>$m3</span>-<span>$m4</span><span>; </span>?>
<?<span>php </span><span>$string</span> = "就是就是"<span>; </span><span>var_dump</span>(<span>$string</span>);<span>//</span><span>string(12) "就是就是"</span> <span>echo</span> "<br />"<span>; </span><span>$string</span> = 9494<span>; </span><span>var_dump</span>(<span>$string</span>);<span>//</span><span>int(9494)</span> <span>echo</span> "<br />"<span>; </span>?>
PHP有三种不同的变量作用域:local(局部)、global(全局)、static(静态)
函数之外声明的变量有global作用域,只能在函数以外进行访问;函数内部声明的变量有local作用域,只能在函数内部进行访问
<?<span>php </span><span>error_reporting</span>(0); <span>//</span><span>禁止显示PHP警告提示</span> <span>$x</span>=5; <span>//</span><span> 全局作用域</span> <span>function</span><span> myTest() { </span><span>$y</span>=10; <span>//</span><span> 局部作用域</span> <span>echo</span> "<p>测试函数内部的变量:</p>"<span>; </span><span>echo</span> "变量 x 是:<span>$x</span><br>"<span>; </span><span>echo</span> "变量 y 是:<span>$y</span>"<span>; } </span><span>//</span><span>无输出 //10</span> <span>myTest(); </span><span>echo</span> "<p>测试函数之外的变量:</p>"<span>; </span><span>echo</span> "变量 x 是:<span>$x</span><br>";<span>//</span><span>5</span> <span>echo</span> "变量 y 是:<span>$y</span>";<span>//</span><span>无输出</span> ?>
用于访问函数内的全局变量
<?<span>php </span><span>$x</span>=5<span>; </span><span>$y</span>=10<span>; </span><span>function</span><span> myTest() { </span><span>global</span> <span>$x</span>,<span>$y</span><span>; </span><span>$y</span>=<span>$x</span>+<span>$y</span><span>; } myTest(); </span><span>echo</span> <span>$y</span>; <span>//</span><span> 输出 15</span> ?>
PHP同时在名为GLOBALS[index]的数组中存储了所有的全局变量,下标存有变量名。这个数组在函数内也可以访问,并能够用于直接更新全局变量
<?<span>php </span><span>$x</span>=5<span>; </span><span>$y</span>=10<span>; </span><span>function</span><span> myTest() { </span><span>$GLOBALS</span>['y']=<span>$GLOBALS</span>['x']+<span>$GLOBALS</span>['y'<span>]; } myTest(); </span><span>echo</span> <span>$y</span>; <span>//</span><span> 输出 15</span> ?>
通常,当函数完成或执行后,会删除所有变量,不过,有时需要不删除某个局部变量。要完成这一点,需要在首次声明变量时使用static关键词。每当函数被调用时,这个变量所存储的信息都是函数最后一次被调用时所包含的信息,但要注意的是,这个变量仍然是函数的局部变量
<?<span>php </span><span>function</span><span> myTest() { </span><span>static</span> <span>$x</span>=0<span>; </span><span>echo</span> <span>$x</span><span>; </span><span>$x</span>++<span>; } myTest();</span><span>//</span><span>0</span> <span>echo</span> "<br>"<span>; myTest();</span><span>//</span><span>1</span> <span>echo</span> "<br>"<span>; myTest();</span><span>//</span><span>2</span> <span>echo</span> "<br>"<span>; myTest();</span><span>//</span><span>3</span> <span>echo</span> "<br>"<span>; myTest();</span><span>//</span><span>4</span> ?>