$fruit[] = 'banana';
$fruit[] = 'papaya';
你也可以用多维数组:
$people['David']['shirt'] = 'blue';
$people['David']['car'] = 'minivan';
$people['Adam']['shirt'] = 'white';
$people['Adam']['car'] = 'sedan';
一个创建数组的简便方法是
array()
函数为:
$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);
得到如下结果
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.
同样可写为
$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,甚至是 ?: 结构。