Home > Article > Backend Development > Front-end learning PHP variables, data types and scope, php data types_PHP tutorial
A variable is a container for storing information, starting with the $ symbol, followed by the variable name. Variable names must start with a letter or underscore and are case-sensitive
<?<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 does not have a command to create variables. Variables will be created when they are assigned a value for the first time
<?<span>php </span><span>$txt</span>="Hello world!"<span>; </span><span>$x</span>=5<span>; </span><span>$y</span>=10.5<span>; </span>?>
PHP is a loosely typed language. You do not need to tell PHP the data type of the variable. PHP will automatically convert the variable into the correct data type based on its value. PHP data types include 8 types: including four scalar types, two composite types and two special types. Specifically: string, integer, floating point number, Boolean, array, object, NULL, resource
[Note] var_dump() will return the data type and value of the variable
A string can be any text within quotes, which can be single or double quotes
<?<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>?>
a425056ea21816fa641f7ae73d578571When the string contains quotation marks, there are three solutions:
[1] Embed double quotes within single quotes
[2] Embed single quotes in double quotes
[3] Use the escape character ""
<?<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>?>
bbcd48ad01ce0f51931d9fe514173446When the quotation marks of a string encounter a variable, there are two situations:
[1] When a variable is contained in double quotes, the variable will be concatenated with the content in the double quotes
[2] When a variable is enclosed in single quotes, the variable will be output as a string
<?<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> ?>
7a4f8b29942562410bea3902e90fe101When the string is very long, use the Heredoc structure method. First, the delimiter represents the string (<<<), and then ( <<<) is followed by an identifier (any name), a newline followed by a string, and finally the string ends with this identifier. Be careful not to have extra spaces around the identifier
<?<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 integers must have at least one number, cannot contain commas or spaces, cannot have decimal points, can be positive or negative, and can specify integers in three formats: decimal, hexadecimal (prefix is 0x ) or octal (prefix is 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 floating point numbers are numbers with decimal point or exponential form
<?<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 boolean has only two values: true or false (case-insensitive), which is often used for conditional testing. When using the echo command to output a Boolean type, if it is true, it will output "1", if it is false, it will output nothing
<?<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 arrays can store multiple values in one variable
<?<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 objects are data types that store data and information about how to process the data. In PHP, objects must be declared explicitly, but first the object's class must be declared. For this, use the class keyword. A class is a structure that contains properties and methods. Then define the data type in the object class and then use this data type in the instance of that 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>?>
NULL in PHP is an empty type and is not case-sensitive. The NULL type has only one value, which means that a variable has no value. When it is assigned to NULL, or it has not been assigned a value, or it has been unset(), In these three cases the variable is considered 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 resources are created and used by specialized functions, such as opening files, data connections, and graphics canvases. Resources can be manipulated (created, used and released). Any resources should be released promptly when no longer needed. If we forget to release resources, the system automatically enables garbage collection and recycles resources after the page is executed to avoid memory being exhausted
<?<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> ?>
[Note] memory_get_usage() will get the memory currently consumed by PHP, the unit is 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 has three different variable scopes: local (local), global (global), static (static)
Variables declared outside the function have global scope and can only be accessed outside the function; variables declared inside the function have local scope and can only be accessed inside the function
<?<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> ?>