Blogger Information
Blog 27
fans 0
comment 0
visits 17825
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
函数,数组与键值操作8-23
Yyk.的博客
Original
681 people have browsed it

 实例演示while(),do~while()

实例

<?php
header("Content-Type:text/html;charset=utf-8");
//输入x个数,打印出x个0
//while
$num = 0;
$x=8;
while($num<$x){
	echo 0 .'<br>';
	$num++;
	
}
echo '<hr>';
//do-while
$num = 0;
$x=8;
do{
	echo 0 .'<br>';
	$num++;
} while($num<$x);
?>

运行实例 »

函数的参数与作用域:

实例

<?php
header("Content-Type:text/html;charset=utf-8");
function hello($one,$two,$three){
	$sum = $one + $two + $three;
	return $sum;
}
echo  hello(1,2,3),'<hr>';
function hello1()
{
//    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 hello1(4,5,6),'<hr>';

$num = 1;//全局变量
function hello2(){
	global $num;//声明一下全局变量
	return $num;
}
echo hello2();

?>

运行实例 »

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

实例

<?php
$user = ['id'=>5, 'name'=>'Y', 'grade'=>85, 'class'=>'Math', 'age'=>19];
echo in_array(5,$user) ? 'Yse' : 'No';//查看是否存在某个值
echo '<hr>';
echo array_key_exists('namen',$user) ? 'Yse' : 'No';//查看是否存在某个键
echo '<hr>';
print_r(array_flip($user));//键值对调

//数组内部操作
echo count($user),'<br>';
//key()返回当前元素的键
echo key($user),'<br>';
//current()返回当前元素的值
echo current($user),'<hr>';
//next()指针下移
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),'<br>';
reset($user);
// each()返回当前元素的键值的索引与关联的描述,并自动下移
print_r(each($user));
//print_r(each($user));
//list() //将索引数组中的值,赋值给一组变量
list($key, $value) = each($user);
echo $key, '******', $value,'<hr>';
// while,list(),each() 遍历数组
?>

运行实例 »

数组模拟栈与队列操作

实例

<?php
$user = ['id'=>5, 'name'=>'Y', 'grade'=>85, 'class'=>'Math', 'age'=>19];
echo count($user),'<hr>';

echo array_push($user,'php'),'<hr>';//入栈,返回长度
print_r($user);
echo '<hr>';

echo array_pop($user);
echo array_pop($user);//出栈
print_r($user);
echo '<hr>';


$user1 = ['id'=>5, 'name'=>'Y', 'grade'=>85, 'class'=>'Math', 'age'=>19];
echo array_unshift($user1,'one');//头部入列
print_r($user1);


echo '<hr>';


echo array_shift($user1);//头部出列
print_r($user1);

echo '<hr>';


echo array_push($user1, 'two');//尾部入列
print_r($user1);

echo '<hr>';

echo array_pop($user1);//尾部出列
print_r($user1);
?>

运行实例 »

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



个人总结:

        初看很难,多操作几遍一切就不是问题了




Correction status:Uncorrected

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