<?php
//PHP 之自定义函数
/**
* 不带参数的函数
*/
function functionTest(){
echo "this is a function";
}
functionTest();
echo "<br>";
/**
* 带参数的函数
*/
function funcationArea($r){
echo "面积为:".$r*$r*pi();
}
funcationArea(10);
/**
* 带返回值得函数
*/
echo "<br>";
function funcationArea2($k){
$c=$k*$k*pi();
return $c;
}
$area=funcationArea2(50);
echo "带返回值得函数:".$area;
/**
* 返回多个值得函数,即返回数组
*/
echo "<br>";
function functionArray($name,$age){
$arr=array($name,$age);
return $arr;
}
$arr=functionArray("zuo","26");
echo "array:".$arr[0];
echo "<br>";
/**
* 带有默认值得函数
*/
function funcationArea3($r=100){
echo "默认面积为:".$r*$r*pi();
}
funcationArea3();
echo "<br>";
/**
* 函数中的引用传参
*/
$c=10;
function functionChang(&$c){
$c=$c*$c;
}
functionChang($c);
echo 'this is c: '.$c;
/**
* 全局变量的使用
*/
$x=12;
echo "<br>";
function changX(){
global $x;
$x=14;
}
changX();
echo 'this is x: '.$x;
/**
* 超级全局变量使用
*/
echo "<br>";
$GLOBALS['g']=7;
function superGlobal(){
$GLOBALS['g']=100;
}
superGlobal();
echo "this is super global: ".$GLOBALS['g'];
?>
//PHP 之自定义函数
/**
* 不带参数的函数
*/
function functionTest(){
echo "this is a function";
}
functionTest();
echo "<br>";
/**
* 带参数的函数
*/
function funcationArea($r){
echo "面积为:".$r*$r*pi();
}
funcationArea(10);
/**
* 带返回值得函数
*/
echo "<br>";
function funcationArea2($k){
$c=$k*$k*pi();
return $c;
}
$area=funcationArea2(50);
echo "带返回值得函数:".$area;
/**
* 返回多个值得函数,即返回数组
*/
echo "<br>";
function functionArray($name,$age){
$arr=array($name,$age);
return $arr;
}
$arr=functionArray("zuo","26");
echo "array:".$arr[0];
echo "<br>";
/**
* 带有默认值得函数
*/
function funcationArea3($r=100){
echo "默认面积为:".$r*$r*pi();
}
funcationArea3();
echo "<br>";
/**
* 函数中的引用传参
*/
$c=10;
function functionChang(&$c){
$c=$c*$c;
}
functionChang($c);
echo 'this is c: '.$c;
/**
* 全局变量的使用
*/
$x=12;
echo "<br>";
function changX(){
global $x;
$x=14;
}
changX();
echo 'this is x: '.$x;
/**
* 超级全局变量使用
*/
echo "<br>";
$GLOBALS['g']=7;
function superGlobal(){
$GLOBALS['g']=100;
}
superGlobal();
echo "this is super global: ".$GLOBALS['g'];
?>