php3:跨平台的服务器端嵌入式脚本语言(3-4)
时间:2007-02-17 来源:PHP爱好者
标 题: php3:跨平台的服务器端嵌入式脚本语言(3)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
现在我们来看看第一个php3脚本,照例是Hello,world.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<HTML>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<HEAD>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<TITLE>First PHP3 script</TITLE>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</HEAD>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<BODY>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<CENTER>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<?
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "Hello, world!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
?>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</CENTER>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</BODY>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</HTML>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
把它放在任何一个能通过HTTP访问到的目录,取名hello.php3,再用浏览器连接
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
该文件,就可以看见其效果了(不用我写了吧.)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
如果把它改名成hello.phps,再用Netscape或Explorer连接,可以看见加了颜色
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
的源码,因为解释器在遇到.phps文件时会对它进行语法加亮的处理,在里面加上很多颜色
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
控制符再送出.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
并不需要手工指定解释器所在位置,因为如果在Apache服务器下,srm.conf里面可以
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
指定,在Microsoft IIS 或 PWS下,Registry里面会指定.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
从上面的例子应该很容易看出php3文件的写法.用<?和?>括起来的部分就是php3
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
程序段,解释器对其余部分不作处理,将这些程序段执行后以其输出代替(echo语句输出一
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
个无格式字符串,printf语句类似于C的同名函数.)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
同样可以看出,php3程序虽然是文本,但客户端只能看见解释后的结果(除非你提
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
供同名的phps文件),因此对客户端是保密的,这也是绝大部分服务器端脚本的特点.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
标 题: php3:跨平台的服务器端嵌入式脚本语言(4)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP和一般的解释性语言一样,不需要事先定义变量.如果你试着使用一个
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
没有赋过值的变量,那么返回的值是空字符串.对一个变量赋值则也就同时分配了
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
这个变量的内存.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP的变量名前面必须有一个$号,这也是很多脚本语言的惯例了.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a=1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$b=1.0;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$c="Hello!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
都是合法的PHP语句.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中的数组是这样定义的:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a[1]="a";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a[2]="b";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
它会自动扩展,不必预先设定上限.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
其实数组下标也可以是一个字符串,例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a["a"]=1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a["b"]="Hello!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
这时它实际上是一个哈希表,就象Tcl中的所谓数组一样.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中有一种类似于指针的用法,例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a = "b";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$$a = "c";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
则后一句产生了一个$b变量并给它赋值"c".
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP变量之间的运算符和C的类似,例如也有:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
+,-,*,/,%,^,&,|,&&,||,!,+=,-=,++,--等
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP3.0定义了一个字符串相加运算符".",和Perl的一样.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP的变量类型可以自动转换,如果需要强制转换,做法和C的一样.它有Integer,
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
Double,String三种简单类型.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
要遍历一个哈希表,可以这样:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$i["a"] = 0;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$i["c"] = 1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
reset($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
for ($j=1;$j<=count($i);$j++) {
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$k = key($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "$k, $i[$k]";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "<BR>";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
next($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
}
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中的控制流语句与C几乎完全一样,也有if...else,while,for,switch,break,
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
exit.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
现在我们来看看第一个php3脚本,照例是Hello,world.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<HTML>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<HEAD>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<TITLE>First PHP3 script</TITLE>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</HEAD>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<BODY>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<CENTER>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
<?
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "Hello, world!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
?>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</CENTER>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</BODY>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
</HTML>
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
把它放在任何一个能通过HTTP访问到的目录,取名hello.php3,再用浏览器连接
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
该文件,就可以看见其效果了(不用我写了吧.)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
如果把它改名成hello.phps,再用Netscape或Explorer连接,可以看见加了颜色
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
的源码,因为解释器在遇到.phps文件时会对它进行语法加亮的处理,在里面加上很多颜色
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
控制符再送出.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
并不需要手工指定解释器所在位置,因为如果在Apache服务器下,srm.conf里面可以
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
指定,在Microsoft IIS 或 PWS下,Registry里面会指定.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
从上面的例子应该很容易看出php3文件的写法.用<?和?>括起来的部分就是php3
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
程序段,解释器对其余部分不作处理,将这些程序段执行后以其输出代替(echo语句输出一
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
个无格式字符串,printf语句类似于C的同名函数.)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
同样可以看出,php3程序虽然是文本,但客户端只能看见解释后的结果(除非你提
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
供同名的phps文件),因此对客户端是保密的,这也是绝大部分服务器端脚本的特点.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
标 题: php3:跨平台的服务器端嵌入式脚本语言(4)
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP和一般的解释性语言一样,不需要事先定义变量.如果你试着使用一个
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
没有赋过值的变量,那么返回的值是空字符串.对一个变量赋值则也就同时分配了
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
这个变量的内存.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP的变量名前面必须有一个$号,这也是很多脚本语言的惯例了.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a=1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$b=1.0;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$c="Hello!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
都是合法的PHP语句.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中的数组是这样定义的:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a[1]="a";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a[2]="b";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
它会自动扩展,不必预先设定上限.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
其实数组下标也可以是一个字符串,例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a["a"]=1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a["b"]="Hello!";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
这时它实际上是一个哈希表,就象Tcl中的所谓数组一样.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中有一种类似于指针的用法,例如:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$a = "b";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$$a = "c";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
则后一句产生了一个$b变量并给它赋值"c".
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP变量之间的运算符和C的类似,例如也有:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
+,-,*,/,%,^,&,|,&&,||,!,+=,-=,++,--等
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP3.0定义了一个字符串相加运算符".",和Perl的一样.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP的变量类型可以自动转换,如果需要强制转换,做法和C的一样.它有Integer,
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
Double,String三种简单类型.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
要遍历一个哈希表,可以这样:
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$i["a"] = 0;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$i["c"] = 1;
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
reset($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
for ($j=1;$j<=count($i);$j++) {
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
$k = key($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "$k, $i[$k]";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
echo "<BR>";
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
next($i);
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
}
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
PHP中的控制流语句与C几乎完全一样,也有if...else,while,for,switch,break,
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
exit.
China It Power . Comhmh96T9RTy9BA9rXCCcplXtO8
php爱好者站 http://www.phpfans.net PHP|MySQL|javascript|ajax|html.
相关阅读 更多 +