php学习笔记之--php变量与常量
php变量与常量
一、目录
第一章 变量
1.1变量的声明
1.2变量的命名
1.3变量的数据类型
1.4变量数据类型之间的转换
1.5与变量和类型有关的一些常用函数
第二章 常量
2.1常量的声明与使用
2.1预定义常量和魔术常量
第一章 变量
1.1变量的声明
1.变量以$开头定义,由$和标识符组成,标识符就是变量的名称。(函数和类的名称也是标识符)
$变量名=值
1.2变量的命名
1. 变量前一定要使用”$”, 声明和使用都要有这个符号。
2. 可以是任意长度,有任何字母、数字、下划线组成;但是不能以数字开头
3.不能使用PHP的运算符号+ - * / % & .
4. PHP可以使用系统关键字作为变量名
5. PHP变量区分大小写,(只有变量和常l量区分大小写,其它不区分)
6.变量名称一定要有意义,可以使用英文单词,也可以使用汉语拼音。
7.$aaaBbbCcc 变量的命名风格,驼峰法。
注意:可变变量(一个变量的变量名可以动态设置和使用)
变量的引用赋值(使用一个“&”符号加到将要赋值的变量前面(源变量))
数组与类使用PHP的可变变量名的注意
可以像C++那样, 在变量的前面加上&, 例如:$a = &$b 对一个变量进行值传递。
1.3变量的数据类型
1.PHP是弱类型的语言,变量的类型由存储的值决定
2.PHP中共有8种类型
四种标量类型:
整 型:int integer
布尔型:bool boolean
浮点型:float, double, real
字符串:string
两种复合类型
数组: array
对象: object
两种特殊类型
资源类型:resource
空类型:null
注:
给一个变量赋值为0123,但是输出该变量的值总是为其他数字,请问这是什么问题?PHP 解释器会把以0开始的数字当做是八进制的,所以它的值会变成八进制的。
$a = 0.2+0.7;$b = 0.9;var_dump($a == $b);打印出的结果是:bool(false)。也就是说在这里 0.2+0.7 的计算结果与 0.9 并不相等。请问这个问题如何解决?
PHP官方手册说明:显然简单的十进制分数如 0.2 不能在不丢失一点点精度的情况下转换为内部二进制的格式。这和一个事实有关,那就是不可能精确的用有限位数表达某些十进制分数。例如,十进制的 1/3 变成了 0.3333333...。我们将上面的变量用双精度格式打印出来:$a = 0.2+0.7;$b = 0.9;printf("%0.20f", $a);echo '
';printf("%0.20f", $b);
输出结果如下:
0.899999999999999911180.90000000000000002220
显然在这里,实际上作为浮点型数据,其精度已经损失了一部分,达不到完全精确。所以永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相等。需要说明的是,这不是PHP的问题,而是计算机内部处理浮点数的问题!在 C、JAVA 等语言中也会遇到同样的问题。
所以要比较两个浮点数,需要将其控制在我们需要的精度范围内再行比较,因此使用 bcadd() 函数来对浮点数想加并进行精度转换(为字符串):
var_dump(bcadd(0.2,0.7,1) == 0.9); // 输出:bool(true) 浮点数取整
在《PHP 取整函数 ceil 与 floor》一文中,曾有例子:
经过上面对浮点数计算的探讨,知道这是浮点数计算结果不完全精确造成的:
经过上面对浮点数计算的探讨,知道这是浮点数计算结果不完全精确造成的,因此使用 round() 函数处理一下即可:
虽然 round() 函数是按照指定的精度进行四舍五入,但保留小数点后一位,对我们的取整结果是没影响的。
1.4变量数据类型之间的转换
一种是强制转换:
1.setType(变量, 类型); //这个函数将原变量的类型改变
2.$a=(int)"123abc"; //在赋值前使用(类型)的形式, 不会改变原变量的类型
3.$变量=intval(变量或值); //形成一个新的变量,原有的变量不变
注意:
1.bool型的转换
2.整型在内存中占4个字节32位 最大2.147e9
3.浮点型在内存中占8个字节64位
4.整型与浮点型之间转换时的溢出/精度损失
5.字符串与整型和浮点型之间的转换
一种是自动转换:
最常用的方式,因为这种我们开发时不用去管理类型,变量会根据运行环境自动转换
4.PHP中数据类型转换中的注意
$str="100.123abc";
setType( $str,bool);
var_dump( $str);
整型 为4个字节2个字32位2.147e9
浮点型 为8个字节4个字64位
$float1=123.456;
$float2=2.147e9;
$int1=(int) $float1;
$int2=(int) $float3;
var_dump( $int1);
var_dump( $int2);
(int)(10*(0.7+0.1)) // 结果为7
--------
$amount = 19.99 * 100;
printf("%.13f", $amount);
---------
$num=-1000;
print( $num."\n");
$i_str= sprintf("%u", $num);
print( $i_str."\n");
$i1= intval( $i_str);
print( $i1."\n");
$i2= intval( floatval( $i_str));
print($i2."\n");
输出结果
-1000
4294966296
2147483647
-1000
整型=浮点型 注意:溢出/精度损失
5.字符串转换为整型和浮点
$a="100abc"; $a="a100bc";
$a="abc"; $a="100.123zbc"
$a="100eabc"; $a="100e5abc"
$a=10;
$b="100abc";
$c= true;
$d=12.34;
$sum= $a+ $b+ $c+ $d;
var_dump($sum);
1.5与变量和类型有关的一些常用函数
isset(); //测试变量是否存在,返回的值值如果是null,也表示空
empty(); //判断一个变量是否为空, “” null
unset();
setType();
getType();
var_dump();
变量类型测试函数:
is_bool()
is_int() is_integer() is_long()
is_string()
is_float() is_double()is_real()
is_array()
is_object()
is_resource()
is_null()
is_scalar()
is_numberic()
is_callable()
第二章 常量
2.1常量的声明与使用
1.常量是一个简单值的标识符 2.常量定义后不能再改变他的值,也不能使用unset()取消 3.常量可以不用理会变量范围的规则而在任何地方都可以定义和访问 4.声明常量使用define("常量名",值); 5.常量声明名在声明和使用都不使用"$" 6.常量名称习惯都使用大写 7.常量的值只能用标量类型(int, float, bool, string) 8.常量一定要在声明时就给值 9.defined("常量"); //查看某个常量是否存在
2.2预定义常量和魔术常量
$_GET[];
$_POST[];
$_REQUEST[];
$_COOKIE[];
$_SESSION[];
$_FILES[]; //获取上传表单数据
$_SERVER[];
$_ENV[];

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.
