PHP学习笔记(三):数据类型转换与常量介绍,学习笔记常量
一、PHP数据类型相互转换
1、强制转换
复制代码 代码如下:
// bool,int,float,string,array,object,null
bool settype ( mixed $var , string $type )
1)会改变原变量的类型
复制代码 代码如下:
$a= "123a"; // string
settype($a, "int"); // 勿遗漏双引号
var_dump($a);
2)在赋值前定义,不改变原变量类型
复制代码 代码如下:
$a = "123abc";
$b = (int)"123abc";
var_dump($a);
Tips:避免内存溢出,整型4字节(2.147E9),浮点型8字节
2、自动转换
根据运行环境自动转换,例如加号“+”。如果任何一个操作数是浮点数,则所有的操作数都被当成浮点数,结果也是浮点数。
复制代码 代码如下:
$foo = “0″; // $foo 是字符串 (ASCII 48)
$foo += 2; // $foo 现在是一个整数 (2)
$foo = $foo + 1.3; // $foo 现在是一个浮点数 (3.3)
$foo = 5 + “10 Small Pigs”; // $foo 是整数 (15)
3、变量测试函数
复制代码 代码如下:
is_bool()、is_numeric()、is_float()、is_int()...
is_scalar() //检测变量是否是一个标量
二、常量的声明与使用
复制代码 代码如下:
//定义后不能改变,可在任意地方访问
//默认大小写敏感,习惯大写
//常量值只能用标量(int、float、string、bool)
//常量必须声明时就赋值
define("常量名","值")
检查常量是否存在
复制代码 代码如下:
bool defined ( string $name )
var_dump(defined('a')); //勿遗漏了引号

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

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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.

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.
