博客列表 >08月23号 PHP while,to while,变量的声明,常用键值与数组内部指针,堆栈,队列的学习

08月23号 PHP while,to while,变量的声明,常用键值与数组内部指针,堆栈,队列的学习

18674060620的博客
18674060620的博客原创
2018年10月07日 11:54:12745浏览

最近上班太忙了,都没有来得及写作业,真的是有些对不起自己了,今天终于有空来看老师的视频和写作业了,

这节课学了很多东西,while,do while()的使用,函数的申明与调用,常见的键值in_array(), array_keys,array_key_exists,array_pop,array_push,array_shift,array_unshift,in_array

编程: 实例演示while(),do~while()

实例

<?php
/**
 * while():入口判断型
 */
echo 'for循环','<br/>';
for ($i=0;$i<10;$i++){
    echo $i<9 ? $i .',' :$i;
}
echo "<hr>";
//while()
echo "while",'<br>';
$i=0; //初始条件
while($i<10){
    echo $i<9 ? $i. ',' : $i;
    $i++;//更新条件
}
echo "<hr>";
//do while 与while的唯一区别就是当条件不满足的时候do while 至少要执行一次,while一次都不执行
$i=0;//初始条件
do{
    echo $i<9 ? $i .',' : $i;
    $i++;
}while($i<10);

运行实例 »

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

编程: 函数的参数与作用域

实例

<?php
//声明
function hello()
{
    return '欢迎111';
}
//调用:按名调用,名称后跟上一对圆括号
echo hello(),'<hr>';
function hello1($siteName)
{
    return '欢迎'.$siteName.'来学习';
}
echo hello1('张三'),'<hr>';

function hello2($siteName='李四')
{
    return '欢迎'.$siteName.'来学习';
}
echo hello2(),'<br>';


//php中只有函数作用域,函数外部声明的变量在函数内部不能直接使用(需要global或者$GLOBALS[])
$siteName = 'php中文网';
function hello3()
{
   // return $GLOBALS['siteName'];
    global $siteName;
    return $siteName;
}
echo hello3();

运行实例 »

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

编程: 数组常用的键值操作与指针操作

<?php
/**
* 1.常用的键值操作
* 2.数组内部指针操作
*/

$user = ['id'=>5,'name'=>'peter','gender'=>'male','age'=>20];
echo '<pre>',print_r($user,true);
//in_array()判断数组中是否存在某个值
echo in_array('m3le',$user) ? '存在' : '不存在<br>';
//array_key_exists()判断某个键名是否存在于数组中
echo array_key_exists('name',$user) ? '存在<br>' : '不存在<br>';
//array_values()以索引方式返回数组的值组成的数组
print_r(array_values($user));
//array_keys()
print_r(array_keys($user));
//array_search()以字符串的方式返回指定值的键
echo $user[array_search('peter',$user)];

//array_flip()键值对调
print_r(array_flip($user)) ;

//2.数组的内部操作
echo count($user),'<br>';
//key()返回当前元素的键
echo key($user),'<br>';
//current()返回当前元素的值
echo current($user),'<br>';
//next()指针下移
next($user);
echo key($user),'<br>';
echo current($user),'<br>';
next($user);
echo key($user),'<br>';
echo current($user),'<br>';
//reset 复位
reset($user);
echo key($user),'<br>';
echo current($user),'<br>';
//end()移到尾部
end($user);
echo key($user),'<br>';
echo current($user),'<br>';
reset($user);
//each()返回当前元素的键值的索引与关联的描述,并自动下移
print_r( each($user));
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'=>1,'name'=>'zhangsan','age'=>20];
echo '<pre>',print_r($user,true);
echo '当前长度:'.count($user),'<br>';
//入栈:array_push()返回新数组的长度 =count()
echo array_push($user,'php中文网'),'<br>';
print_r($user);
echo array_pop($user),'<br>';
echo array_pop($user),'<br>';
//echo array_pop($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);

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议