Home php教程 php手册 php中变量作用域和超级全局变量介绍

php中变量作用域和超级全局变量介绍

May 25, 2016 pm 04:46 PM
php effect overall situation variable

一、变量的赋值

变量声明后就可以给其赋值,有两种方式:值赋值和引用赋值。 

1、值赋值 

$color = "red" ; 

$sum = 12+"15" ; /* $sum = 27 */

2、若希望两个变量指向一个值的同一个副本,则需要通过引用赋值.

引用赋值 

所创建的变量与另一个变量引用的内容相同,如果多个变量引用了同一个内容,修改其中任意一个变量,在其余的变量上都将有所反映.

PHP实例代码如下:

<?php  
	$value1 = "hello" ;  
	$value2 = &value1 ; /*$value1 and $value2 both equal "hello" .*/  
	$value2 = "goodbye" ; /*$value1 and $value2 both equeal "goodbye". */  
	 
Copy after login

二、变量的作用域

php脚本的任何位置都可以声明变量,但声明变量的位置会大大影响访问变量的范围,这个可访问的范围称为作用域(scope).

php变量的4中作用域:

△ 局部变量

△ 函数参数

△ 全局变量

△ 静态变量

1、局部变量

在函数中声明的变量认为是局部变量,只能在函数中引用,退出声明变量的函数时,该变量及相应的值就会撤销,消除了导致全局访问的变量被有意或无意修改的可能性.

$x = 4 ;  
	function assignx ( ) {  
	$x = 0 ;  
	print "$x inside function is $x . <br>" ;  
	}  
	assignx ( ) ;  
	print "$x outside of function is $x . <br>" ; 
Copy after login

代码的执行结果为:

$x inside function is 0 .

$x outside function is 4 .

2、函数参数

php和其他编程语言一样,任何接受参数的函数都必须在函数首部中声明这些参数,虽然这些参数接受函数外部的值,但退出函数后就无法再访问这些参数.(按引用传递的参数除外)

例如:

function x10 ( $value ) {  
	$value = $value * 10 ;  
	return $value ;  
	} 
Copy after login

函数执行完毕,参数即将撤销.

3、全局变量

与局部变量相反,全局变量可以在程序的任何位置访问,当在函数中改变全局变量时,需要在该函数中将变量显示地生命为全局变量,只要在函数中的该变量前加上global即可.

例如:

$somevar = 15 ;  
	function addit ( ) {  
	global $somevar ;  
	$somevar ++ ;  
	print "somevar is $somevar" ;  
	}  
	addit ( ) ;  
Copy after login

$somevar 显示的值应为16,但是,若去掉 global $somevar ; 这行,变量$somevar 将被隐含的设置为0,再加1,最后显示的值就是1.

声明全局变量的另一方法是使用php的$global数组,如下:

$somevar = 15 ;  
	function addit ( ) {  
	$globals[ &#39;somevar&#39; ]++ ;  
	}  
	addit ( ) ;  
	print "somevar is ". $globals[ &#39;somevar&#39; ] ;  
	//返回值如下: somevar is 16 . 
Copy after login

4、静态变量

静态(static)作用域,普通变量的函数参数在函数结束时会撤销,但静态变量在函数退出时不会丢失值,并且再次调用此函数时还能保留这个值,在汴梁明前加上关键字 static 就可以声明一个静态变量.

static $somevar ;

考虑一个例子:

function keep_track ( ) {  
	static $count = 0 ;  
	$count ++ ;  
	print $count ;  
	print "<br>" ;  
	}  
	keep_track ( ) ;  
	keep_track ( ) ;  
	keep_track ( ) ;  
	keep_track ( ) ;  
Copy after login

若$count 没有被指明为静态(相应的,$count就是一个局部变量),输出将会是

1

1

1

1

因为$count是静态的,它会在每次执行函数时保留前面的值,输出如下:

1

2

3

4

静态作用域对于递归函数很有用,递归函数(recursive function)是一个功能强大的编程概念,它是一个可以重复调用自身的函数,直到满足某个条件为止.

5、 php的超级全局变量

php提供了很多有用的预定义变量,可以在执行脚本的人和位置访问,用于提供大量与环境有关的信息,可以通过这些变量获得关于当前用户会话、用户操作系统环境和本地操作环境等详细信息,php会创建部分变量,而其他许多变量的可用性和值则取决于操作系统和web服务.

输出所有预定义变量:

foreach ( $_server as $var => $value ) {  
	echo "$var => $value <br>" ;  
	}  
Copy after login

显示用户的ip地址:

print "hi!your ip address is ".$_server[ ' remote_addr' ] ;

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1668
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

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 and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

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 vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

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.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

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 and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

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 vs. Other Languages: A Comparison PHP vs. Other Languages: A Comparison Apr 13, 2025 am 12:19 AM

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.

PHP and Python: Code Examples and Comparison PHP and Python: Code Examples and Comparison Apr 15, 2025 am 12:07 AM

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.

See all articles