Home  >  Article  >  Backend Development  >  菜鸟求解答:php中怎么定义一个文件作用域的数组?

菜鸟求解答:php中怎么定义一个文件作用域的数组?

WBOY
WBOYOriginal
2016-06-23 13:47:501205browse

global $param_array = array('P', 'Q', 'CR', 'LT', 'TC,M1', 'TC,M2', 'RI,M1', 'RI,M2');

显示语法错误,那么怎么才能让这个数组就有文件内的作用域呢?
或者数组的文件作用域是不可能实现的?


回复讨论(解决方案)

文件作用域 是什么意思?

global 不能那么用的,可以去看一下手册。

文件作用域 是什么意思?

global 不能那么用的,可以去看一下手册。



不好意思,不怎么懂php
准确的说应该是脚本的作用域?
举个例子:
如果数组a定义在a.php中,那么它的作用域就是a.php,所有函数都可以访问该数组
但在b.php中就无法访问

不用进行特殊定义
一但定义一个数组
$a=array(1,2,3,4,5)

那么它的作用域就是在本文件当中,所有的函数都可以访问.

定义在a.php 中,那么这个数组默认就是全局变量,函数外可以直接使用,函数内需声明 global $a , 才能使用。

<?php$num = 123;function aa(){	global $num;	echo $num;}aa();?>

$param_array = array('P', 'Q', 'CR', 'LT', 'TC,M1', 'TC,M2', 'RI,M1', 'RI,M2');function local(){	global $param_array;	print_r($param_array);}class localclass{	function run(){		global $param_array;		print_r($param_array);	}}print_r($param_array);local();$obj = new localclass();$obj->run();

Statement:
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