php学习笔记

函数的应用

函数中的变量定义后也只能在它的作用范围内才可以使用


$var = 100; //此处$var是全局变量
function test($var){
   echo "在函数var的内部的值为:".$var; //在函数内部调用全局变量$var
}
test();
 ?>

//如何在函数内部使用函数外部的变量
1.在函数内部使用关键字global修饰变量


$var = 100; //此处$var是全局变量
function test($var){
	global $val; //在test()范围内,使用函数在外面的变量
   echo "在函数var的内部的值为:".$var; //在函数内部调用全局变量$var
}
test();
 ?>

2.//使用预定义变量 G L O B A L S 预 定 义 变 量 GLOBALS 预定义变量 GLOBALSGLOBALS用于引用全局作用域中可用的全部变量


$var = 100; //此处$var是全局变量
function test($var){

   echo "在函数var的内部的值为:".$GLOBALS['var'];
}
test();
 ?>

会话练习

登录设置

<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body>
<form action="login.php" method="post">
	<label>用户名:</label>
	<input type="text" name="username" />
	<label>密码:</label>
	<input type="text" name="password" />
	<input type="submit" name="submit" value="登录">
</form>
</body>
</html>


$arr = array("username"=>"xiangming","password"=>"123456");
if (isset($_POST['submit'])) {
	$username = $_POST['username'];
	$password = $_POST['password'];
	if ($username == $arr['username']) {
		if ($password == $arr['password']) {
			session_start();

			echo "";
			echo "";
			# code...
		}else {
			echo "";
			echo "";
		}
		
		# code...
	}
	else{
			echo "";
			echo "";
		}
	# code...
}

?>
 
   session_start();
if (!isset($_SESSION['username'])=='') {
	echo "";
	echo "";

	# code...
}
echo "登录成功后的页面";
?>

数组分页

 
$arr = array(
    array("001","aaa",1,"php"),
    array("002","aaa",1,"php"),
    array("003","aaa",1,"php"),
    array("004","aaa",1,"php"),
    array("005","aaa",1,"php"),
    array("006","aaa",1,"php"),
    array("007","aaa",1,"php"),
    array("008","aaa",1,"php"),
    array("009","aaa",1,"php"),
    array("010","aaa",1,"php"),
    array("011","aaa",1,"php"),
    array("012","aaa",1,"php"),
    array("013","aaa",1,"php"),
);
$page = $_GET['page'];
$perpage = 3;
$page_num = ceil(count($arr)/$perpage);
$page = max(1,$page);
$page = min($page_num,$page);
$start_index = ($page-1)*$perpage;
$end_index = $page*$perpage-1;
// echo $start_index;
// echo "
";
// echo $end_index; ?> <!DOCTYPE html> <html> <head> <title></title> </head> <body> <table border="1"> <tr> <th>学号</th> <th>姓名</th> <th>班级</th> <th>科目</th> </tr> for($i = $start_index; $i<= $end_index; $i++){ # code... ?> <tr> <td> echo $arr[$i][0];?></td> <td> echo $arr[$i][1];?></td> <td> echo $arr[$i][2];?></td> <td> echo $arr[$i][3];?></td> </tr> }?> </table> <a href="?page=1">【首页】</a> <a href="?page=$page-1);?>">【上一页】</a> <a href="?page=$page+1);?>">【下一页】</a> <a href="?page=$page_num;?>">【尾页】</a> </body> </html>

你可能感兴趣的:(php)