Declaration of variables
PHP variable declaration must be named with $(dollar sign)+variable name, and assignment after =(assignment operator)
The declared variable can not only be used in one , it can also be used All open functions on the current page, including files introduced by include and require, are of course local variables in the function, which is another matter. Before using this variable, we usually use the isset() and empty() functions. isset() checks whether the variable is set, empty() checks whether the variable is empty, and unset() releases the variable. It is recommended to use it here! empty() exists and cannot be empty
The naming of PHP variables is case-sensitive , and cannot be the keyword Demo<?php //声明变量a $a="hello world"; ?> <?php //判断变量a是否存在,如果存在,就打印,echo为打印函数 if(!empty($a)){ echo "变量存在"; echo $a; } //销毁变量a unset($a); if(empty($a)){ echo "变量不存在!"; } ?>
<?php //声明变量$a $a="hello"; //声明可变变量$$a $$a="world"; //将会全部打印"hello world" echo "$a $hello"; echo "$a ${$a}" ?>
<?php //声明变量$a $a="hello"; //声明变量$b $b=&$a; $b="world"; //将会打印"word world" echo "$a $b"; unset($a); //将会打印world echo $b; ?>
<?php $bool=true; $str="hello"; $int=123; $float=1.2e3;//相当于1.2乘以10的三次方 $arr=array("key1"=>12,"key2"=>true); //声明对象类型 class Person{ var $name; function say(){ echo "I am happy"; } } $p=new Person(); $p->name="Tom"; $p->say(); //var_dump()直接输出变量类型 var_dump($bool); var_dump($str); var_dump($int); var_dump($float); var_dump($arr); var_dump($p); //输出结果为 //I am happy //bool(true) string(5) "hello" int(123) float(1200) //array(2) { ["key1"]=> int(12) ["key2"]=> bool(true) } //object(Person)#1 (1) { ["name"]=> string(3) "Tom" } ?>
<?php //以写的方式打开本目录下的1.txt文件 $file=fopen("1.txt","w"); //连接本地数据库 $mysql=mysql_connect("localhost","root","root"); ?>
The callback type is a function such as call_user_func() that can receive a user-defined function as a parameter. The callback function can not only be a function, but also a method of an object and a method of a static class. A PHP function is passed as a function name string. Any built-in or user-defined function can be passed, except for example array(), echo(), empty(), eval(), exit(), isset(), list (), print(), unset() and other built-in functions.
Automatic type conversion
This conversion usually occurs when mixing operations of different types. It follows the following principles
If it is a Boolean type, true will become 1 and false will become 0
If it is null, it will become The value 0
If it is a mixed operation of float and int, convert it to float type
If it is a string, extract the numbers in the string, for example, "123.45abc" becomes 123.45, if there is no number, it is 0
Mandatory Type casts
Type casts in PHP are very much like in C: the variable to be converted is preceded by the target type enclosed in parentheses. The allowed casts are:
(int), (integer) - converted to integer type
(bool), (boolean) - converted to Boolean type
(float), (double), (real) - converted to Floating point type
(string) - Convert to string
(array) - Convert to array
(object) - Convert to object
At the same time, we can determine the variable type through some functions during use. Commonly used functions to determine variable types include the following:
gettype() returns variable type, is_array(), is_bool(), is_float(), is_double(), is_integer(), is_null(), is_numeric(), is_object( ), is_resource(), is_string() and is_callable() to determine whether it is a valid function