PHP - 变量类型

PHP - 变量类型 首页 / PHP入门教程 / PHP - 变量类型

PHP共有八种数据类型,可用于构造变量-

  • Integer       - 整数类型.

  • Doubles     - 浮点类型.

  • Booleans    - 布尔类型.

  • NULL         - 空类型

  • String         - 字符串类型

  • Arrays        - 数组类型。

  • Objects      - 对象类型。

  • Resources  - 特殊资源变量,用于保存对PHP外部资源(如数据库连接)的引用。

整数类型

它们是整数,没有小数点,如4195。它们对应于简单的整数,包括正数和负数, 整数可以分配给变量,也可以在表达式中使用,就像这样-

$int_var=12345;
$another_int=-12345 + 12345;

整数可以是十进制(以10为底),八进制(以8为底)和十六进制(以16为底)格式。十进制格式是默认值,八进制整数以前导0表示,十六进制以前导0x表示。

浮点类型

如,3.14159或49.1。

<?php
   $many=2.2888800;
   $many_2=2.2111200;
   $few=$many + $many_2;
   
   print("$many + $many_2=$few <br>");
?>

它产生以下浏览器输出-

2.28888 + 2.21112=4.5

布尔类型

它们只有两个可能的值true或false。 PHP提供了两个常量,尤其是用作布尔值的常量:TRUE和FALSE,可以这样使用-

if (TRUE)
   print("This will always print<br>");

else
   print("This will never print<br>");

0、""、"0"、NULL、[] 表示false,其它为true

无涯教程网

$true_num=3 + 0.14159;
$true_str="Tried and true"
$true_array[49]="An array element";
$false_array=array();
$false_null=NULL;
$false_num=999 - 999;
$false_str="";

空类型

NULL是一种只有一个值的特殊类型:NULL,要为变量提供NULL值,只需像这样分配它-

$my_var=NULL;

特殊常量NULL由约定大写,但实际上不区分大小写;您也可以输入-

$my_var=null;

已分配为NULL的变量具有以下属性-

  • 在布尔类型中为FALSE。

  • 使用IsSet()函数进行检查时,它将返回FALSE。

字符串类型

它们是字符序列,以下是字符串的有效示例

$string_1="This is a string in double quotes";
$string_2='This is a somewhat longer, singly quoted string';
$string_39="This string has thirty-nine characters";
$string_0=""; //a string with zero characters

单引号的字符串几乎按字面意义处理,而双引号的字符串则用变量的值替换变量,并专门解释某些字符序列。

<?php
   $variable="name";
   $literally='My $variable will not print!';
   
   print($literally);
   print "<br>";
   
   $literally="My $variable will print!";
   print($literally);
?>

这将产生以下输出-

My $variable will not print!
My name will print

用双引号定界的字符串(如“ this”)由PHP通过以下两种方式进行预处理-

  • 以反斜杠(\)开头的某些字符序列已替换为特殊字符

  • 变量名(以$开头)被替换为其值的字符串表示形式。

多行文档

您可以使用此处文档将多行分配给单个字符串变量-

<?php
   $channel =<<<_XML_
   
   <channel>
      <title>What's For Dinner</title>
      <link>http://menu.example.com/</link>
      <description>Choose what to eat tonight.</description>
   </channel>
   _XML_;
   
   echo <<<END
   This uses the "here document" syntax to output multiple lines with variable 
   interpolation. Note that the here document terminator must appear on a line with 
   just a semicolon. no extra whitespace!
   
   END;
   
   print $channel;
?>

这将产生以下输出-

This uses the "here document" syntax to output
multiple lines with variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!

<channel>
<title>What's For Dinner<title>
<link>http://menu.example.com/<link>
<description>Choose what to eat tonight.</description>

变量范围

范围可以定义为变量在声明它的程序中所具有的可用性范围。 PHP变量可以是四种范围类型之一-

局部变量

在函数中声明的变量被认为是局部变量(Local),也就是说,只能在该函数中引用它。

<?php
   $x=4;
   
   function assignx () { 
      $x=0;
      print "\$x inside function is $x. <br />";
   }
   
   assignx();
   print "\$x outside of function is $x. <br />";
?>

这将产生以下输出-

$x inside function is 0. 
$x outside of function is 4. 

函数变量

函数(Function)参数在函数名称后和括号内声明。

<?php
   //multiply a value by 10 and return it to the caller
   function multiply ($value) {
      $value=$value * 10;
      return $value;
   }
   
   $retval=multiply (10);
   Print "Return value is $retval\n";
?>

这将产生以下输出-

Return value is 100

全局变量

与局部变量相反,可以在程序的任何部分访问全局(Global)变量。通过将关键字 GLOBAL 放置在应被识别为全局变量的前面.

<?php
   $somevar=15;
   
   function addit() {
      GLOBAL $somevar;
      $somevar++;
      
      print "Somevar is $somevar";
   }
   
   addit();
?>

这将产生以下输出-

Somevar is 16

静态变量

静态(Static)变量在函数退出时值不会丢失,您只需将关键字STATIC放在变量名称的前面,即可将变量声明为静态变量。

<?php
   function keep_track() {
      STATIC $count=0;
      $count++;
      print $count;
      print "<br />";
   }
   
   keep_track();
   keep_track();
   keep_track();
?>

这将产生以下输出-

1
2
3

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

趣谈网络协议 -〔刘超〕

10x程序员工作法 -〔郑晔〕

浏览器工作原理与实践 -〔李兵〕

Redis核心技术与实战 -〔蒋德钧〕

深度学习推荐系统实战 -〔王喆〕

Python自动化办公实战课 -〔尹会生〕

技术面试官识人手册 -〔熊燚(四火)〕

如何落地业务建模 -〔徐昊〕

LangChain 实战课 -〔黄佳〕

好记忆不如烂笔头。留下您的足迹吧 :)