PHP变量详解(1)
基础
PHP 中一个美元符号后面跟上一个变量名称,即表示一个变量。变量的名称是对大小写敏感的。
变量名与 PHP 中其它的标签一样遵循相同的规则。一个有效的变量名由字母或者下划线开头,后面跟上任意数量的字母,数字,或者下划线。按照正常的正则表达式,它将被表述为:'[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*'
注: 字母为 a-z,A-Z,ASCII 字符从 127 到 255(0x7f-0xff)。
<?php$var = "Bob";$Var = "Joe";echo "$var, $Var"; // outputs "Bob, Joe"$4site = 'not yet'; // invalid; starts with a number$_4site = 'not yet'; // valid; starts with an underscore$t?yte = 'mansikka'; // valid; '洄 is (Extended) ASCII 228.?> Copy after login |
PHP 3 中,变量总是传值赋值。那也就是说,当你将一个表达式的值赋予一个变量时,整个原始表达式的值被赋值到目标变量。这意味着,例如,当一个变量的值赋予另外一个变量时,改变其中一个变量的值,将不会影响到另外一个变量。有关这种类型的赋值操作,请参阅表达式一章。
PHP 4 提供了另外一种方式给变量赋值:传地址赋值。这意味着新的变量简单的引用(换言之,“成为其别名” 或者 “指向”)了原始变量。改动新的变量将影响到原始变量,反之亦然。这同样意味着其中没有执行复制操作;因而,这种赋值操作更加快速。尽管如此,任何提速的操作只有在紧密循环或者大数组或者对象才可能被注意到。
使用传地址赋值,简单地追加一个(&)符号到将要赋值的变量前(源变量)。例如,下列代码片断两次输出‘My name is Bob’:
<?php$foo = 'Bob'; // Assign the value 'Bob' to $foo$bar = &$foo; // Reference $foo via $bar.$bar = "My name is $bar"; // Alter $bar...echo $bar;echo $foo; // $foo is altered too.?> Copy after login |
需要注意的是只有命名变量才可以传地址赋值,这一点非常重要。
<?php$foo = 25;$bar = &$foo; // This is a valid assignment.$bar = &(24 * 7); // Invalid; references an unnamed expression.function test(){ return 25;}$bar = &test(); // Invalid.?> Copy after login |
预定义变量
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

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

AI Hentai Generator
Generate AI Hentai for free.

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 this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Since its inception in 2009, Bitcoin has become a leader in the cryptocurrency world and its price has experienced huge fluctuations. To provide a comprehensive historical overview, this article compiles Bitcoin price data from 2009 to 2025, covering major market events, changes in market sentiment, and important factors influencing price movements.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
