当前位置:首页 » 编程语言 » php常量定义数组

php常量定义数组

发布时间: 2022-09-27 17:57:40

php如何定义一个常量数组

<?php
define('abc',array('a'=>'123','b'=>'456'));

echo'<pre>',var_mp(abc),'</pre>';
die();

最靠谱的做法是:
<?php
/*
全局配置
用法:
echo $_ENV['SYS_UP']['Size'];
echo $_ENV['Page'];
*/
$_ENV=array(
'now'=>time(), //当前时间
'JsVer'=>8, //Js的版本号
'Page'=>20, //每页显示的个数

'SYS_UP'=>array( //后台上传商品图
'Size'=>1000, //KB
'Path'=>'_proct/', //商品图档存储位置,必须/结尾
'AllowTyp'=>array( //允许上传的类型
'jpg','gif','png','bmp'
),
),
);

❷ php可以定义数组的常量吗

不可以。

常量只能包含标量数据(boolean,integer,float 和 string)。可以定义 resource 常量,但应尽量避免,因为会造成不可预料的结果。

更详细的说明,请参考php手册的常量部分内容网页链接

❸ php 如何做一个固定长度的数组

<?php
//php仿造java定长数组: int[] source = new int[10];
$source = new SplFixedArray(10);
//$source[10] = 1;这种会报错,因为超越了数组的边界,$source下标最大为9
//仿造java给数组元素默认赋值为0
for ($i=0; $i < sizeof($source); $i++) {
$source[$i] = 0;
}

//数组扩容为原来的1.5倍
$size = sizeof($source);
$destination = new SplFixedArray($size + ($size >> 1));

for ($i=0; $i < sizeof($source); $i++) {
$destination[$i] = $source[$i];
}

print_r($destination);

❹ php 怎么声明数组

一、什么是数组
数组就是一组数据的集合,把一系列数据组织起来,形成一个可操作的整体。数组的每个实体都包含两项:键和值。

二、声明数据
在PHP中声明数组的方式主要有两种:一是应用array()函数声明数组,一是直接为数组元素赋值。
<1>array()函数声明的数组的方式array([mixed...]) 参数mixed的语法为key=>value
如,<?php
$array=array("1"=>"编","2"=>"程","3"=>"词","4"=>"典");
print_r($array);
echo "<br>";
echo $array[1]; //注意:下标默认是从0开始
echo $array[2];
echo $array[3];
echo $array[4];
?>
<2>直接为数组元素赋值。如果在创建数组时不知所创建数组的大小,或在实际编写程序时数组的大小可能发生变化,采用这种数组创建的方法较好。
如,<?php
$array[1]="I";
$array[2]="love";
$array[3]="PHP";
print_r($array); //输出所创建数组的结构
?>

三、数组的类型
PHP支持两种数组:索引数组(indexed array)和联合数组(associative array),前者使用数字作为键,后者使用字符串作为键。

四、输出数组
在PHP中对数组元素输出,可以通过echo和print语句来实现,但这只能对数组中某一元素进行输出;要将数组结构输出则要使用print_r()函数,其语法:print_r(mixed expression_r),参数expression_r为普通的整形、字符型或实型变量则输出该变量本身,如果该参数为数组,则按一定键值和元素的顺序显示出该数组中的所有元素。

五、数组的构造
一维数组:
当一个数组的元素是变量时,则称其为一维数组。
声明一位数组: 类型说明符 数组名[常量表达式];
二维数组:
当一个数组的元素是一个一位数组时,则称之为二维数组。
如,<?php
$str = array (
"网络编程语言"=>array ("PHP","JSP","ASP"),
"体育项目"=>array ("m"=>"足球","n"=>"篮球"));
print_r ( $str) ;
?>

六、遍历数组
遍历数组中的所有元素是常用的一种操作,在遍历的过程中可以完成查询或其他功能。在PHP中遍历数组的方法有多种,下面介绍最常用的两种方法。
<1>使用foreach结构遍历数组;
<2>使用list()函数遍历数组,list()函数仅能用于数字索引的数组,且数字索引从0开始。
例:综合使用list()和each()认证用户登录:
<?php
//输出用户登录信息
while(list($name,$value)=each($_POST)){
if($name!="submit"){
echo "$name=$value<br>";
}
}
?>
七、统计数组元素个数
在PHP中,使用count()函数对数组中的元素个数进行统计,语法为:int coun(mixed array[,int mode]),其中参数array为必要参数,mode为可选参数,若选中COUNT——RECURSIVE(或1),本函数将递归地对数组数组。如,
<?php
$array = array("php" => array("PHP函数参考大全","PHP程序开发范例宝典","PHP数据库系统开发完全手册"),
"asp" => array("ASP经验技巧宝典")
); //声明一个二维数组
echo count($array,COUNT_RECURSIVE);//递归统计数组元素的个数,运行结果为6
?>

八、数组排序
<1>使用sort()和rsort()分别对数组进行升序和降序,如,
<?php
$array=array(5,26,37,18,9,42,88,66);
$array1=sort($array);
for($i=0;$i<count($array);$i++){
echo $array[$i]."";
}
echo "<br>";
$array1=rsort($array);
for($i=0;$i<count($array);$i++){
echo $array[$i]." ";
}
?>
运行结果:
5 9 18 26 37 42 66 88
88 66 42 37 26 18 9 5
<2>使用ksort()和asort()对关联数组排序
如果使用了相关数组,在排序后还要保持关键字和值的排序一致,这是就需要使用ksort()和asort()函

数,如,
<?php
$array=array('php'=>1,'jsp'=>2,'asp'=>3);
ksort($array);
print_r($array);
echo "<br>";
asort($array);
print_r($array);
?>
运行结果:
Array ( [asp] => 3 [jsp] => 2 [php] => 1 )
Array ( [php] => 1 [jsp] => 2 [asp] => 3 )

❺ php如何定义一个常量数组

<?php
constARR=['a','b'];
var_mp(ARR);

php版本要>=5.6才能使用,如果是5.6以下版本可以考虑把数组序列化后赋值给常量

❻ php的数组怎么定义长度

php如何获取一维数组的长度,使用php函数count(),或是sizeof();
示例如下:
$arr = Array('0','1','2','3','4');
echo count($arr);
// 输出 5
$arr = array('A','B','C');
echo sizeof($arr);
//输出3

多维数组:
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => '内容1'),
1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => '内容2')
);
echo '不统计多维数组:'.count($arr,0); echo '或用sizeof为'.sizeof($arr,0);
echo "<br/>";
echo '统计多维数组:'.count($arr,1);echo '或用sizeof为'.sizeof($arr,1);

❼ php中多维数组的问题

"Griffin"=>array()
表示索引"Griffin"是一个数组。=>可以简单理解为赋值。这是php里特有的一种写法。
数组分为2种,一种是自动索引数组。比如
$x=array ("Peter","Lois", "Megan");
那么$X[0]值为"peter",$X[1]为lois。
还有一种是自定义索引数组。
比如
$x=array ("father"=>"Peter","mother"=>"Lois","son"=> "Megan");
那么$x["father"] 就为"peter"
用引号围起来表示这是一个索引字符串值。通常情况下你直接[Griffin]也可以。
但是如果你在系统里有一个变量
$Griffin="son";
那么$families[Griffin]实际上会等于$families['son']。所以最好用引号围起来。
更多详细可以看php手册数组一章。

==========================================================
数组
PHP 中的 数组 实际上是一个有序映射。映射是一种把 values 关联到 keys 的类型。此类型在很多方面做了优化,因此可以把它当成真正的数组,或列表(向量),散列表(是映射的一种实现),字典,集合,栈,队列以及更多可能性。数组元素的值也可以是另一个数组。树形结构和多维数组也是允许的。

解释这些结构超出了本手册的范围,但对于每种结构至少会提供一个例子。要得到这些结构的更多信息,建议参考有关此广阔主题的其它着作。

语法
定义数组 array()
可以用 array() 语言结构来新建一个 array。它接受任意数量用逗号分隔的 键(key) => 值(value) 对。

array( key => value
, ...
)
// 键(key) 可是是一个 整数(integer) 或 字符串(string)
// 值(value) 可以是任意类型的值<?php
$arr = array("foo" => "bar", 12 => true);

echo $arr["foo"]; // bar
echo $arr[12]; // 1
?>
key 可以是 integer 或者 string。如果key是一个 integer 的标准表示,则被解释为整数(例如 "8" 将被解释为 8,而 "08" 将被解释为 "08")。key 中的浮点数被取整为 integer。在 PHP 中索引数组与关联 数组 是相同的,它们都可以同时包含 整型 和 字符串 的下标。

值可以是任意的 PHP 类型。

<?php
$arr = array("somearray" => array(6 => 5, 13 => 9, "a" => 42));

echo $arr["somearray"][6]; // 5
echo $arr["somearray"][13]; // 9
echo $arr["somearray"]["a"]; // 42
?>
如果对给出的值没有指定键名,则取当前最大的整数索引值,而新的键名将是该值加一。如果指定的键名已经有了值,则该值会被覆盖。

<?php
// 这个数组与下面的数组相同 ...
array(5 => 43, 32, 56, "b" => 12);

// ...
array(5 => 43, 6 => 32, 7 => 56, "b" => 12);
?>
Warning
自 PHP 4.3.0 起,上述的索引生成方法改变了。如今如果给一个当前最大键名是负值的数组添加一个新值,则新生成的的索引将为零(0)。以前新生成的索引为当前最大索引加一,和正值的索引相同。

使用 TRUE 作为键名将使 integer 1 成为键名。使用 FALSE 作为键名将使 integer 0 成为键名。使用 NULL 作为键名将等同于使用空字符串。使用空字符串作为键名将新建(或覆盖)一个用空字符串作为键名的值,这和用空的方括号不一样。

不能用数组和对象作为键(key)。这样做会导致一个警告:Illegal offset type。

用方括号的语法新建/修改
可以通过明示地设定值来改变一个现有的数组。

这是通过在方括号内指定键名来给数组赋值实现的。也可以省略键名,在这种情况下给变量名加上一对空的方括号(“[]”)。

$arr[key] = value;
$arr[] = value;
// key 可以是 integer 或 string
// value 可以是任意类型的值如果 $arr 还不存在,将会新建一个。这也是一种定义数组的替换方法。要改变一个值,只要给它赋一个新值。如果要删除一个键名/值对,要对它用 unset()。

<?php
$arr = array(5 => 1, 12 => 2);

$arr[] = 56; // This is the same as $arr[13] = 56;
// at this point of the script

$arr["x"] = 42; // This adds a new element to
// the array with key "x"

unset($arr[5]); // This removes the element from the array

unset($arr); // This deletes the whole array
?>
Note:

如上所述,如果给出方括号但没有指定键名,则取当前最大整数索引值,新的键名将是该值 + 1。如果当前还没有整数索引,则键名将为 0。如果指定的键名已经有值了,该值将被覆盖。

注意这里所使用的最大整数键名不一定当前就在数组中。它只要在上次数组重新生成索引后曾经存在过就行了。以下面的例子来说明:

<?php
// 创建一个简单的数组
$array = array(1, 2, 3, 4, 5);
print_r($array);

// 现在删除其中的所有元素,但保持数组本身不变:
foreach ($array as $i => $value) {
unset($array[$i]);
}
print_r($array);

// 添加一个单元(注意新的键名是 5,而不是你可能以为的 0)
$array[] = 6;
print_r($array);

// 重新索引:
$array = array_values($array);
$array[] = 7;
print_r($array);
?>
以上例程会输出:

Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
Array
(
)
Array
(
[5] => 6
)
Array
(
[0] => 6
[1] => 7
)

实用函数
有很多操作数组的函数,参见数组函数一节。

Note:

unset() 函数允许删除数组中的某个键。但要注意数组将不会重建索引。 If a true "remove and shift" behavior is desired, the array can be reindexed using the array_values() function.

<?php
$a = array(1 => 'one', 2 => 'two', 3 => 'three');
unset($a[2]);
/* will proce an array that would have been defined as
$a = array(1 => 'one', 3 => 'three');
and NOT
$a = array(1 => 'one', 2 =>'three');
*/

$b = array_values($a);
// Now $b is array(0 => 'one', 1 =>'three')
?>

foreach 控制结构是专门用于数组的。它提供了一个简单的方法来遍历数组。

数组做什么和不做什么
为什么 $foo[bar] 错了?
应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么 $foo[bar] 错了呢?可能在老的脚本中见过如下语法:

<?php
$foo[bar] = 'enemy';
echo $foo[bar];
// etc
?>
这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 PHP 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 PHP 自动将裸字符串(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,PHP 将把它替代为 'bar' 并使用之。

Note: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 PHP 不能解析它们。

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', false);
// Simple array:
$array = array(1, 2);
$count = count($array);
for ($i = 0; $i < $count; $i++) {
echo "\nChecking $i: \n";
echo "Bad: " . $array['$i'] . "\n";
echo "Good: " . $array[$i] . "\n";
echo "Bad: {$array['$i']}\n";
echo "Good: {$array[$i]}\n";
}
?>
以上例程会输出:

Checking 0:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 1
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 1

Checking 1:
Notice: Undefined index: $i in /path/to/script.html on line 9
Bad:
Good: 2
Notice: Undefined index: $i in /path/to/script.html on line 11
Bad:
Good: 2

演示此行为的更多例子:

<?php
// Show all errors
error_reporting(E_ALL);

$arr = array('fruit' => 'apple', 'veggie' => 'carrot');

// Correct
print $arr['fruit']; // apple
print $arr['veggie']; // carrot

// Incorrect. This works but also throws a PHP error of level E_NOTICE because
// of an undefined constant named fruit
//
// Notice: Use of undefined constant fruit - assumed 'fruit' in...
print $arr[fruit]; // apple

// This defines a constant to demonstrate what's going on. The value 'veggie'
// is assigned to a constant named fruit.
define('fruit', 'veggie');

// Notice the difference now
print $arr['fruit']; // apple
print $arr[fruit]; // carrot

// The following is okay, as it's inside a string. Constants are not looked for
// within strings, so no E_NOTICE occurs here
print "Hello $arr[fruit]"; // Hello apple

// With one exception: braces surrounding arrays within strings allows constants
// to be interpreted
print "Hello {$arr[fruit]}"; // Hello carrot
print "Hello {$arr['fruit']}"; // Hello apple

// This will not work, and will result in a parse error, such as:
// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'
// This of course applies to using superglobals in strings as well
print "Hello $arr['fruit']";
print "Hello $_GET['foo']";

// Concatenation is another option
print "Hello " . $arr['fruit']; // Hello apple
?>
当打开 error_reporting 来显示 E_NOTICE 级别的错误(例如将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。

和在语法一节中规定的一样,在方括号(“[”和“]”)之间必须有一个表达式。这意味着可以这样写:

<?php
echo $arr[somefunc($bar)];
?>
这是一个用函数返回值作为数组索引的例子。PHP 也可以用已知常量,可能之前已经见过

<?php
$error_descriptions[E_ERROR] = "A fatal error has occured";
$error_descriptions[E_WARNING] = "PHP issued a warning";
$error_descriptions[E_NOTICE] = "This is just an informal notice";
?>
注意 E_ERROR 也是个合法的标识符,就和第一个例子中的 bar 一样。但是上一个例子实际上和如下写法是一样的:

<?php
$error_descriptions[1] = "A fatal error has occured";
$error_descriptions[2] = "PHP issued a warning";
$error_descriptions[8] = "This is just an informal notice";
?>
因为 E_ERROR 等于 1, 等等.

那么为什么这样做不好?
也许有一天,PHP 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用 empty 和 default 这两个词了,因为他们是保留字。

Note: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 "$foo[bar]"是合法的(“合法”的原文为valid。在实际测试中,这么做确实可以访问数组的该元素,但是会报一个常量未定义的notice。无论如何,强烈建议不要使用$foo[bar]这样的写法,而要使用$foo['bar']来访问数组中元素。--haohappy注)。至于为什么参见以上的例子和字符串中的变量解析中的解释。

转换为数组
对于任意类型: integer, float, string, boolean and resource,如果将一个值转换为数组,将得到一个仅有一个元素的数组(其下标为 0),该元素即为此标量的值。换句话说, (array)$scalarValue 与 array($scalarValue) 完全一样。

If an object is converted to an array, the result is an array whose elements are the object's properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:

<?php

class A {
private $A; // This will become '\0A\0A'
}

class B extends A {
private $A; // This will become '\0B\0A'
public $AA; // This will become 'AA'
}

var_mp((array) new B());
?>
The above will appear to have two keys named 'AA', although one of them is actually named '\0A\0A'.

将 NULL 转换到 数组(array) 会得到一个空的数组。

比较
可能使用 array_diff() 和数组运算符来比较数组。

Examples
PHP 中的数组类型有非常多的用途,因此这里有一些例子展示数组的完整威力。

<?php
// This:
$a = array( 'color' => 'red',
'taste' => 'sweet',
'shape' => 'round',
'name' => 'apple',
4 // key will be 0
);

$b = array('a', 'b', 'c');

// . . .is completely equivalent with this:
$a = array();
$a['color'] = 'red';
$a['taste'] = 'sweet';
$a['shape'] = 'round';
$a['name'] = 'apple';
$a[] = 4; // key will be 0

$b = array();
$b[] = 'a';
$b[] = 'b';
$b[] = 'c';

// After the above code is executed, $a will be the array
// array('color' => 'red', 'taste' => 'sweet', 'shape' => 'round',
// 'name' => 'apple', 0 => 4), and $b will be the array
// array(0 => 'a', 1 => 'b', 2 => 'c'), or simply array('a', 'b', 'c').
?>
Example #1 Using array()

<?php
// Array as (property-)map
$map = array( 'version' => 4,
'OS' => 'Linux',
'lang' => 'english',
'short_tags' => true
);

// strictly numerical keys
$array = array( 7,
8,
0,
156,
-10
);
// this is the same as array(0 => 7, 1 => 8, ...)

$switching = array( 10, // key = 0
5 => 6,
3 => 7,
'a' => 4,
11, // key = 6 (maximum of integer-indices was 5)
'8' => 2, // key = 8 (integer!)
'02' => 77, // key = '02'
0 => 12 // the value 10 will be overwritten by 12
);

// empty array
$empty = array();
?>
Example #2 集合

<?php
$colors = array('red', 'blue', 'green', 'yellow');

foreach ($colors as $color) {
echo "Do you like $color?\n";
}

?>
以上例程会输出:

Do you like red?
Do you like blue?
Do you like green?
Do you like yellow?
直接改变数组的值在 PHP 5 中可以通过引用传递来做到。之前的版本需要需要采取变通的方法:

Example #3 集合

<?php
// PHP 5
foreach ($colors as &$color) {
$color = strtoupper($color);
}
unset($color); /* ensure that following writes to
$color will not modify the last array element */

// Workaround for older versions
foreach ($colors as $key => $color) {
$colors[$key] = strtoupper($color);
}

print_r($colors);
?>
以上例程会输出:

Array
(
[0] => RED
[1] => BLUE
[2] => GREEN
[3] => YELLOW
)
本例生成一个下标从1开始的数组。This example creates a one-based array.

Example #4 下标从1开始的数组

<?php
$firstquarter = array(1 => 'January', 'February', 'March');
print_r($firstquarter);
?>
以上例程会输出:

Array
(
[1] => 'January'
[2] => 'February'
[3] => 'March'
)
Example #5 填充数组

<?php
// fill an array with all items from a directory
$handle = opendir('.');
while (false !== ($file = readdir($handle))) {
$files[] = $file;
}
closedir($handle);
?>
数组是有序的。也可以使用不同的排序函数来改变顺序。更多信息参见数组函数。可以用 count() 函数来数出数组中元素的个数。

Example #6 数组排序

<?php
sort($files);
print_r($files);
?>
因为数组中的值可以为任意值,也可是另一个数组。这样可以产生递归或多维数组。

Example #7 递归和多维数组

<?php
$fruits = array ( "fruits" => array ( "a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array ( 1,
2,
3,
4,
5,
6
),
"holes" => array ( "first",
5 => "second",
"third"
)
);

// Some examples to address values in the array above
echo $fruits["holes"][5]; // prints "second"
echo $fruits["fruits"]["a"]; // prints "orange"
unset($fruits["holes"][0]); // remove "first"

// Create a new multi-dimensional array
$juices["apple"]["green"] = "good";
?>
数组(Array) 的赋值总是会涉及到值的拷贝。使用 引用操作符 通过引用来拷贝数组。

<?php
$arr1 = array(2, 3);
$arr2 = $arr1;
$arr2[] = 4; // $arr2 is changed,
// $arr1 is still array(2, 3)

$arr3 = &$arr1;
$arr3[] = 4; // now $arr1 and $arr3 are the same
?>

❽ php数组的数组做什么和不做什么

为什么 $foo[bar] 错了?
应该始终在用字符串表示的数组索引上加上引号。例如用 $foo['bar'] 而不是 $foo[bar]。但是为什么 $foo[bar] 错了呢?可能在老的脚本中见过如下语法:
<?php$foo[bar] = 'enemy';echo $foo[bar];// etc?>
这样是错的,但可以正常运行。那么为什么错了呢?原因是此代码中有一个未定义的常量(bar)而不是字符串('bar'-注意引号),而 php 可能会在以后定义此常量,不幸的是你的代码中有同样的名字。它能运行,是因为 php 自动将裸字符串(没有引号的字符串且不对应于任何已知符号)转换成一个其值为该裸字符串的正常字符串。例如,如果没有常量定义为 bar,php 将把它替代为 'bar' 并使用之。 注: 这并不意味着总是给键名加上引号。用不着给键名为常量或变量的加上引号,否则会使 php 不能解析它们。
<?phperror_reporting(E_ALL);ini_set('display_errors', true);ini_set('html_errors', false);// Simple array:$array = array(1, 2);$count = count($array);for ($i = 0; $i < $count; $i++) { echo /nChecking $i: /n; echo Bad: . $array['$i'] . /n; echo Good: . $array[$i] . /n; echo Bad: {$array['$i']}/n; echo Good: {$array[$i]}/n;}?>
演示此效应的更多例子:
<?php// 显示所有错误error_reporting(E_ALL);$arr = array('fruit' => 'apple', 'veggie' => 'carrot');// 正确print $arr['fruit']; // appleprint $arr['veggie']; // carrot// 不正确。This works but also throws a php error of// level E_NOTICE because of an undefined constant named fruit//// Notice: Use of undefined constant fruit - assumed 'fruit' in...print $arr[fruit]; // apple// Let's define a constant to demonstrate what's going on. We// will assign value 'veggie' to a constant named fruit.define('fruit','veggie');// Notice the difference nowprint $arr['fruit']; // appleprint $arr[fruit]; // carrot// The following is okay as it's inside a string. Constants are not// looked for within strings so no E_NOTICE error hereprint Hello $arr[fruit]; // Hello apple// With one exception, braces surrounding arrays within strings// allows constants to be looked forprint Hello {$arr[fruit]}; // Hello carrotprint Hello {$arr['fruit']}; // Hello apple// This will not work, results in a parse error such as:// Parse error: parse error, expecting T_STRING' or T_VARIABLE' or T_NUM_STRING'// This of course applies to using autoglobals in strings as wellprint Hello $arr['fruit'];print Hello $_GET['foo'];// Concatenation is another optionprint Hello . $arr['fruit']; // Hello apple?>
当打开 error_reporting() 来显示 E_NOTICE 级别的错误(例如将其设为 E_ALL)时将看到这些错误。默认情况下 error_reporting 被关闭不显示这些。
和在语法一节中规定的一样,在方括号(“[”和“]”)之间必须有一个表达式。这意味着可以这样写:
<?phpecho $arr[somefunc($bar)];?>
这是一个用函数返回值作为数组索引的例子。php 也可以用已知常量,可能之前已经见过 E_*。 <?php$error_descriptions[E_ERROR] = A fatal error has occured;$error_descriptions[E_WARNING] = php issued a warning;$error_descriptions[E_NOTICE] = This is just an informal notice;?>
注意 E_ERROR 也是个合法的标识符,就和第一个例子中的 bar 一样。但是上一个例子实际上和如下写法是一样的: <?php$error_descriptions[1] = A fatal error has occured;$error_descriptions[2] = php issued a warning;$error_descriptions[8] = This is just an informal notice;?>
因为 E_ERROR 等于 1,等等。 如同在以上例子中解释的那样,$foo[bar] 起作用但其实是错误的。它起作用是因为根据语法的预期,bar 被当成了一个常量表达式。然而,在这个例子中不存在名为 bar 的常量。php 就假定指的是字面上的 bar,也就是字符串 bar,但忘记加引号了。
那么为什么这样做不好?在未来的某一时刻,php 开发小组可能会想新增一个常量或者关键字,或者用户可能希望以后在自己的程序中引入新的常量,那就有麻烦了。例如已经不能这样用 empty 和 default 这两个词了,因为他们是保留字。
注: 重申一次,在双引号字符串中,不给索引加上引号是合法的因此 $foo[bar]是合法的。至于为什么参见以上的例子和字符串中的变量解析中的解释。

❾ php 如何定义空二维数组

//2维 [x][y]
$x=22;
$y=11;

$a=array_fill(0,$x,array_fill(0,$y,array()));
//$a 就是 [22][11]的2维数组

❿ php能定义数组为类的常量吗

可以。

在PHP中,我们不能用const直接定义数组常量,但是const可以定义字符串常量,结合eval()函数使字符串常量能执行。所以,我们可以用定义字符串常量的方式来返回一个数组常量。

<?php
class Test
{
const MY_ARR="return array(\"a\",\"b\",\"c\",\"d\");";
public function getConstArray()
{
return eval(Test::MY_ARR);//eval()函数把字符串作为PHP代码执行
}
}
$t=new Test();
print_r($t->getConstArray());
?>

热点内容
app什么情况下找不到服务器 发布:2025-05-12 15:46:25 浏览:714
php跳过if 发布:2025-05-12 15:34:29 浏览:467
不定时算法 发布:2025-05-12 15:30:16 浏览:131
c语言延时1ms程序 发布:2025-05-12 15:01:30 浏览:166
动物园灵长类动物配置什么植物 发布:2025-05-12 14:49:59 浏览:736
wifi密码设置什么好 发布:2025-05-12 14:49:17 浏览:148
三位数乘两位数速算法 发布:2025-05-12 13:05:48 浏览:398
暴风影音缓存在哪里 发布:2025-05-12 12:42:03 浏览:544
access数据库exe 发布:2025-05-12 12:39:04 浏览:632
五开的配置是什么 发布:2025-05-12 12:36:37 浏览:365