|
使用方括弧([ and ])设置数组索引(一般或关联性):
|
以下为引用的内容: $fruit[0] = 'banana'; $fruit[1] = 'papaya'; $favorites['animal'] = 'turtle'; $favorites['monster'] = 'cookie';
|
中国站长.站如果你对数组赋值,但索引是空白的,PHP则会把对象放于数组末尾。上面对变量$fruit的声明同下面代码的结果是一样的:
$fruit[] = 'banana';
$fruit[] = 'papaya';
你也可以用多维数组:
|
以下为引用的内容: $people['David']['shirt'] = 'blue'; $people['David']['car'] = 'minivan'; $people['Adam']['shirt'] = 'white'; $people['Adam']['car'] = 'sedan';
|
一个创建数组的简便方法是
array() php开源 开源
函数为:
|
以下为引用的内容: $fruit = array('banana','papaya'); $favorites = array('animal' => 'turtle', 'monster' => 'cookie);
|
或者 中国站长_站,为中文网站提供动力
|
以下为引用的内容: $people = array ('David' => array('shirt' => 'blue', 'car' => 'minivan'), 'Adam' => array('shirt' => 'white', 'car' => 'sedan'));
|
内建函数count()表明一个数组里有多少元素: 中国站.长站
|
以下为引用的内容: $fruit = array('banana','papaya'); print count($fruit);
|
得到如下结果 站.长php开源 开源站
2
控制结构
你可以利用循环结构例如for以及while:
for ($i = 4; $i < 8; $i++) {
print "I have eaten $i bagels today.\n"; }
结果
|
以下为引用的内容: I have eaten 4 bagels today. I have eaten 5 bagels today. I have eaten 6 bagels today. I have eaten 7 bagels today.
|
同样可写为 Chinaz^com
|
以下为引用的内容: $i = 4; while ($i < 8) { print "I have eaten $i bagels today.\n"; $i++; }
|
你可以使用控制结构if以及elseif:
|
以下为引用的内容: if ($user_count > 200) { print "The site is busy right now!"; } elseif ($user_count > 100) { print "The site is sort of active right now!"; else { print "The site is lonely - only $user_count user logged on."; }
|
使用运算符的经验法则同样也可以运用在控制结构上面。你还可以使用switch,do...while,甚至是 ?: 结构。 Chinaz
(阅读次数:)
共2页: 上一页 [1] 2 下一页
|