正文
一、代码的重用-------include()和require()
include()和require()用法作用等完全相同,唯一的区别在于函数失败后,require()函数将 给出一个致命的错误。而include()只是给出一个警告。并且 注意 可以使用 任何扩展名 来命名包含文件,但要尽量遵循一个约 定,例如将扩展名命名为.inc或.php是一个很好的办法。
示例:
在reusable.php中:
在main.php中:
最后在main.php网页中显示的结果:
require()的具体实例
不使用require的方法 :在以下home.html中(举例,略看):
<html>
<head>
<title>TLA Consulting Pty Ltd</title>
<style type="text/css">
h1{color:white;font-size:24pt;text-align:center;
font-family:arial,sans-serif}
.menu{color:white;font-size:12pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
td{background:black}
p{color:black;font-size:12pt;text-align:justify;
font-family:arial,sans-serif}
p.foot{color:white;font-size:9pt;text-align:center;
font-family:arial,sans-serif;font-weight:bold}
a:link,a:visited,a:active{color:white}
</style>
</head>
<body>
<!--page header--> <!--显示了公司的名称和徽标-->
<table width="100%"cellpadding="12"cellspacing="0"border="0">
<tr bgcolor="black">
<td align="left">
<img src="logo.gif"alt="TLA logo"height="70" width="70">
</td>
<td>
<h1>TLA Consulting</h1>
</td>
<td align="right">
<img src="logo.gif"alt="TLA logo"height="70" width="70">
</td>
</tr>
</table>
<!--menu--> <!--创建了页面的导航条-->
<table width="100%"bgcolor="white"cellpadding="4"cellspacing="4">
<tr>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Home</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Contact</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Services</span></td>
<td width="25%">
<img src="s-logo.gif"alt=""height="20"width="20">
<span class="menu">Site Map</span></td>
</tr>
</table>
<!--page content--> <!--页面中的文本-->
<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs
and hope to hear from you soon.</p>
<!--page footer--> <!--包含了在每个页面底部脚注处显示的表格-->
<table width="100%"bgcolor="black"cellpadding="12"border="0">
<tr>
<td>
<p class="foot">&copy;TLA Consulting Pty Ltd.</p>
<p class="foot">Please see our
<a href="legal.php">legal information page</a></p>
</td>
</tr>
</table>
</body>
</html>
使用require的方法 可以将<!--page header--> <!--menu-->
表示的内容放入到一个文件header.php,<!--page footer-->
的内容放入到footer.php,而新建立一个hmoe.php内容为:
<?php
require('header.php');
?>
<!--page content-->
<p>Welcome to the home of TLA Consulting.
Please take some time to get to know us.</p>
<p>We specialize in serving your business needs
and hope to hear from you soon.</p>
<?php
require('footer.php');
?>
二者所展现的内容是相同的,即:
使用文件包含的好处
很容易使网站具有统一的风格。比如在一个网站拥有很多界面,但是其开头部分和页眉注脚等又相同,此时我便可以将这些放入一个文本中,然后不同的主界面部分自己手动修改,这样可以避免重复写很多相似的
代码。而若要对整体网站所有网页的界面风格进行修改(比如注脚页眉),则可值修改一个网页footer.php即可。
二、PHP中使用函数
函数使用示例
function_name();
function_name(7.993);
function_name(“hello,world!”);
function_name(
v
a
r
i
a
b
l
e
)
;
f
u
n
c
t
i
o
n
n
a
m
e
(
variable); function_name(
variable);functionname(variable,2342,“great”);
函数定义与C语言的区别
1、函数名称
注意 function_name()、Function_Name()或 FUNCTION_NAME(),指代的都是同一个函数
2、定义函数的语法:
function my_function(){内容}
;
3、函数定义的位置:
<?php
function my_function(){
//内容
}
?>
4、$name()
也可以被当作函数执行。
即$name
的值(假设$name
值为"myfunction"),然后php会取得name的值,然后去寻找名为myfunction的函数,也就是说$name()==myfunction()
;
5、函数中的可选参数的设置:
未设置可选参数:function create_table($data)
;
设置了可选参数:function create_table2($data,$border=1,$cellpadding=4,$cellspacing=4)
;//此时设置了第2~4位参数为默认值,用户可在之后选择传入或者不传染这几个值;
6、声明能够接收可变参数数量的函数-------func_num_args()、 func_get_arg()以及func_get_args()
作用是:可以确定已经传递了多少个参数以及这些参数的值。
例如:
function var_args(){
echo"Number of parameters:";
echo func_num_args();
echo"<br/>";
$args=func_get_args();
foreach($args as$arg){
echo$arg."<br/>";
}}
func_num_args()
函数将返回 传入的参数个数。而func_get_args()
函数将返回参数的数组。或者,可以使用 func_get_arg()
函数一次获得一个参数,该函数需要以希望访问的参数个数作为参数(参数 从0开始)。
7、函数作用域
局部变量与全局变量与C语言相同,而global声明的超级全局变量(global$var
)在函数内部和外部都是可见的。
函数unset($variable_name)可以手动删除变量。
三、 参数的引用传递和值传递-------即类似C语言指针
改变外部传入的值
未使用指针时,不改变实际的value值:
function increment($value,$amount=1){
$value=$value+$amount;
}
使用指针时,改变了从外部传入的指针值:
function increment(&$value,$amount=1){
$value=$value+$amount;
}
Return关键字
用法与C语言相同,可以通过此输出或者赋予一个值
递归
不使用函数的递归,颠倒一个字符串的内容:
function reverse_i($str){
for($i=1;$i<=strlen($str);$i++){
echo substr($str,-$i,1);
}
return;
}
使用函数的递归,颠倒一个字符串的内容:
function reverse_r($str){
if(strlen($str)>0){
reverse_r(substr($str,1));
}echo substr($str,0,1);
return;
}