批改状态:合格
老师批语:
1,while do while 使用
<?php
/**
* while 和do while使用
*/
$sum=0;
$i=0;
while ($i<=100)
{
$sum=$sum+$i;
$i++;
}
echo $sum;
echo '<br>';
$score=0;
$studentCount=0;
do{
$score=$score+$studentCount;
$studentCount++;
}while($studentCount<=100);
echo $score;点击 "运行实例" 按钮查看在线实例
2,函数的参数与作用域
<?php
/**
* 函数的参数与作用域
*/
//定义函数
function test()
{
return 'this is a php';
}
echo test();
echo '<hr>';
//带参函数
function show($score)
{
return '此次成绩是:'.$score;
}
echo show('50');
echo '<hr>';
//默认参数应该往后放
function show1($name,$study='学习php')
{
return $name.'正在'.$study;
}
echo show1('小米');
echo '<hr>';
//参数只是占位符,其可以不用必须写在函数中,但是必须要使用 函数
function show2()
{
return func_get_arg(0);
}
echo show2('小明','小红','小军');点击 "运行实例" 按钮查看在线实例
3,数组常用的键值操作与指针操作
<?php
/**
* 函数常用的键值操作与指针操作
*/
$user=['id'=>5,'name'=>'peter','gender'=>'male','age'=>'20'];
echo '<pre>',print_r($user,true);
//in_array()判断数组中是否存在某个值
echo in_array('peter',$user)? '存在<br>':'不存在<br>';
//array_key_exists():判断某个键名是否存在于数组中?
echo array_key_exists('age',$user) ? '存在<br>':'不存在<br>','<hr>';
//array_values();以索引方式返回数组的值组成的数组
print_r(array_values($user));
//array_keys(): 返回数组中部分的或所有的键名
print_r(array_keys($user));
// array_search():以字符串的方式返回指定值的键
echo $user[array_search('peter',$user)];
//键值对调
print_r(array_flip($user));
//数组的内部操作
echo count($user),'<br>';
//key()返回当前元素的键
echo key($user),'<br>';
//current()返回当前元素的值
echo current($user),'<hr>';
//next()指针下移
next($user);
echo key($user),'<br>';
echo current($user),'<hr>';
next($user);
echo key($user),'<br>';
echo current($user),'<hr>';
//复位
reset($user);
echo key($user),'<br>';
echo current($user),'<hr>';
//尾部
end($user);
echo key($user),'<br>';
echo current($user),'<br>';
reset($user);
// each()返回当前元素的键值的索引与关联的描述,并自动下移
print_r(each($user));
//print_r(each($user));
//list() //将索引数组中的值,赋值给一组变量
list($key, $value) = each($user);
echo $key, '******', $value,'<hr>';
// while,list(),each() 遍历数组
reset($user);
while (list($key, $value) = each($user)) {
echo $key , ' => ', $value, '<br>';
}点击 "运行实例" 按钮查看在线实例
4,数组模拟栈与队列操作
<?php /** * 使用数组来模拟堆栈(后入先出)和队列操作 */ $information=['name'=>'小王','age'=>'22','sex'=>'male']; //入栈;array_push();从末尾进 array_push($information,'55'); print_r($information); echo '<br>'; //出栈,从末尾出 print_r(array_pop($information)); echo '<br>'; //array_unshift() - 在数组开头插入一个或多个单元 array_unshift($information,'你好'); print_r($information); echo '<br>'; //array_shift 在数组开头单元移除 print_r(array_shift($information)); echo '<br>'; //队列模仿操作:增删只能在二端进行,不能一端进行 array_push($information,'哈哈'); //尾部入队 print_r($information); echo '<br>'; print_r(array_shift($information)); //头部出 echo '<br>'; //头部入队 array_unshift($information,'科技'); print_r($information); echo '<br>'; //尾部出 print_r(array_pop($information)); //array_push 尾部入队 array_shit()头部出 , array_unshit头部入队,array_pop 尾部出
点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号