Home > Backend Development > PHP Tutorial > php (3) PHP variable type

php (3) PHP variable type

黄舟
Release: 2023-03-04 11:14:01
Original
1392 people have browsed it

1.PHP has eight variable types:

<1>Scalar type:

boolean (Boolean)

integer (integer)

float (floating point type, also called "double")

string (string)

<2>Composite type:

array (array)

object (object)

<3> Special types:

resource (resource)

NULL

PS: PHP variable type is not used Statement, PHP will automatically determine its type based on the context in which the program is running. Isn’t it smart? So awesome

If you want to check the value of an expression and similar, you can use the function var_dump().

(1).boolean (Boolean type)

There are only two values, true or false, which are not case-sensitive. All non-0 values ​​are is true, 0 is false.

boolean (Boolean type) is often used for conditional judgment in process control.

Example:

[php]  
<?php  
$b=true;  
if ($b == true)  
{  
    echo &#39;$b is true&#39;;  
}  
?>
Copy after login

2.integer (integer)

Integer values ​​can be specified in decimal, hexadecimal or octal notation

Example:

<?php
$b = 1234; // 十进制数
$b = -123; // 一个负数
$b = 0123; // 八进制数(等于十进制的 83)
$b = 0x1A; // 十六进制数(等于十进制的 26)
Copy after login

?>

3.float (floating point type, also called "double")

Floating point number ( Also called floats, doubles or real numbers) can be defined with any of the following syntax:

Example:

[php]  
<?php  
$b = 1.334;  
$b = 1.3e3;  
$b = 8E-10;  
?>
Copy after login

(4)string( String)

String definitions are divided into three ways: single quotes, double quotes, and delimiters.

For example:

[php]  
<?php  
//单引号定义字符串  
$a = &#39;aaa&#39;;  
//双引号定义字符串  
$b = "bbb";  
//定界符定义字符串  
$c = <<<eof  
ccccccccc  
eof;//顶到头开始写,前面不能留空格  
echo $a;  
echo "<br>";  
echo $b;  
echo "<br>";  
echo $c;  
?>
Copy after login

Variable analysis:

Single quotes: If the definition content includes variables, the variable name is output directly instead of the content.

Double quotation marks: If the definition content includes variables, the content will be output directly.

delimiter: If the definition content includes variables, the content will be output directly.

In double quotes and delimiters, {} can be used to specify variable scope.

[php]  
<?php  
$temps = "123";  
$tempss = "1234";  
$b = "bbb{$temps}s";  
echo $b;  
?>
Copy after login

(5)array() (array) definition

array( [key =>]

value

, ...

)

// key Can be integer or string

// value can be any value

For example:

[php]  
<?php  
$arr = array("foo" => "bar", 12 => true);  
echo $arr["foo"]; // bar  
echo $arr[12];    // 1  
?>
Copy after login

(6)object (object)

To initialize an object, use the new statement to instance the object to in a variable.

Example:

[php] 
<?php  
//创建一个foo的类  
class foo  
{  
    //创建一个do_foo的方法  
    function do_foo()  
    {  
        //输出Dong Foo  
        echo "Doing foo.";  
    }  
}  
//创建一个$bar的实例  
$bar = new foo;  
//$bar的实例调用do_foo的方法  
$bar->do_foo();  
?>
Copy after login

(7)resource( Resources)

To be written. . .

(8)NULL

The special NULL value means that a variable has no value, not that the variable does not exist. The only possible value of type NULL is NULL. ‘

A variable is considered NULL when:

is assigned a value of NULL.

has not been assigned a value.

is unset().

For example:

[php]  
<?php  
$var = NULL;  
?>
Copy after login

Two related functions :

is_null(): Determine whether the variable is NUll

unset(): Delete the variable declaration

The above is php (3) PHP variable types For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template