Blogger Information
Blog 11
fans 0
comment 0
visits 7574
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php的学习第三课(流程控制实战与常用数组函数一)-2018年8月26日13点10分
victor的博客
Original
761 people have browsed it

代码作业:

实例

<?php
/**
 *表单get请求,会将用户的数据直接添加到浏览器url上;不安全;数据量有限
 * post请求,安全
 * while循环:入口判断型
 * do...while:出口判断型
 *
 */

for($i=0; $i<10; $i++){
    echo $i<9 ? $i.',' : $i;
}
echo '<hr/>';

$j=0;
//while
while($j<10){

//    初始条件
    echo $j<9 ? $j.',' : $j;
    $j++;
    //更新条件
}

echo '<hr/>';

//do...while()
$k = 0;
do{
    echo $k<9 ? $k.',' : $k;
    $k++;
}while($k<10);

//当条件不满足的情况下,while可能出现一次都不执行的情况;do while会执行一次

运行实例 »

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

实例

<?php
/**函数
 * 1. 声明语法
 * 2. 参数设置
 * 3. 返回值
 * 4. 作用域
 *
 * 开发中循环用的基本都是while循环
 */

//声明
function hello()
{//每个花括号独占一行
    return '啦啦啦';
}
// 函数内部用return,外部用echo输出function名字
echo hello();

echo '<hr/>';

//带参数
function bonjour($num)
{
    return '啦'. $num .'啦啦';
}

echo  bonjour('123');

echo '<hr/>';

//参数设定默认值
function bonjour2($num = '123')
{
    return '啦'. $num .'啦啦';
}

echo  bonjour2('345');

echo '<hr/>';

//多个参数
function bonjour3($num = '123', $lang)
{
    return '啦'. $num .'啦啦' . $lang;
}

echo  bonjour3('345','aaaa');

echo '<hr/>';
//多个参数,传参顺序从前到后,必须先给第一个参数传参(即便第一个参数是函数默认参数),然后才能给与第二个参数
// 或者在函数圆括号内,把非默认参数放到默认参数之前
function bonjour4($lang, $num = '123')
{
    return '啦'. $num .'啦啦' . $lang;
}

echo  bonjour4('aaaa');

echo '<hr/>';


function hello2($a, $b, $c)
{
//    return print_r(func_get_args(), true);
//    return func_get_arg(0); //获取第一个参数值
//    return func_num_args();//获取参数数量
//    return ($a + $b + $c);
    return (func_get_arg(0) + func_get_arg(1) + func_get_arg(2));
}
echo hello2(4, 5, 6);

echo '<hr/>';

// 参数可以不下,用func_get_arg获取值
function hello3()
{
//    return print_r(func_get_args(), true);
//    return func_get_arg(0); //获取第一个参数值
//    return func_num_args();//获取参数数量
//    return ($a + $b + $c);
    return (func_get_arg(0) + func_get_arg(1) + func_get_arg(2));
}
echo hello3(4, 5, 6);

echo '<hr/>';

$siteNum = 'php.org';
function hello4()
{
//    global $siteNum;
    return $GLOBALS['siteNum'];
}
echo hello4();

运行实例 »

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

实例

<?php
/**
 * 数组函数
 * 1. 键值操作
 * 2. 数组内部指针操作(巡航)
 */

$user = ['id' => 5, 'name' => 'peter', 'gender' => 'male', 'education' => 'primary school'];
echo '<pre>' . print_r($user, true) .'</pre>';

echo '<hr/>';

// in_array() 判断数组中是否存在某个值
echo in_array('peter',$user) ? 'exist' : 'no exist';

echo '<hr/>';

// array_key_exists()   判断某个键名是否存在于数组中
echo array_key_exists('name', $user) ? 'exist' : 'no exist';

echo '<hr/>';

// array_values()   以索引方式返回数组的值 组成的数组
print_r(array_values($user));

echo '<hr/>';
// array_values()   以索引方式返回数组的键 组成的数组
print_r(array_keys($user));

echo '<hr/>';

// array_search()   以字符串的形式返回指定值的键
$key = array_search('peter',$user);
echo $key;
echo '<hr/>';
echo $user[$key];

echo '<hr/>';
//  键值对调 array_flip
print_r(array_flip($user));

echo '<hr/>';
//数组内部操作
echo count($user);

echo '<hr/>';
// key()    返回当前元素的键
echo key($user);

echo '<hr/>';
//current   返回当前元素的值
echo current($user);

echo '<hr/>';

//  next()  指针下移,指向下一个
next($user);
echo key($user);
echo '<hr/>';
echo current($user);

echo '<hr/>';

next($user);
echo key($user);
echo '<hr/>';
echo current($user);
echo '<hr/>';

//  重置,复位指针 reset
reset($user);
echo key($user);
echo '<hr/>';
echo current($user);

echo '<hr/>';

// 将指针移到尾部,最后一个
end($user);
echo key($user);
echo '<hr/>';
echo current($user);

echo '<hr/>';

reset($user);
//  each()  返回当前元素的键值索引与关联描述,并自动下移指针
print_r(each($user));   //id 5

echo '<hr/>';

//  list()    将索引数组中的值,赋值给变量
list($key, $value) = each($user);  //id 5
//$key = 'name';
//$value = 'peter';
echo $key . ' and ' . $value;

echo '<hr/>';

reset($user);
// while list each 遍历数组 each会遍历函数,然后自动下移指针,遍历完毕自动返回false
while(list($key, $value) = each($user)){
    echo $key . '=>' . $value . '<br/>';
};

运行实例 »

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

实例

<?php
/**
 * 使用数组模拟 堆栈,队列
 * 栈    后进先出    从羽毛球桶里面取羽毛球
 * 队列   先进先出    排队
 */

$user = ['id' => 5, 'name' => 'peter', 'gender' => 'male', 'education' => 'primary school'];
echo '<pre>' . print_r($user, true) .'</pre>';
echo '<hr/>';
echo '当前长度:' . count($user);
echo '<hr/>';

//入栈    array_push返回新数组的长度
array_push($user, 'php');
echo '当前长度:' . count($user);

echo '<hr/>';

echo '<pre>' . print_r($user, true) .'</pre>';

echo '<hr/>';

//出栈    顺序,从尾到头
echo array_pop($user);
echo '<hr/>';
echo array_pop($user);
echo '<hr/>';


echo '<pre>' . print_r($user, true) .'</pre>';
echo '<hr/>';

reset($user);

echo '<pre>' . print_r($user, true) .'</pre>';  //reset无效

echo '<hr/>';
//  队列操作    shift(),unshift()
echo array_unshift($user, 'php.net');
// 数据从头部进来
echo '<pre>' . print_r($user, true) .'</pre>';

echo array_shift($user);    //  直接返回值;数据从头部出去

echo '<pre>' . print_r($user, true) .'</pre>';

echo '<hr/>';

//  模拟队列:增删只能从两端进行,不能从同一端进行
array_push($user, 'css');   //入队,尾部入队
echo '<pre>' . print_r($user, true) .'</pre>';

array_shift($user); //头部删除

echo '<pre>' . print_r($user, true) .'</pre>';

echo '<hr/>';

array_unshift($user, 'html');   //头部入队

echo '<pre>' . print_r($user, true) .'</pre>';

array_pop($user);   //尾部出队

echo '<pre>' . print_r($user, true) .'</pre>';

运行实例 »

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

结果图:

1.png

2.png

3.png

4.png

总结:

首先写了表单提交get和post两种形式,get请求会将用户的数据直接显示在浏览器的url上,不安全,而且get请求的数据量很有限;post请求就不会有这些情况,因而很安全。

学习了两种循环,分别是while和do...while,while是入口判断型循环,在函数入口处进行判断,而do....while是出口判断型函数,在函数尾部进行判断。因而如果遇到不符合条件的判断,while不会执行,而do...while会进行一次执行。

函数学习,首先是函数的关键字function,之后声明这个函数,相当于函数名字,然后两个圆括号,其次花括号,花括号独占一行。函数内部用return,外部用echo外加函数名字进行输出。拥有多个参数的函数,传参顺序从前到后,必须先给第一个参数传参(即便第一个参数是函数默认参数),然后才能给与第二个参数;或者在函数圆括号内,把非默认参数放到默认参数之前。

最后学了数组,键值/指针/队列/堆栈的操作。

键值/指针主要就几个关键字的操作进行获取数组中的键或者值。而队列/堆栈就记住,增删只能从两端进行,不能从同一端进行,要么头部入列,尾部出列;要么尾部入队,头部删除;关键字主要也就四个,push/pop/shift/unshift

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!