批改状态:未批改
老师批语:
代码实例如下:
<?php
/**
* while(): 入口判断型
* do ~ while(): 出口判断型
*/
for ($i=0; $i<18; $i++) {
echo $i<18 ? $i.',' : $i;
}
echo '<hr>';
// while()
$i=18; //初始化条件
while($i<18) {
echo $i<9 ? $i.',' : $i;
$i++; //更新条件
}
echo '<hr>';
//do_while()出口判断型
$i=18; //初始化条件
do {
echo $i<18 ? $i.',' : $i;
$i++; //更新条件
} while($i<19);
//
//综上所述,while()循环和do_while()循环的最大区别就是前者可以一次都不执行而后者最少要执行一次。点击 "运行实例" 按钮查看在线实例
<?php
/**
* 函数的基本知识
* 1.声明的语法
* 2.参数设置
* 3.返回值
* 4.作用域
*/
//声明
function hello()
{
return '欢迎来到php中文网学习';
}
//调用: 按名调用,名称后跟上一对圆括号
echo hello(),'<hr>';
function hello1($siteName)
{
return '欢迎来到'.$siteName.'学习';
}
echo hello1('php中文网'),'<hr>';
//当有可选参数的时候,必须把必选参数往前放
function hello2($lang,$siteName='php中文网')
{
return '欢迎来到'.$siteName.'学习'.$lang;
}
echo hello2('php'),'<hr>';
//参数实际就是一个占位符,仅供参考,可以没有
function hello3()
{
// return print_r(func_get_args(),true);
// return func_get_arg(2);
// return func_num_args(); //获取参数的数量
return (func_get_arg(0) + func_get_arg(1) + func_get_arg(2));
}
echo hello3(4,5,6),'<hr>';
$siteName = 'php中文网';
// php中只有函数作用域,一般来说函数外部声明的变量的作用域是有范围限制的,在函数内部不能直接使用;
//如果一定要在内部使用就必须使用$GLOBALS进行声明,如下所示:
function hello4 ()
{
// global $siteName;
return $GLOBALS['siteName'];
}
echo hello4();点击 "运行实例" 按钮查看在线实例
<?php
/**
* 1.常用的键值操作
* 2.数组内部指针操作(巡航)
*/
$user = ['id'=>8, 'name'=>'bill','gender'=>'male','age'=>18];
echo '<pre>',print_r($user,true);
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>';
}点击 "运行实例" 按钮查看在线实例
<?php /** * 使用数组来模拟堆栈和队列操作 */ $user = ['id'=>8, 'name'=>'bill','gender'=>'male','age'=>'18']; echo '<pre>',print_r($user,true); //echo '当前长度: '. count($user), '<br>'; // 入栈:array_push()返回新数组的长度= count() //echo array_push($user, 'php中文网'); //echo '当前长度: '. count($user), '<br>'; //print_r($user); //echo array_pop($user),'<br>'; //echo array_pop($user),'<br>'; //echo array_pop($user),'<br>'; //print_r($user); //队: shift(),unshift() //echo array_unshift($user, 'www.php.cn','peterzhu'); //print_r($user); //echo array_shift($user),'<br>'; //print_r($user); //模拟队列操作: 增删只能在二端进行,不允许同一端进行 array_push($user, 'php'); //尾部进队 print_r($user); array_shift($user); // 头部出队 print_r($user); array_unshift($user, 'html'); // 头部进队 print_r($user); array_pop($user); // 尾部出队 print_r($user);
点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号