作用域比较奇怪,一般的全局变量是不能在函数中使用的,不过代码块对作用域没有影响。 另外,参数定义时不能指定类型,除非它是个对象;返回值也不能指定(array似乎是可以的)。
P.S. 函数名称不区分大小写,而变量名称区分大小写。
head.inc(扩展名是任意的):
<?php
# function name does not case sensitive, but variable name does
// function plus(integer $a, integer $b) { <-- WRONG!!!
// here integer should be a class
function plus($a,$b) {
return $a+$b;
}
function mul($a, $b) {
return $a*$b;
}
?>
func.php:
#!/usr/bin/php
<?php
// if head.inc was not be found, require() will cause a fatal error, but include() will
// cause a warning
// use require_once/include_once to avoid from include a file more than one time
require_once('head.inc');
echo plus(3,4)." ";
echo mul(3,4)." ";
echo "------- ";

# scope
# P.S. code block {} does nothing about scope
$x=3;
function a() {
echo $x." "; // $x is a global variable,
// but it non-visible here
// except $x is a super global variable
}
a();
echo $x." ";

# make a variable to be global
function b() {
global $y; // export $y to be global
$y=4; // assigned after global declaration
echo "$y ";
}
b();
echo "$y ";

# default value
function c($z=5) {
echo "$z ";
}
c();
c(6);
?>
P.S. 函数名称不区分大小写,而变量名称区分大小写。
head.inc(扩展名是任意的):












func.php:





































