批改状态:未批改
老师批语:
实例演示while(),do~while()的基本用法
<?php
header("content-type:text/html;charset=utf-8");
// while(): 入口判断型
// do ~ while(): 出口判断型
echo '<h2>while()和do ~ while()循环的基本用法</h2>';
for ($i=0; $i<10; $i++){
echo $i<9 ? $i.',' : $i;
}
echo '<hr>';
//while ()
$a=0;//初始化条件
while ($a<10){
echo $a<9 ? $a.',' : $a;
$a++;// 更新循环的条件
}
echo '<hr>';
//do ~ while ()
$b=0; //初始化循环条件
do {
echo $b<9 ? $b.',' : $b;
$b++; //更新循环条件
}while($b<10);
//while ()和do ~ while ()的区别:
//当循环条件不满足的时候,while ()一次都不执行,do ~ while ()至少会执行一次点击 "运行实例" 按钮查看在线实例
函数的参数与作用域:
<?php
header("content-type:text/html;charset=utf-8");
/**
* 函数的基本知识
* 1、函数声明的语法
* 2、函数的参数设置
* 3、函数的返回值
* 4、函数的作用域
*/
//函数的声明:
function week()
{
return '今天是星期二';
}
echo week(),'<hr>'; //调用:按名字调用,名称后面跟一堆圆括号
//
function today($weather)
{
return '今天是星期二'.$weather;
}
echo today(',天气晴'),'<hr>';
//
function today2($weather='天气晴')
{
return '今天是星期二'.$weather;
}
echo today2(),'<hr>';
//两个参数
//当有可选的时候,必须要把必选参数放到前面
function today3($tomorrow,$weather='天气晴')
{
return '今天是星期二'.$weather.$tomorrow;
}
echo today3(',明天是暴雨'),'<hr>';
//参数实际上就是一个占位符,可以没有
//参数不一定要写在()里面
function today4($a, $b, $c)
{
return print_r(func_get_args(),true);
return func_get_arg(0);
return func_num_args();
return ($a+$b+$c);
}
echo today4(1,2,3),'<hr>';
//php中只有函数作用域,函数外部声明的变量,在函数内部不能直接使用
$siteName = '今天是个好天气';
function today5()
{
// global $siteName;
return $GLOBALS['siteName'];
}
echo today5();点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号