本章说说变量的定义。
如果对于变量和常量的定义,你会注意几个方面呢?你可能会想到:
如何定义变量,它和C# 等语言有什么不同呢? 变量区分大小写吗? PHP的变量还有其他重要的吗?
常量和变量的定义一样吗? 分别讲述吧。
1.如何定义变量,它和C# 等语言有什么不同呢?
PHP 中的变量用一个 美元符号后面跟变量名来表示。变量名是 区分大小写的。例如:
$var = ' Jim ' ;
$VAR = ' Kimi;
echo "$var,$VAR";//输出“Jim,Kimi"
?>
2. 变量区分大小写吗?
如 1里说的,区分大小写。
注意,需要说明的一点是自PHP4以来,引入了引用赋值的概念,其实和多数语言的引用类似,不过我觉得最类似的是C/C++.因为它也用到了"&"符号。例如:
1 php
2 $foo = ' Bob ' ; // 赋值'Bob'给foo
3 $bar = & $foo ; // 通过$bar引用.注意&符号
4 $bar = " My name is $bar " ; // 修改 $bar
5 echo $bar ;
6 echo $foo ; // $foo 也修改了.
7 ?>
3. PHP其他重要点
预定义变量
预定义变量在PHP是一个重要的概念。 PHP 提供了大量的预定义变量。由于许多这些变量依赖于运行的服务器的版本和设置,及其它因素,所以并没有详细的说明文档。一些预定义变量在 PHP 以命令行形式运行时并不生效。
需要注意的是 在PHP 4.2.0 以及后续版本中,PHP 指令 register_globals 的默认值为 off。这是 PHP 的一个主要变化。让 register_globals 的值为 off 将影响到预定义变量集在全局范围内的有效性。例如,为了得到 DOCUMENT_ROOT 的值,将必须使用 $_SERVER['DOCUMENT_ROOT'] 代替 $DOCUMENT_ROOT,又如,使用 $_GET['id'] 来代替 $id 从 URL http://www.example.com/test.php?id=3 中获取 id 值,亦或使用 $_ENV['HOME'] 来代替 $HOME 获取环境变量 HOME 的值。
从 PHP 4.1.0 开始,PHP 提供了一套附加的预定数组,这些数组变量包含了来自 web 服务器(如果可用),运行环境,和用户输入的数据。这些数组非常特别,它们在全局范围内自动生效,例如,在任何范围内自动生效。因此通常被称为自动全局变量(autoglobals)或者超全局变量(superglobals)。(PHP 中没有用户自定义超全局变量的机制。)超全局变量罗列于下文中;而且,你也将注意到旧的预定义数组( $HTTP_*_VARS)仍旧存在。自 PHP 5.0.0 起,长格式的 PHP 预定义变量可以通过设置 register_long_arrays 来屏蔽。
下表是PHP的超全局变量:
超全局变量 | 描述 |
$GLOBALS | 包含一个引用指向每个当前脚本的全局范围内有效的变量。该数组的键名为全局变量的名称。从 PHP 3 开始存在 $GLOBALS 数组。 |
$_SERVER | 变量由 web 服务器设定或者直接与当前脚本的执行环境相关联。类似于旧数组 $HTTP_SERVER_VARS 数组(依然有效,但反对使用)。 |
$_GET | 经由 URL 请求提交至脚本的变量。类似于旧数组 $HTTP_GET_VARS 数组(依然有效,但反对使用)。 |
$_POST | 经由 HTTP POST 方法提交至脚本的变量。类似于旧数组 $HTTP_POST_VARS 数组(依然有效,但反对使用)。 |
$_COOKIE | 经由 HTTP Cookies 方法提交至脚本的变量。类似于旧数组 $HTTP_COOKIE_VARS 数组(依然有效,但反对使用)。 |
$_FILES | 经由 HTTP POST 文件上传而提交至脚本的变量。类似于旧数组 $HTTP_POST_FILES 数组(依然有效,但反对使用) |
$_ENV | 执行环境提交至脚本的变量。类似于旧数组 $HTTP_ENV_VARS 数组(依然有效,但反对使用)。 |
$_REQUEST | 经由 GET,POST 和 COOKIE 机制提交至脚本的变量,因此该数组并不值得信任。所有包含在该数组中的变量的存在与否以及变量的顺序均按照 php.ini 中的 variables_order 配置指示来定义。此数组在 PHP 4.1.0 之前没有直接对应的版本。参见 import_request_variables()。 |
$_SESSION | 当前注册给脚本会话的变量。类似于旧数组 $HTTP_SESSION_VARS 数组(依然有效,但反对使用) |
每个变量都有应用范围,那么PHP是怎么定义的呢?还是先看看下面代码吧:
1 php
2 $var = 0 ;
3 function test( $index )
4 {
5 $var = $var + 1 ;
6 echo " The " . $index . " number is " . $var . "
" ;
7 }
8 test( 1 );
9 test( 2 )
10 ?>
如果你认为是下面:
结果1:
The 1 number is 1
The 2 number is 2
其实正确的结果应该是:
结果2
The 1 number is 1
The 2 number is 1
1 php
2 $var = 0 ;
3 function test( $index )
4 {
5 global $var ;
6 $var = $var + 1 ;
7 echo " The " . $index . " number is " . $var . "
" ;
8 }
9 test( 1 );
10 test( 2 )
11 ?>
那么还有没有其他方法呢?答案是肯定的。
代码如下:
1 php
2 $var = 0 ;
3 function test( $index )
4 {
5
6 $GLOBALS [ " var " ] = $GLOBALS [ " var " ] + 1 ;
7 echo " The " . $index . " number is " . $GLOBALS [ " var " ] . "
" ;
8 }
9 test( 1 );
10 test( 2 )
11 ?>
PHP也有静态变量的说法。不过静态变量一般用在函数里,只能是局部变量了。看看下面代码吧:
1 php
2 function Test()
3 {
4 static $a = 0 ;
5 echo $a . "
" ;
6 $a ++ ;
7 }
8 Test();
9 Test();
10 ?>
1
2
PHP还有一个相当令人兴奋的特性: 可变变量
所谓可变变量,就是一个变量的变量名可以动态的设置和使用。
看看下面的例子:
1 php
2 $a = " hello " ;
3 $hello = " world " ;
4 echo $a . " " . $ $a ;
5 ?>
变量就这多了。下面看看常量。
常量
PHP的常量是不是前面加const呢?让我们看一看。
不是的。在PHP必须用下面的方式定义。
bool define ( string name, mixed value [, bool case_insensitive] )
name 为常量名,value为常量的值。case_insensitive]为大小写敏感。默认为敏感。例如:
1 php
2 define ( " CONSTANT " , " Hello world. " );
3 echo CONSTANT ; // outputs "Hello world."
4 echo Constant ; // outputs "Constant" and issues a notice.
5
6 define ( " GREETING " , " Hello you. " , true );
7 echo GREETING; // outputs "Hello you."
8 echo Greeting; // outputs "Hello you."
9
10 ?>
常量和变量不同:
常量前面没有美元符号($);
常量只能用 define() 函数定义,而不能通过赋值语句;
常量可以不用理会变量范围的规则而在任何地方定义和访问;
常量一旦定义就不能被重新定义或者取消定义;
常量的值只能是标量。

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

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.


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

Atom editor mac version download
The most popular open source editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

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

SublimeText3 English version
Recommended: Win version, supports code prompts!

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