function test($param=null) {
if ($param===null)
.....
}
由於$ param在函數頭設置爲null,爲什麼即使打擾測試$ param === null?如果有一個情況$參數不會爲空?PHP函數的參數不清楚
function test($param=null) {
if ($param===null)
.....
}
由於$ param在函數頭設置爲null,爲什麼即使打擾測試$ param === null?如果有一個情況$參數不會爲空?PHP函數的參數不清楚
由於$ param在函數頭設置爲null,爲什麼即使打擾測試 if $ param === null?如果有一個情況下$參數不會爲空?
也就是說可選參數,因爲你定義的null
到它的默認值。
至於爲什麼要檢查它,你想確保一個參數的確是不是null
。
讓我們假設你想echo
它:
function test($param=null) {
echo $param;
}
當你調用函數,不會有事的,你不希望這樣做,對吧。出於這個原因,你想確保參數的值不爲null,以便你可以操縱它,但是你喜歡。
測試:
function test($param=null) {
echo $param;
}
test(); // no output
test('hello there'); // output: hello there
$param
如果沒有值傳遞給函數只會是空的。這是optional parameters的一個例子。
你可以通過傳遞一個值
test(10); //$param inside the method will be 10;
test(); //$param will be null
這是一個可選/ default argument調用該函數。
如果你調用該函數,那麼你可以把它的兩種方法之一:
test($value);
或
test();
在第一種情況下,$param
持有$value
值。在第二種情況下,$param
總是null
。
$ param = null只是一個默認變量,當用變量調用該函數時會被覆蓋。
如果你調用使用
$helloworld = test('notnull');
然後$ PARAM將在功能上等效 'NOTNULL' 的功能。
案例$ PARAM就不會是空:
test("ok");
在這種情況下,$ PARAM = 「OK」。
參數有時會通過函數調用傳遞。去搞清楚 – 2012-02-26 17:09:19