PHP3:if、else、switch语句、数组、数组排序(sort、rsort、asort、arsort、ksort、krsort)

PHP3:if、else、switch语句、数组、数组排序(sort、rsort、asort、arsort、ksort、krsort)

一、条件语句:

1、if语句:

<?php
$x=3;
$y=4;
$z=$x*$y;
if ($z<"13")  //如果z小于13则执行if语句
{
    echo "Hello world!";
}
//结果显示:Hello world!
?>

2、if…else语句:

<?php
$x=3;
$y=5;
$z=$x*$y;

if ($z<"13")
{
    echo "Hello world!";
}
else //如果不满足,则输出如下语句
{
	echo "good bye!!!";
}

//结果显示:good bye!!!
?>

3、if…elseif(elseif…)…else:

在多个条件中,直到一个条件成立时执行相应的代码块。需要知道的是elseif 可以多次嵌套进去,并不是只适用于3个条件。

<?php
$x=11;
if ($x<"10")
{
    echo "HELLO";
}
elseif ($x<"12")
{
    echo "hello world";
}
else
{
    echo "hello world !!!!!";
}
//结果显示:hello world
?>

4、switch语句:

switch语句最大的特点是它可以有选择性的执行若干代码块中的一个。

<?php
$lesson="Chinese";
switch ($lesson)
{
case "Chinese":
    echo "语文课";
    break;
case "math":
    echo "数学课";
    break;
case "English":
    echo "英语课";
    break;
default:
    echo "其它课程";
}
//结果显示:语文课
?>

二、数组:

PHP中使用array()来创建数组,它能在单个变量中存储多个值的特殊变量。在PHP中分别有以下三种类型的数组:数值数组、关联数组、多维数组。

1、数值数组:

(1)自动分配ID键(ID键总是从0开始)
$Lesson=array(“Chinese”,“Math”,“English”);
手动分配ID键
$Lesson[0]=“Chinese”;
$Lesson[1]=“Math”;
$Lesson[2]=“English”;

<?php
$Lesson=array("Chinese","Math","English");
echo "I like $Lesson[0]";
echo "<br>";
echo "I like $Lesson[0] $Lesson[1]  and $Lesson[2]";
/*结果显示:
I like Chinese
I like Chinese Math  and English
*/
?>

(2)获取数组的长度 - count()函数

echo count($Lesson);  //显示3

(3)遍历数值数组:
使用for循环来遍历并打印数值数组中的所有值,思路如下:
首先统计字符串的长度来确定循环控制次数,然后使用for循环设置自加变量x并且从0开始,最后写入输出语句,完成。

<?php
$Lesson=array("Chinese","Math","English");
$arrlength=count($Lesson);
 
for($x=0;$x<$arrlength;$x++)
{
    echo $Lesson[$x];
    echo "<br>";
}
/*结果显示:
Chinese
Math
English
*/
?>

2、关联数组:

(1)关联数组:
关联数组是使用分配给数组的指定的键的数组。
这里有两种创建关联数组的方法:

<?php
$Lesson=array("a"=>"Chinese","b"=>"Math","c"=>"English");
echo $Lesson['a'];
//结果显示:Chinese
?>

(2)遍历关联数组:
思路:首先使用foreach()将 变量 as为一 一对应,之后在foreach()循环语句中输出,达到遍历。

<?php
$Lesson=array("a"=>"Chinese","b"=>"Math","c"=>"English");
foreach($Lesson as $x=>$y)
{
	echo "$x,$y";
	echo "<br>";
}
/*显示结果为:
a,Chinese
b,Math
c,English
*/
?>

3、多维数组:

一个数组中包含数组。

<?php 
$x = array 
( 
    "a"=>array 
    ( 
        "hello wold", 
        "thit is beautiful wold" 
    ), 
    "b"=>array 
    ( 
        "hello China", 
        "China is very beautiful!" 
    ), 
    "c"=>array 
    ( 
        "hello xi'an", 
        "Xi'an has a long history!" 
    ) 
); 
print_r($x); 

/*结果显示:
Array
(
    [a] => Array
        (
            [0] => hello wold
            [1] => thit is beautiful wold
        )

    [b] => Array
        (
            [0] => hello China
            [1] => China is very beautiful!
        )

    [c] => Array
        (
            [0] => hello xi'an
            [1] => Xi'an has a long history!
        )

)
*/
?>

有选择性的输出上述数组中的几个值:

echo $x['b'][1].' and '.$x['c'][1];
/*运行结果如下:
China is very beautiful! and Xi'an has a long history!
*/

三、数组排序:

函数注解
sort()对数组进行升序排列
rsort()对数组进行降序排列
asort()根据关联数组的值,对数组进行升序排列
ksort()根据关联数组的键,对数组进行升序排列
arsort()根据关联数组的值,对数组进行降序排列
krsort()根据关联数组的键,对数组进行降序排列

1、sort()数组升序、rsort()数组降序:

这里用一个例子来演示吧:

<?php
$x=array(12,-23,7,0,-9);
$arrlength=count($x);
sort($x);
//排序好之后进行遍历
for($y=0;$y<$arrlength;$y++)
{
    echo $x[$y];
    echo "<br>";
}
/*显示结果为:
-23
-9
0
7
12
*/
echo "<br>";
rsort($x);
for($y=0;$y<$arrlength;$y++)
{
    echo $x[$y];
    echo "<br>";
}
/*显示结果为:
12
7
0
-9
-23
*/
?>

2、asort()、arsort()关联数组的值升、降序:

<?php
$Lesson=array("Chinese"=>"40","Math"=>"42","English"=>"37");
asort($Lesson);
foreach($Lesson as $x=>$y)
{
	echo "$x $y";
	echo "<br>";
}
echo "<br>";
arsort($Lesson);
foreach($Lesson as $x=>$y)
{
	echo "$x $y";
	echo "<br>";
}
/*显示结果为:
English 37
Chinese 40
Math 42

Math 42
Chinese 40
English 37
*/
?>

3、ksort()、krsort()关联数组的键升、降序:

<?php
$Lesson=array("Chinese"=>"40","Math"=>"42","English"=>"37");
ksort($Lesson);
foreach($Lesson as $x=>$y)
{
	echo "$x $y";
	echo "<br>";
}
echo "<br>";
krsort($Lesson);
foreach($Lesson as $x=>$y)
{
	echo "$x $y";
	echo "<br>";
}
/*显示结果为:
Chinese 40
English 37
Math 42

Math 42
English 37
Chinese 40

*/
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值