Home  >  Article  >  类库下载  >  PHP variables

PHP variables

高洛峰
高洛峰Original
2016-10-09 12:43:591303browse

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 "变量不存在!";
}
?>

Variable variable

Variable variable means that the variable name of a variable can be set and used dynamically. An ordinary variable is set through declaration, and then the variable variable gets the value of the ordinary variable as the variable name of the variable variable. The variable variable declaration starts with $$.

Demo

<?php
//声明变量$a
$a="hello";
//声明可变变量$$a
$$a="world";

//将会全部打印"hello world"
echo "$a $hello";
echo "$a ${$a}"
?>

Reference assignment of variables

The reference of PHP is to add the & symbol in front of the variable, function, object, etc. It is actually equivalent to an alias of a variable. If the value of any one of the variables is changed, the value of the other variable will change accordingly. But it is not like variable reference assignment in C language. If I use the unset() function to destroy any one of the variables, the other variable still exists.

Demo

<?php
//声明变量$a
$a="hello";
//声明变量$b
$b=&$a;

$b="world";
//将会打印"word world"
echo "$a $b";

unset($a);

//将会打印world
echo $b;
?>

Types of variables

PHP supports eight primitive types. Specifically, it is divided into four scalar types: string (string), integer (integer), float (floating point type, and higher-precision double) and boolean (Boolean type), and two composite types: array (array) ) and object (object), two special types resource (resource) and NULL. The declaration of arrays and objects can refer to the format in Demo. We use array() to construct the array here, and its parameters are separated by commas in the key=>value format.

Demo

<?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" }
?>

Resource type

Resource is a special variable that holds a reference to an external resource. Resources are created and used through specialized functions. Since resource type variables hold special handles for opening files, database connections, graphics canvas areas, etc., there is no point in converting values ​​of other types into resources.

Demo

<?php
//以写的方式打开本目录下的1.txt文件
$file=fopen("1.txt","w");

//连接本地数据库
$mysql=mysql_connect("localhost","root","root");
?>

NULL type

There are three situations that are considered to be NULL types in PHP

Assign variables directly to NULL

Declared variables are not assigned

Variables destroyed by the unset() function

There are three pseudo-types in PHP: mixed type, number type and callback type.

mixed indicates that a parameter can accept multiple different types, but not all types. For example, str_replace() can accept strings and arrays, and gettype() can accept any type.

The number parameter can accept integer and float.

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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more