Blogger Information
Blog 12
fans 0
comment 0
visits 7730
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
表格生成器案例,while(),do~while()案例,函数的参数与作用域,数组常用的键值操作与指针操作,数组模拟栈与队列操作2018-08-23
Jimi的博客
Original
605 people have browsed it

实例

<?php
/**
1. 编程: 实例演示while(),do~while()
 */

//while()
$i = 1;//初始化条件
while ($i<10){
    echo $i<9 ? $i.',': $i;
    $i++;//更新条件
}
echo '<hr>';

// do while()
$i=1;
do{
    echo $i<9 ? $i.',': $i;
    $i++;//更新条件
}
while($i<10);

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
2. 编程: 函数的参数与作用域
 */

//声明
function hello()
{
    return '潘多拉星欢迎您!';
}
//调用:按名称后跟上一对圆括号
echo hello(),'<hr>';

function hello1($siteName)
{
    return '欢迎来到'.$siteName.'玩耍';
}
echo hello1('潘多拉星'),'<hr>';

//当有可选参数的时候,必须把必选参数往前放
function hello2($lang,$siteName='潘多拉星')
{
    return '欢迎来到'.$siteName.'玩耍'.$lang;
}
echo hello2('php'),'<hr>';


//参数实际就是一个占位符,仅供参考,可以没有
//func_get_args() 获取函数参数列表的数组
//func_num_args(); //获取参数的数量
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中只有函数作用域,函数外部声明的变量在函数内部不能直接使用
function hello4()
{
    return $GLOBALS['siteName'].'<hr>';
}
echo hello4();

运行实例 »

点击 "运行实例" 按钮查看在线实例

实例

<?php
/**
3. 编程: 数组常用的键值操作与指针操作
 */
$user = ['id' => 5, 'name' => '均无敌', 'gender' => 'male','age' => 20];
echo  '<pre>',print_r($user,true);
//in_array()判断数组中是否存在某个值
echo in_array('均无敌',$user)?'存在<br>':'不存在<br>';
//array_key_exists();判断某个键名是否存在于数组中?
echo array_key_exists('salary',$user)?'存在<br>':'不存在<br>';
//array_values():以索引方式返回数组的值组成的数组
print_r(array_values($user));
//array_keys() 输出数组中的所有键名
print_r(array_keys($user));
//array_search():以字符串的方式返回指定值的键
echo $user[array_search('male',$user)];
//键值对调
//print_r(array_flip($user));

echo "<br>";
//数组的内部操作
echo count($user),'<br>';//返回数组中元素的个数
echo key($user),'<br>'; //key()返回当前元素的键
echo current($user),'<hr>'; //返回当前元素的值
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),'<hr>';
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
/**
4. 编程: 数组模拟栈与队列操作
 */
$user = ['id' => 5, 'name' => '均无敌', 'gender' => 'male','age' => 20];
echo '<pre>',print_r($user,true);
//echo '当前长度:',count($user),'<br>';
//入栈: array_push(),向数组尾部插入; 返回新数组的长度 = count()
//echo array_push($user,'PHP中文网');
//echo '<br>';
//echo '当前长度:',count($user),'<br>';
//print_r($user);
//
//array_pop() 函数删除数组中的最后一个元素。
//echo array_pop($user),'<br>';
//echo array_pop($user),'<br>';
//echo array_pop($user),'<br>';
//print_r($user);

//对:
//array_shift() 函数删除数组中第一个元素,并返回被删除元素的值。
//array_unshift() 在数组开头插入一个或多个单元
//echo array_unshift($user,'www.php.cn','qq无敌');
//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);

运行实例 »

点击 "运行实例" 按钮查看在线实例


Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!