1. 程式人生 > >PHP函數初識(1)

PHP函數初識(1)

function private 區分大小寫 關鍵字 public

函數

php函數的定義方式:

修飾符 function 函數名(參數1,參數2...){

執行體.

}

修飾符:

public :公有的.(默認權限)

private : 私有的.

protected:受保護的. 
1:必須要使用關鍵字‘function‘ ;
2:函數名可以是字母或下劃線開頭.
3:在大括號中編寫函數體.
判斷函數是否存在:
    function_exists(‘函數名‘);
  註意點:函數調用不區分大小寫。
  例如:
  Name()=========》name();
  變量可以按引用傳遞,在參數前加&;
  $ceshi=10;
  function test4(&$a){
		$a=80;
}
	test4($ceshi);
	echo $ceshi;//值被改變
 <?php
	$ceshi = 10;
	//無參無返回值
	function test(){
		echo "Hello PHP";
	}
	test();
	echo "<br/>";
	echo "<br/>";
	//無參有返回值
	function test1(){
		$ceshi2 = 50;
		return $ceshi2; 
	}
	$c  =test1();
	echo $c ;
	echo "<br/>";
	echo "<br/>";
	//有參無返回值
	function test2($ceshi){
		$ceshi = 30;
	}
	test2($ceshi);
	echo $ceshi;//值不會被改變
	echo "<br/>";
	echo "<br/>";
	//有參有返回值
	function test3($ceshi){
		$num = $ceshi*15;
		return $num;
	}
	 $num = test3($ceshi);

	echo $num;
	//可變函數:
	//通過變量的值來自己調用函數.
	$func = ‘test‘;
	$func();
	$func = ‘test1‘;
	$func();
	$func = ‘test2‘;
	$func($ceshi);
	$func = ‘test3‘;
	$func($ceshi);


本文出自 “12897581” 博客,請務必保留此出處http://12907581.blog.51cto.com/12897581/1937375

PHP函數初識(1)