php基础知识(四)

流程控制语句 同js

<?php $t=date("H"); if ($t<"20") { echo "Have a good day!"; } $t=date("H"); if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } $t=date("H"); if ($t<"10") { echo "Have a good morning!"; } else if ($t<"20") { echo "Have a good day!"; } else { echo "Have a good night!"; } $favcolor="red"; switch ($favcolor) { case "red": echo "Your favorite color is red!"; break; case "blue": echo "Your favorite color is blue!"; break; case "green": echo "Your favorite color is green!"; break; default: echo "Your favorite color is neither red, blue, or green!"; } //循环 $i=1; while($i<=5) { echo "The number is " . $i . "
"; $i++; } $i=1; do { $i++; echo "The number is " . $i . "
"; } while ($i<=5); for ($i=1; $i<=5; $i++) { echo "The number is " . $i . "
"; } $x=array("one","two","three"); foreach ($x as $value) { echo $value . "
"; } ?>

数组

<?php $cars=array("Volvo","BMW","Toyota"); //数组定义 数值数组 echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . "."; //访问数组元素 //数组长度count() $cars=array("Volvo","BMW","Toyota"); echo count($cars); //count() 函数用于返回数组的长度 //遍历数值数组 $cars=array("Volvo","BMW","Toyota"); $arrlength=count($cars); for($x=0;$x<$arrlength;$x++) { echo $cars[$x]; echo "
"; } $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");//定义关联数组 echo "Peter is " . $age['Peter'] . " years old."; //遍历关联数组 $age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43"); foreach($age as $x=>$x_value) { echo "Key=" . $x . ", Value=" . $x_value; echo "
"; } ?>

数组排序
PHP - 数组排序函数
sort() - 对数组进行升序排列
rsort() - 对数组进行降序排列
asort() - 根据关联数组的值,对数组进行升序排列
ksort() - 根据关联数组的键,对数组进行升序排列
arsort() - 根据关联数组的值,对数组进行降序排列
krsort() - 根据关联数组的键,对数组进行降序排列
//sort()
c a r s = a r r a y ( &quot; V o l v o &quot; , &quot; B M W &quot; , &quot; T o y o t a &quot; ) ; s o r t ( cars=array(&quot;Volvo&quot;,&quot;BMW&quot;,&quot;Toyota&quot;); sort( cars=array("Volvo","BMW","Toyota");sort(cars);

c l e n g t h = c o u n t ( clength=count( clength=count(cars);
for( x = 0 ; x=0; x=0;x< c l e n g t h ; clength; clength;x++)
{
echo c a r s [ cars[ cars[x];
echo “
”;
}

//rsort()
c a r s = a r r a y ( &quot; V o l v o &quot; , &quot; B M W &quot; , &quot; T o y o t a &quot; ) ; r s o r t ( cars=array(&quot;Volvo&quot;,&quot;BMW&quot;,&quot;Toyota&quot;); rsort( cars=array("Volvo","BMW","Toyota");rsort(cars);

//asort() arsort() 关联数组排序 value
a g e = a r r a y ( &quot; P e t e r &quot; = &gt; &quot; 35 &quot; , &quot; B e n &quot; = &gt; &quot; 37 &quot; , &quot; J o e &quot; = &gt; &quot; 43 &quot; ) ; a s o r t ( age=array(&quot;Peter&quot;=&gt;&quot;35&quot;,&quot;Ben&quot;=&gt;&quot;37&quot;,&quot;Joe&quot;=&gt;&quot;43&quot;); asort( age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");asort(age);
arsort($age);

//ksort() krsort() 关联数组排序 key
a g e = a r r a y ( &quot; P e t e r &quot; = &gt; &quot; 35 &quot; , &quot; B e n &quot; = &gt; &quot; 37 &quot; , &quot; J o e &quot; = &gt; &quot; 43 &quot; ) ; k s o r t ( age=array(&quot;Peter&quot;=&gt;&quot;35&quot;,&quot;Ben&quot;=&gt;&quot;37&quot;,&quot;Joe&quot;=&gt;&quot;43&quot;); ksort( age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");ksort(age);
krsort($age);
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值