PHP 或超文本预处理器是一种基于 Web 的应用程序开发编程语言,可以在其中合并 HTML 编码以构建 Web 应用程序。在 PHP 中,有八种不同的数据类型用于在脚本中声明和调用变量。它们是用于真或假值的“布尔”,用于数字值的“整数”,用于十进制数字的“浮点/双精度”,用于字符的“字符串”,用于固定元素大小的“数组”,用于表示类实例的“对象” ,'NULL' 表示 void,'resources' 表示 PHP 脚本外部的元素。
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
前 3 种 PHP 数据类型
用于存储值的 PHP 变量可能与各种数据类型相关联,从最简单的 int 到更复杂的数据类型(例如数组)。 PHP 被称为松散类型编程语言,这意味着变量数据类型是在运行时根据其属性决定的,并且没有显式定义。它分析给定值的属性,然后确定分配给它的数据类型。 PHP 支持 8 种原始数据类型,可进一步分为以下 3 种:
让我们通过示例详细了解每一个。
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); ?>
输出:
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; ?>
输出:
c.浮动/双
具有小数点或指数的数字称为浮点数/实数。它可以有正数和负数。该数字应显示预定义的小数位数。
代码:
<?php $dec1 = 0.134; var_dump($dec1); $exp1 = 23.3e2; var_dump($exp1); $exp2 = 6E-9; var_dump($exp2); ?>
输出:
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; ?>
输出:
2.复合类型
这些是无法分配新值的。数组和对象都属于这一类。
a.数组
它是一种数据结构,具有固定大小的具有相似数据类型的元素的集合。它还用于以有序映射的形式存储已知数量的键值对。它可用于多种用途,如列表、哈希表(映射实现)、集合、堆栈、字典、队列等;多维数组也是可能的。
一个简单的数组示例如下:
代码:
<?php $animals = array("Dog", "Cat", "Cow"); var_dump($animals); $animal_babies = array( "Dog" => "Puppy", "Cat" => "Kitten", "Cow" => "Calf" ); var_dump($animal_babies); ?>
输出:
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); ?>
输出:
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:
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中文网其他相关文章!

Thedifferencebetweenunset()andsession_destroy()isthatunset()clearsspecificsessionvariableswhilekeepingthesessionactive,whereassession_destroy()terminatestheentiresession.1)Useunset()toremovespecificsessionvariableswithoutaffectingthesession'soveralls

stickysessensureuserRequestSarerOutedTothesMeServerForsessionDataConsisterency.1)sessionIdentificeAssificationAssigeaSsignAssignSignSuserServerServerSustersusiseCookiesorUrlModifications.2)一致的ententRoutingDirectSsssssubsequeSssubsequeSubsequestrequestSameSameserver.3)loadBellankingDisteributesNebutesneNewuserEreNevuseRe.3)

phpoffersvarioussessionsionsavehandlers:1)文件:默认,简单的ButMayBottLeneckonHigh-trafficsites.2)Memcached:高性能,Idealforsforspeed-Criticalapplications.3)REDIS:redis:similartomemememememcached,withddeddeddedpassistence.4)withddeddedpassistence.4)databases:gelifforcontrati forforcontrati,有用

PHP中的session是用于在服务器端保存用户数据以在多个请求之间保持状态的机制。具体来说,1)session通过session_start()函数启动,并通过$_SESSION超级全局数组存储和读取数据;2)session数据默认存储在服务器的临时文件中,但可通过数据库或内存存储优化;3)使用session可以实现用户登录状态跟踪和购物车管理等功能;4)需要注意session的安全传输和性能优化,以确保应用的安全性和效率。

PHPsessionsstartwithsession_start(),whichgeneratesauniqueIDandcreatesaserverfile;theypersistacrossrequestsandcanbemanuallyendedwithsession_destroy().1)Sessionsbeginwhensession_start()iscalled,creatingauniqueIDandserverfile.2)Theycontinueasdataisloade

绝对会话超时从会话创建时开始计时,闲置会话超时则从用户无操作时开始计时。绝对会话超时适用于需要严格控制会话生命周期的场景,如金融应用;闲置会话超时适合希望用户长时间保持会话活跃的应用,如社交媒体。

服务器会话失效可以通过以下步骤解决:1.检查服务器配置,确保会话设置正确。2.验证客户端cookies,确认浏览器支持并正确发送。3.检查会话存储服务,如Redis,确保其正常运行。4.审查应用代码,确保会话逻辑正确。通过这些步骤,可以有效诊断和修复会话问题,提升用户体验。

session_start()iscucialinphpformanagingusersessions.1)ItInitiateSanewsessionifnoneexists,2)resumesanexistingsessions,and3)setsasesessionCookieforContinuityActinuityAccontinuityAcconActInityAcconActInityAcconAccRequests,EnablingApplicationsApplicationsLikeUseAppericationLikeUseAthenticationalticationaltication and PersersonalizedContentent。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

记事本++7.3.1
好用且免费的代码编辑器

Dreamweaver Mac版
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

SublimeText3 英文版
推荐:为Win版本,支持代码提示!