进一步练习 数组 函数 include 类 接口 表单 COOKIE 简单JS脚本等
<?php
session_start();
header("Content-type:text/html; charset=UTF-8;");
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
/*
//$_SESSION['username'] = $_GET['name'];
$_SESSION['username'] = $_REQUEST['name'];
$_SESSION['passwd'] = $_GET['passwd'];
$_SESSION['authuser'] = 0;
//Check name and passwd information
if (($_SESSION['username'] == 'luoz') and
($_SESSION['passwd'] == '123456')) {
$_SESSION['authuser'] = 1;
} else {
echo 'Sorry, but you don\'t have permission to view this page!';
exit();
}
*/
//本页面的访问时,服务器将COOKIE返回给浏览器,下一次访问时将附带上COOKIE,这样服务器侧就可以直接使用提交的COOKIE了
setcookie('username', "luo zhun", time() + 60);
$_SESSION['authuser'] = 1;
include "header.php";
echo $data;
?>
<script language="javascript">
function checkName()
{
var frm = document.forms['formTest'];
var name = frm.elements['username'].value;
if(name == "luozhun")
{
alert("OOOKKKK!");
return true;
}
else
{
alert("NH!");
return false;
}
}
</script>
<html>
<head>
<title>My First PHP Program</title>
</head>
<body>
<?php
//转义实现
/*
echo "<h1>I'm \"a\" lumberjack.</h1>";
echo "<h2>And I'm okay.</h2>";
echo '<h1>I\'m "a" lumberjack.</h1>';
*/
//heredoc 一定要注意<<<OOO后不能有任何其他字符,包括空格
$num = 1;
$str = <<<OOO
<h1>I'm "a" lumberjack.</h1>
<h3>And I'm okay.</h3> $num
<h1>I'm "a" lumberjack.</h1>
OOO;
echo $str;
//通过GET方式传送变量
//echo $_GET['name'];
echo "<br />*********************** <br /> ";
//常量
define(CONST_VALUE, "I am a const value!");
echo "CONST_VALUE";
echo "<br />*********************** <br /> ";
//变量
$value = 1.1211;
echo $value + 1;
echo "<br />*********************** <br /> ";
//运算符&&(and) ||(or) 与C类似
$value2 = "2";
echo $value2 + 1;
if(($value > 1) or ($value2 = 5))
{
echo "and == &&";
}
echo "<br />".$value2;
//数组
echo "<br />*********************** <br /> ";
$arr[] = "F";
$arr[] = "U";
$arr[] = "C";
$arr[] = "K";
print_r($arr);
echo "<br />*********************** <br /> ";
foreach($arr as $index => $val)
{
if($arr[$index] == $val)
{
echo "$arr[$index]"." ";
}
}
echo "<br />*********************** <br /> ";
foreach($arr as $val)
{
echo $val."<br />";
}
echo "<br />*******print********** <br /> ";
print($arr);
print("<br />".$arr[0]."<br />");
print_r($arr);
echo "<br />*********************** <br /> ";
echo max($arr);
echo "<br />*********************** <br /> ";
//页面间传递变量:GET方式
//echo "<a href='index.php?name=lz&id=2'>Click Me!</a>";
//特殊字符如空格等的处理
$strr = "hero of the war";//urlencode("luo zhun");
echo "<a href='moviesite.php?favmovie=$strr&id=2'>Click Me!</a>";
//查看PHP 日志,方法1:修改PHP.ini文件; 方法2:采用代码方式打开,如下
ini_set("display_errors", "On");
error_reporting(E_ALL | E_STRICT);
//echo $_GET["121"];
//页面间传递变量:SESSION方式见上面 ($_REQUEST 与 $_GET的区别)
//页面间传递变量:COOKIE
echo "<br />**********CLASS********* <br /> ";
//类 & 接口
//虚函数不要实现(PHP里都属于纯虚函数)
//内部存在虚函数的类必须为虚类
//子类若实现了构造函数,不会自动去调用父类的构造函数,这个与C++不同
//没有多重继承,使用类似JAVA的类和接口来实现多重继承效果
abstract class Dot
{
private $x = 2;
private $y = 4;
/*
public function __construct()
{
echo "Dot construct function called!";
}
*/
public function __construct($x, $y)
{
$this->x = $x;
$this->y = $y;
}
static public function Stat()
{
echo "static function called!";
}
public function area()
{
return $this->x * $this->y;
}
abstract function getName();
}
//接口不允许实现任何的方法,接口中所有的方法,都必须在子类中被覆写。
interface Inf
{
public function Inf1();
}
class DDot extends Dot implements Inf
{
public function __construct($x, $y)
{
parent::__construct($x, $y);
echo "DDot construct function called!";
}
public function getName()
{
return "DDOT!";
}
public function Inf1()
{
echo "<br /> Interface call <br />";
}
}
Dot::Stat();
$dot = new DDot(4,4);
echo $dot->area();
$dot->Inf1();
echo "<br />*********************** <br /> ";
?>
<!-- 页面间传递变量:表单 -->
<form method='post' action="moviesite.php" name="formTest">
<table cellpadding=2 cellspacing=2>
<tr>
<td >名称:</td>
<td ><input type="text" name="username" ></td>
</tr>
<tr>
<td >密码:</td>
<td ><input type="password" name="passwd" ></td>
</tr>
<tr>
<td >描述:</td>
<td><textarea name="content" rows="5" cols="25"></textarea></td>
</tr>
<tr>
<td></td>
<td>
<!-- <input type="submit" class="button" value="提 交"> -->
<input type="image" name="提 交" src="commentsBnt.gif" οnclick="return checkName();">
<input type="reset" class="button" value="清 空">
</td>
</tr>
</table>
</form>
</body>
</html>