在php中检查变量类型的方法很简单,利用gettype()函数就可返回当前变量类型的了,下面我来给各位朋友详细介绍如何利用gettype函数检查变量类型,有需要了解的朋友可参考.
string gettype ( mixed $var ) 返回 PHP 变量的类型 var.
实例代码如下:
<?php function get_type($var) { if (is_object($var)) return get_class($var); if (is_null($var)) return 'null'; if (is_string($var)) return 'string'; if (is_array($var)) return 'array'; if (is_int($var)) return 'integer'; if (is_bool($var)) return 'boolean'; if (is_float($var)) return 'float'; if (is_resource($var)) return 'resource'; //throw new NotImplementedException(); return 'unknown'; } ?>
官方说:不要使用 gettype() 来测试某种类型,因为其返回的字符串在未来的版本中可能需要改变.此外,由于包含了字符串的比较,它的运行也是较慢的.
使用 is_* 函数代替.实例代码如下:
<?php /** * Returns the type of the var passed. * * @param mixed $var Variable * @return string Type of variable */ function myGetType($var) { if (is_array($var)) return "array"; if (is_bool($var)) return "boolean"; if (is_float($var)) return "float"; if (is_int($var)) return "integer"; if (is_null($var)) return "NULL"; if (is_numeric($var)) return "numeric"; if (is_object($var)) return "object"; if (is_resource($var)) return "resource"; if (is_string($var)) return "string"; return "unknown type"; } ?>
其它的些变量类型判断收藏
array_key_exists(mixed key, array search) ://检查给定的键名或索引是不是存在于数组中判断数据类型 is_numeric ( mixed var ): //检验测定变量是不是为数码或数码字符串 is_bool($ var): //检验测定变量是不是是布尔型 is_float($ var): //检验测定变量是不是是浮点型 和is_double,is_real()都同样的用法 is_int($ var): //检验测定变量是不是是整儿is_integer()同样的用法 is_string($ var): //检验测定变量是不是是字符串 is_object($ var): //检验测定变量是不是是一个对象 is_array($ var): //检验测定变量是不是是数组 is_null($ var): //检验测定变量是不是为 null
教程链接:
随意转载~但请保留教程地址★

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.