首页  >  文章  >  后端开发  >  PHP 数据类型

PHP 数据类型

WBOY
WBOY原创
2024-08-29 12:37:56916浏览

PHP 或超文本预处理器是一种基于 Web 的应用程序开发编程语言,可以在其中合并 HTML 编码以构建 Web 应用程序。在 PHP 中,有八种不同的数据类型用于在脚本中声明和调用变量。它们是用于真或假值的“布尔”,用于数字值的“整数”,用于十进制数字的“浮点/双精度”,用于字符的“字符串”,用于固定元素大小的“数组”,用于表示类实例的“对象” ,'NULL' 表示 void,'resources' 表示 PHP 脚本外部的元素。

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

前 3 种 PHP 数据类型

用于存储值的 PHP 变量可能与各种数据类型相关联,从最简单的 int 到更复杂的数据类型(例如数组)。 PHP 被称为松散类型编程语言,这意味着变量数据类型是在运行时根据其属性决定的,并且没有显式定义。它分析给定值的属性,然后确定分配给它的数据类型。 PHP 支持 8 种原始数据类型,可进一步分为以下 3 种:

让我们通过示例详细了解每一个。

PHP 数据类型

1.标量类型

它们可以进一步分为以下原始类型:

a.布尔

这些类型的可能输出为 0 或 1,即 true 或 false。它们用于条件测试用例,其中满足条件时事件返回 true,不满足时事件返回 false。它还将 NULL 和空字符串视为 false。

代码:

<?php
// TRUE is assigned to a variable value
$variable_value = true;
var_dump($variable_value);
?>

输出:

PHP 数据类型

b.整数

整数数据类型保存 -2,147,483,648 和 2,147,483,647 之间的非十进制整数值。该最大值和最小值取决于系统,无论是 32 位还是 64 位。通过使用常量 PHP_INT_MAX,我们可以找出最大值。它还保存以 10 为基数、以 8 为基数和以 6 为基数的值。

代码:

<?php
// example for decimal (base 10)
$dec1 = 100;
$dec2 = 200;
// example for decimal (base 8)
$oct1 = 10;
// example for decimal (base 6)
$hex1 = 0x15;
$addn = $dec1 + $dec2;
echo $addn;
?>

输出:

PHP 数据类型

c.浮动/双

具有小数点或指数的数字称为浮点数/实数。它可以有正数和负数。该数字应显示预定义的小数位数。

代码:

<?php
$dec1 = 0.134;
var_dump($dec1);
$exp1 = 23.3e2;
var_dump($exp1);
$exp2 = 6E-9;
var_dump($exp2);
?>

输出:

PHP 数据类型

d.字符串

字符串数据类型基本上是字符的集合,包括数字、字母和字母。它们可以保存高达 2GB 的值。如果必须在字符串中显示变量,则应使用双引号声明它们。另外,单引号也可以。

代码:

<?php
$name = "Jay";
$str1 = 'Declaring name in single quote as $name';
echo $str1;
echo "\n";
$str2 = "Declaring name in double quote as $name";
echo $str2;
echo "\n";
$str3 = 'Just a string';
echo $str3;
?>

输出:

PHP 数据类型

2.复合类型

这些是无法分配新值的。数组和对象都属于这一类。

a.数组

它是一种数据结构,具有固定大小的具有相似数据类型的元素的集合。它还用于以有序映射的形式存储已知数量的键值对。它可用于多种用途,如列表、哈希表(映射实现)、集合、堆栈、字典、队列等;多维数组也是可能的。

一个简单的数组示例如下:

代码:

<?php
$animals = array("Dog", "Cat", "Cow");
var_dump($animals);
$animal_babies = array(
"Dog" => "Puppy",
"Cat" => "Kitten",
"Cow" => "Calf"
);
var_dump($animal_babies);
?>

输出:

PHP 数据类型

b.对象

它允许存储数据(称为其属性)并提供有关如何处理数据(称为对象的方法)的信息。对象充当类的实例,该类用作其他对象的模板。关键字“new”用于创建对象。

每个对象都继承父类的属性和方法。它需要每个对象中有一个显式声明和一个“类”。

代码:

<?php
// Declaring a class
class statement{
// properties
public $stmt = "Insert any string here";
// Declaring a method
function show_statement(){
return $this->stmt;
}
}
// Creation of new object
$msg = new statement;
var_dump($msg);
?>

输出:

PHP 数据类型

3.特殊类型

PHP 中有 2 种特殊的数据类型属于此类,因为它们是唯一的。他们是:

a. NULL

In PHP, this special NULL is used for representing empty variables, i.e. the variable has no data in it, and NULL is the only possible value to it. If it has been set to unset() or if no value has been set to it, a variable assigned to the constant NULL becomes a NULL data type.

Here we are setting NULL directly to val1. For the val2 variable, we are assigning a string value first and then setting it as NULL. In both cases, the final value of variables is NULL.

Code:

<?php
$val1 = NULL;
var_dump($val1);
echo "<br>";
$val2 = "Any string";
$val2 = NULL;
var_dump($val2);
?>

Output:

PHP 数据类型

b. Resources

A resource is not an actual data type, whereas it is a special variable that keeps a reference to a resource external to PHP. They hold special handlers for files and database connections that are open. Special functions usually create and use these resources.

To run this code, we must have the file.txt created in the system with reading permission given to it. It throws an error in case “handle” is not a resource. Also, make sure to connect to any existing database in your system.

Code:

<?php
// Open an existing file to read
$handle = fopen("file.txt", "r");
var_dump($handle);
echo "<br>";
// Connecting to MySQL database server with settings set to default
$db = mysql_connect("localhost", "root", "");
var_dump($db);
?>

Apart from the above data types, we also have something called pseudo-types: the keywords in PHP document used to indicate the types or values that an argument can have. Some of them are:

  • mixed: They allow a parameter to accept more than one type. Ex: gettype()
  • number: With a number, a parameter can be afloat or an integer.
  • void, callback, array|object are some of the other pseudo-types

Conclusion

Here we have covered almost all of the data types which are available in PHP. All of the above 8 primitive types are implicitly supported by PHP, and there is no need for the user to specify them manually. Arrays and objects can hold multiple values, whereas, for rest, all can hold only a single value (except NULL, which holds no value).

以上是PHP 数据类型的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:Phalcon Model下一篇:PHP Integer