批改状态:未批改
老师批语:
实例演示while(),do~while()
<?php
// while 循环 入口判断型 案例
//求1-100相加的值
$i = 1;
$sum = 0;
while($i<=100) {
$sum += $i;
$i++;
}
echo $sum.'<br>';
// do-while循环 出口判断性 案例
// 求1-100相加的值
$j = 1;
$sum2 = 0;
do {
$sum2 += $j;
$j++;
}while($j<=100);
echo $sum2;点击 "运行实例" 按钮查看在线实例
编程: 函数的参数与作用域
<?php
header('Content-type:text/html;charset=utf-8');
// 函数声明
function hello()
{
return 'Hello World!';
}
// 函数调用,按名调用
echo hello();
// 函数参数设置默认值,$siteName = 'php中文网' ,当没有参数时,$siteName默认值为php中文网,当有参数时,$siteName值为传的参数。
function hello2($siteName = 'php中文网')
{
return '欢迎来到'.$siteName;
}
echo hello2();
echo '<hr>';
// 当有可选参数时,必须把必选参数往前放,可选参数往后放。
function hello3($siteName,$user='')
{
return '欢迎'.$user.'到'.$siteName.'学习';
}
echo hello3('php中文网'),'</br>';
//匿名函数,获得参数的方法:fun_get_arg();
function hello4()
{
return (func_get_arg(0) + func_get_arg(1) + func_get_arg(2));
}
echo hello4(4,5,6),'<br>';
//函数作用域
//php中只有函数作用域,函数外部声明的变量,在函数内部不能直接使用。
//如果需要在函数内部使用,需要global 声明,或者$GLOBALS['siteName']
$siteName = 'php中文网';
function site()
{
// global $siteName;
return $GLOBALS['siteName'];
}
echo site();点击 "运行实例" 按钮查看在线实例
3. 编程: 数组常用的键值操作与指针操作
<?php
header('Content-type:text/html;charset=utf-8');
// 1、数组中常用的键值操作
$user = ['id'=>5,'name'=>'changhu','gender'=>26];
echo '<pre>'.(print_r($user,true));
// in_array() 判断数组中是否存在某个值。*精确匹配(区分大小写)
echo in_array('changhu',$user) ? '存在' : '不存在';
echo '<hr>';
// array_key_exists() 判断某个键是否存在数组中
echo array_key_exists('name',$user) ? '键存在' : '键不存在';
echo '<hr>';
//array_values 以索引方式返回数组的值组成的数组
print_r(array_values($user));
echo '<hr>';
//array_keys 以索引方式返回数组的键组成的数组
print_r(array_keys($user));
echo '<hr>';
//array_search 以字符串的方式返回指定值的键
echo array_search('26',$user);
echo '<hr>';
//array_flip 数组键值对调
print_r(array_flip($user));
/******************************************************/
// 2、数组的内部指针操作(巡航)
// count() 计算当前数组有多少个元素
echo count($user).'<br>';
// key() 返回数组当前元素的键
echo key($user),'<br>';
//current() 返回数组当前元素的值
echo current($user),'<br>';
//next() 指针后移
next($user);
echo current($user),'<br>';
//end() 指针移到结尾
end($user);
echo current($user),'<br>';
// 指针复位(回到第一位)
reset($user);
echo current($user).'<br>';
//each() 返回当前元素的键值的索引的关联描述
print_r (each($user));
reset($user);
//list() 将索引数组中的值,付给一组变量
next($user);
list($key,$value) = each($user);
echo $value,'<br>';
reset($user);
for($i=0;$i<count($user);$i++) {
list($key,$value) = each($user);
echo $key.' => '.$value.'<br>';
}点击 "运行实例" 按钮查看在线实例
4、数组模拟栈与队列操作
<?php
//3、数组模拟栈与队列操作
// 3.1 栈:典型的先进后出结构
//array_push($array,val) 入栈
$user = ['id'=>5,'name'=>'changhu','gender'=>26];
array_push($user,'push');
print_r ('<pre>'.$user);
//array_pop($array) 出栈
array_pop($user);
print_r ($user);
// 3.2 队列:先进先出结构
//array_unshift($array) 入队
array_unshift($user,'100');
print_r ($user);
// 出队 array_shift($array)
array_shift($user);
print_r($user);
// 模拟队列操作:增删只能在两端进行,不允许在同一端进行
// 尾部入队,头部删除
array_push($user,'php');
print_r($user);
array_shift($user);
print_r($user);
// 头部入队,尾部删除
array_unshift($user,'hello');
print_r($user);
array_pop($user);
print_r($user);点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号