Home > Backend Development > PHP Tutorial > php data types

php data types

WBOY
Release: 2016-07-29 09:00:44
Original
1180 people have browsed it

PHP 支持8种基本的数据类型。

四种标量类型:

  • boolean (布尔型)
  • integer (整型)
  • float (浮点型, 也称作 double)
  • string (字符串)

两种复合类型:

  • array (数组)
  • object (对象)

最后是两种特殊类型:

  • resource (资源)
  • NULL (NULL)

为了确保代码的易读性,本手册还介绍了一些伪类型:

  • mixed
  • number
  • callback

以及伪变量 $....

可能还会读到一些关于“双精度(double)”类型的参考。实际上 double 和 float 是相同的,由于一些历史的原因,这两个名称同时存在。

变量的类型通常不是由程序员设定的,确切地说,是由 PHP 根据该变量使用的上下文在运行时决定的。

Note如果想查看某个表达式的值和类型,用 var_dump()

如果只是想得到一个易读懂的类型的表达方式用于调试,用 gettype()。要查看某个类型,不要用 gettype(),而用 is_type 函数。以下是一些范例:

php data types

<p><span></span><span>php
</span><span>$a_bool</span><span>=</span><span>TRUE</span><span>;   </span><span>//</span><span> a boolean</span><span>$a_str</span><span>=</span><span>"</span><span>foo</span><span>"</span><span>;  </span><span>//</span><span> a string</span><span>$a_str2</span><span>=</span><span>'</span><span>foo</span><span>'</span><span>;  </span><span>//</span><span> a string</span><span>$an_int</span><span>=</span><span>12</span><span>;     </span><span>//</span><span> an integer</span><span>echo</span><span>gettype</span><span>(</span><span>$a_bool</span><span>); </span><span>//</span><span> prints out:  boolean</span><span>echo</span><span>gettype</span><span>(</span><span>$a_str</span><span>);  </span><span>//</span><span> prints out:  string

// If this is an integer, increment it by four</span><span>if</span><span> (</span><span>is_int</span><span>(</span><span>$an_int</span><span>)) {
    </span><span>$an_int</span><span>+=</span><span>4</span><span>;
}

</span><span>//</span><span> If $bool is a string, print it out
// (does not print out anything)</span><span>if</span><span> (</span><span>is_string</span><span>(</span><span>$a_bool</span><span>)) {
    </span><span>echo</span><span>"</span><span>String: </span><span>$a_bool</span><span>"</span><span>;
}
</span><span>?></span></p>
Copy after login

php data types


如果要将一个变量强制转换为某类型,可以对其使用强制转换或者 settype() 函数。

注意变量根据其当时的类型在特定场合下会表现出不同的值。更多信息见类型戏法。此外,你还可以参考 PHP 类型比较表看不同类型相互比较的例子。

#

php data types

<p><span>二、PHP的数据类型

Int zjj</span><span>=</span><span>100</span><span>;                  </span><span>//</span><span>这种赋值是对的</span><span>
Int zjj</span><span>=</span><span>"</span><span>hello,world</span><span>"</span><span>;            </span><span>//</span><span>这种编译器就会提示错误</span><span>
在PHP中,对变量的数据类型不需要声明,可以直接赋值。如下面的

$zjj</span><span>=</span><span>100</span><span>;               </span><span>//</span><span>表示zjj为整型</span><span>
$zjj</span><span>=</span><span>"</span><span>hello,world</span><span>"</span><span>//</span><span> 表示zjj为字符串型</span><span>
PHP中的数据类型分成两种:一是标量数据类型,另一种就是复合数据类型。

标量数据类型有:

布尔型(boolean)         整型(integer)          浮点型(foat也叫做double)     

复合数据有:

数组(array)        对象(</span><span>object</span><span>)

还有两种特殊的数据

资源(resource)           空值(NULL)

</span><span>1</span><span>、布尔型是最简单的变量,布尔变量:true和false值

要设定一个布尔变量,只需将这两个值赋给变量就行了。PHP中就算是关键字也可以做变量名。

</span><span></span><span>php
 $True</span><span>=</span><span>"</span><span>我是变量</span><span>"</span><span> . True;                   </span><span>//</span><span>这里有点的,书上有时印刷并没有。</span><span> echo($True);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);                        </span><span>//</span><span>起到回车的作用</span><span> echo(</span><span>"</span><span>我是关键字</span><span>"</span><span> . True)
</span><span>?></span><span>执行结果:我是变量1                            </span><span>//</span><span>这里true就是1</span><span>
                  我是关键字1

true和false实际上是代表数值1和0,因此true在输出时显示为1,但是在给变量赋值的时候,如果赋值为1,则被当成整型,如果被赋值为true,则被当成布尔型

当转换为布尔型时,以下值被认为是false

 (</span><span>1</span><span>)布尔值false

(</span><span>2</span><span>)整型值0(零)

(</span><span>3</span><span>)浮点型值0.</span><span>0</span><span>(零)

(</span><span>4</span><span>)空白字符串和字符串</span><span>"</span><span>0</span><span>"</span><span>(</span><span>5</span><span>)没有成员变量的数组

(</span><span>6</span><span>)没有单元的对象(仅适用php4)

(</span><span>7</span><span>)特殊类型NULL

而所有其他值都被认为是TRUE包括任何资源。

</span><span>2</span><span>、整型

</span><span>+</span><span>2147483647到</span><span>-</span><span>2147483647</span><span></span><span>php
 $int_D</span><span>=</span><span>2147483647</span><span>;  </span><span>//</span><span>十进制赋值</span><span> echo($int_D);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);
 $int_H</span><span>=</span><span>0x7FFFFFFF</span><span>;  </span><span>//</span><span>十六进制赋值</span><span> echo($int_H);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);
 $int_O</span><span>=</span><span>017777777777</span><span>;  </span><span>//</span><span>八进制赋值</span><span> echo($int_O);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);
</span><span>?></span><span>执行结果:

</span><span>2147483647</span><span>2147483647</span><span>2147483647</span><span>如果指定一个数超出了inter的范围,将会被解释为float

</span><span>3</span><span>、浮点型

</span><span>1</span><span>.7E</span><span>-</span><span>308到1.7E</span><span>+</span><span>308</span><span></span><span>php
 $float_1</span><span>=</span><span>90000000000</span><span>;  
 echo($float_1);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);
 $float_2</span><span>=</span><span>9E10;  
 echo($float_2);
 echo(</span><span>"</span><span><br></span><span>"</span><span>);
 $float_3</span><span>=</span><span>9E</span><span>+</span><span>10</span><span>; 
 echo($float_3);
</span><span>?></span><span>执行结果:

</span><span>90000000000</span><span>90000000000</span><span>90000000000</span><span>4</span><span>、字符串

PHP中有三种方式定义字符串

</span><span>1</span><span>、单引号方式。

$single_str</span><span>=</span><span>'</span><span>我是人</span><span>'</span><span>;

如果想在浏览中输出单引号,必须在字符串中加入转义符号“\”

</span><span></span><span>
 $single_str</span><span>=</span><span>'</span><span>我被单引号括起来了!<br></span><span>'</span><span>;      //正常输出
 echo $single_str;
 $single_str</span><span>=</span><span>'</span><span>输出单引号: \'嘿嘿,我在单引号里面\'<br></span><span>'</span><span>;      //加了转义符可以输出单引号
 echo $single_str;
 $single_str</span><span>=</span><span>'</span><span>输出双引号:"我在双引号里面"<br></span><span>'</span><span>;         //正常输出
 print $single_str;
 $single_str</span><span>=</span><span>'</span><span>输双美元符号:$</span><span>'</span><span>;                 //正常输出
 print $single_str;
</span><span>?></span><span>执行结果:

我被单引号括起来了</span><span>!</span><span>
输出单引号: </span><span>'</span><span>嘿嘿,我在单引号里面</span><span>'</span><span>
输出双引号:</span><span>"</span><span>我在双引号里面</span><span>"</span><span>
输双美元符号:$

(</span><span>2</span><span>)、双引号方式

$Double_Str</span><span>=</span><span>"</span><span>我是人</span><span>"</span><span>为了在字符串中输出各种特殊字符,可以使用转义符

$Double_Str</span><span>=</span><span>"</span><span>输出美元符号:\$</span><span>"</span><span>;

$Double_Str</span><span>=</span><span>"</span><span>输出反斜杠:\\</span><span>"</span><span>;

</span><span></span><span>
 $Double_str</span><span>=</span><span>"</span><span>我被双引号括起来了!<br></span><span>"</span><span>;
 echo $single_str;
 $single_str</span><span>=</span><span>"</span><span>输出单引号:'嘿嘿,我在单引号里面'<br></span><span>"</span><span>; </span><span>//</span><span>不需要转义符</span><span> echo $single_str;
 $single_str</span><span>=</span><span>"</span><span>输出双引号:\"我在双引号里面\"<br></span><span>"</span><span>;  </span><span>//</span><span>需要转义符 </span><span> print $single_str;
 $single_str</span><span>=</span><span>"</span><span>输出美元符号:\$ <br></span><span>"</span><span>;  </span><span>//</span><span>需要转义符 </span><span> print $single_str;
 $single_str</span><span>=</span><span>"</span><span>输出反斜杠 :\\ <br></span><span>"</span><span>;  </span><span>//</span><span>需要转义符 </span><span> print $single_str;
 
</span><span>?></span><span>执行结果:

输出单引号:</span><span>'</span><span>嘿嘿,我在单引号里面</span><span>'</span><span>
输出双引号:</span><span>"</span><span>我在双引号里面</span><span>"</span><span>
输出美元符号:$ 
输出反斜杠 :\ 

(</span><span>3</span><span>)、Heredoc 方式给字符串赋值

</span><span></span><span>
$zjj1_str </span><span>=<span>zjj          //</span><span>=<span>这个相当于链接符,获取zjj到zjj中间的数据
 你好</span><span><span>br</span><span>></span><span>
 美元符号  $ </span><span><span>br</span><span>></span><span>         //zjj1和zjj都是可以随便起名字的。
 反斜杠   \</span><span><span>br</span><span>></span><span>"</span><span>我爱你</span><span>"</span><span><span>br</span><span>></span><span>'</span><span>我恨你</span><span>'</span><span>
zjj;
 echo $zjj1_str;
</span><span>?></span><span>执行结果:

你好
美元符号 $ 
反斜杠 \
</span><span>"</span><span>我爱你</span><span>"</span><span>'</span><span>我恨你</span><span>'</span><span>(</span><span>4</span><span>)、串中的变量

</span><span></span><span>php
 $str_1 </span><span>=</span><span>"</span><span>我是变量的值!</span><span>"</span><span>;  
 $str_2 </span><span>=</span><span>"</span><span>str_1 : $str_1<br></span><span>"</span><span>;  </span><span>//</span><span>双引号字符串中包含变量$str_1</span><span> echo $str_2;

 $str_1 </span><span>=</span><span>'</span><span>我是变量的值</span><span>'</span><span>;
 $str_2 </span><span>=</span><span>'</span><span>str_1 : $str_1<br></span><span>'</span><span>;  </span><span>//</span><span>单引号中包含字符串的值</span><span> echo $str_2;

 $str_1 </span><span>=</span><span>"</span><span>我是变量的值!</span><span>"</span><span>;  
 $str_2 </span><span>=</span><span>"</span><span>str_1 : $str_12 <br></span><span>"</span><span>;  </span><span>//</span><span>引用的变量名后,多了个字符2即$str_12</span><span> echo $str_2;

 $str_1 </span><span>=</span><span>"</span><span>我是变量的值!</span><span>"</span><span>;  
 $str_2 </span><span>=</span><span>"</span><span>str_1 : ${str_1}2 <br></span><span>"</span><span>;  </span><span>//</span><span>引用的变量名后,多了个字符2即$str_12</span><span> echo $str_2;
</span><span>?></span><span>执行结果:

str_1 : 我是变量的值</span><span>!</span><span>
str_1 : $str_1
str_1 : 
str_1 : 我是变量的值</span><span>!</span><span>2</span><span>从输出可以看出,双引号字符串是赋值的含义,直接输出变量的值。而单引号的字符串,包含的是变量的名称,可以理解为直接运行语句的结果,直接输出变量的名称而不是变量的值。

$str_2 </span><span>=</span><span>"</span><span>str_1 : $str_12 <br></span><span>"</span><span>;    这句如果变量后面跟着非空格的字符,会让编译器糊涂,会把$str_12当成变量,而上面又没有定义,<img src="http://image.codes51.com/Article/image/20160304/20160304145125_8260.jpg" alt="php data types">因此会输出空值。为了解决这个问题可以用${str_1}</span><span>2</span><span> 或{$str_1}</span><span>2</span><span> 这个来表达。</span></span></span></span></span></span></span></p>
Copy after login

php data types

Use PHP functions to compare variables $x
Expression gettype() empty() is_null() isset() boolean : if($x)
$x = ""; string TRUE FALSE TRUE FALSE
$x = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$x is undefined N ULL TRUE TRUE FALSE FALSE
$x = array(); array TRUE FALSE TRUE FALSE
$x = false; boolean TRUE FALSE TRUE FALSE
$x = true; boolean FALSE FALSE TRUE TRUE
$x = 1; integer F ALSE FALSE TRUE TRUE
$x = 42; integer FALSE FALSE TRUE TRUE
$x = 0; integer TRUE FALSE TRUE FALSE
$x = -1; integer FALSE FALSE TRUE TRUE
$x = "1"; string FALSE F ALSE TRUE TRUE
$x = "0"; string TRUE FALSE TRUE FALSE
$x = "-1" " string FALSE FALSE TRUE TRUE
$x = "true"; string FALSE FALSE TRUE TRUE
$x = "false"; string FALSE FALSE TRUE TRUE
Loose comparison==
TRUE FALSE 1 0 -1 "1" "0 " "-1" NULL array() "php" ""
TRUE TRUE FALSE TRUE FALSE TRUE TRUE FALSE TRUE FALSE FALSE TRUE FALSE
FALSE FALSE TRUE FALSE TRUE FALSE FALSE TRUE FALSE TRUE TRUE FALSE TRUE
1 TRUE FALSE TRUE FALSE FALSE FALSE 0 FALSE TRUE FALSE TRUE FALSE
FALSE TRUE FALSE TRUE FALSE TRUE TRUE -1 TRUE FALSE FALSE FALSE TRUE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE "1" TRUE FALSE TRUE FALSE FALSE
TRUE FALSE FALSE FALSE FALSE FALSE FALSE "0" FALSE TRUE FALSE TRUE FALSE
FALSE TRUE FALSE FALSE FALSE FALSE FALSE "-1" TRUE FALSE FALSE FALSE TRUE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE NULL FALSE TRUE FALSE TRUE FALSE
FALSE FALSE FALSE TRUE TRUE FALSE TRUE
array() FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
"php" TRUE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE
Strict comparison===
TRUE FALSE 1 0 - 1 "1" " 0" "-1" NULL array() "php" ""
TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
1 FAL SE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
0 FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
-1 FALSE FALSE FALSE FALSE TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
"1" FALSE FALSE FALSE FALSE FALSE FALSE "0 " FALSE FALSE FALSE FALSE FALSE
FALSE TRUE FALSE FALSE FALSE FALSE FALSE "-1" FALSE FALSE FALSE FALSE FALSE
FALSE FALSE TRUE FALSE FALSE FALSE FALSE NULL FALSE FALSE FALSE FALSE FALSE
FALSE FALSE FALSE TRUE FALSE FALSE FALSE
array() FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE
"php" FALSE F ALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE FALSE
"" FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE TRUE

The above introduces the PHP data types, including aspects of the content. I hope it will be helpful to friends who are interested in PHP tutorials.

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