日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > php >内容正文

php

php函数(检查变量、函数、键是否存在)

發(fā)布時間:2025/3/15 php 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php函数(检查变量、函数、键是否存在) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

empty

(PHP 4, PHP 5)

empty — 檢查一個變量是否為空

描述

bool?empty?(?mixed?$var?)

如果?var?是非空或非零的值,則?empty()?返回?FALSE。換句話說,""、0、"0"、NULL、FALSE、array()、var $var;?以及沒有任何屬性的對象都將被認為是空的,如果?var?為空,則返回?TRUE。

除了當(dāng)變量沒有置值時不產(chǎn)生警告之外,empty()?是?(boolean) var?的反義詞。參見轉(zhuǎn)換為布爾值獲取更多信息。

?

Example#1?empty()?與?isset()?的一個簡單比較。

<?php
$var?=?0
;

// 結(jié)果為 true,因為 $var 為空
if (empty($var
)) {??
???? echo?'$var is either 0 or not set at all'
;
}

// 結(jié)果為 false,因為 $var 已設(shè)置
if (!isset($var
)) {?
???? echo?'$var is not set at all'
;
}
?>

?

Note:?由于這是一個語言結(jié)構(gòu)而非函數(shù),因此它無法被變量函數(shù)調(diào)用。

Note:?empty()?只檢測變量,檢測任何非變量的東西都將導(dǎo)致解析錯誤。換句話說,后邊的語句將不會起作用:?empty(addslashes($name))。

參見?isset()、unset()、array_key_exists()、count()?和?strlen()。

isset

(PHP 4, PHP 5)

isset — 檢測變量是否設(shè)置

描述

bool?isset?(?mixed?$var?[,?mixed?$var?[,?$...?]] )

如果?var?存在則返回?TRUE,否則返回?FALSE。

如果已經(jīng)使用?unset()?釋放了一個變量之后,它將不再是?isset()。若使用?isset()?測試一個被設(shè)置成?NULL?的變量,將返回?FALSE。同時要注意的是一個?NULL?字節(jié)("\0")并不等同于 PHP 的?NULL?常數(shù)。

Note:?警告?isset()?只能用于變量,因為傳遞任何其它參數(shù)都將造成解析錯誤。若想檢測常量是否已設(shè)置,可使用?defined()?函數(shù)。

?

<?php

$var?=?''
;

// 結(jié)果為 TRUE,所以后邊的文本將被打印出來。
if (isset($var
)) {
???? print?"This var is set set so I will print."
;
}

// 在后邊的例子中,我們將使用 var_dump 輸出 isset() 的返回值。

$a?=?"test"
;
$b?=?"anothertest"
;

var_dump( isset($a) );??????
// TRUE
var_dump( isset ($a,?$b) );?
// TRUE

unset ($a
);

var_dump( isset ($a) );?????
// FALSE
var_dump( isset ($a,?$b) );?
// FALSE

$foo?=?NULL
;
var_dump( isset ($foo) );???
// FALSE

?>

?

這對于數(shù)組中的元素也同樣有效:

<?php

$a?= array ('test'?=>?1,?'hello'?=>?NULL
);

var_dump( isset ($a['test']) );????????????
// TRUE
var_dump( isset ($a['foo']) );?????????????
// FALSE
var_dump( isset ($a['hello']) );???????????
// FALSE

// 鍵 'hello' 的值等于 NULL,所以被認為是未置值的。
// 如果想檢測 NULL 鍵值,可以試試下邊的方法。?
var_dump(?array_key_exists('hello',?$a) );?
// TRUE

?>

?

Note:?由于這是一個語言結(jié)構(gòu)而非函數(shù),因此它無法被變量函數(shù)調(diào)用。

參見?empty()、unset()、defined()、array_key_exists()?和錯誤控制?@?運算符。

unset

(PHP 4, PHP 5)

unset — 釋放給定的變量

描述

void?unset?(?mixed?$var?[,?mixed?$var?[,?$...?]] )

unset()?銷毀指定的變量。注意在 PHP 3 中,unset()?將返回?TRUE(實際上是整型值 1),而在 PHP 4 中,unset()?不再是一個真正的函數(shù):它現(xiàn)在是一個語句。這樣就沒有了返回值,試圖獲取?unset()?的返回值將導(dǎo)致解析錯誤。

?

Example#1?unset()?示例

<?php
// 銷毀單個變量
unset ($foo
);

// 銷毀單個數(shù)組元素
unset ($bar['quux'
]);

// 銷毀一個以上的變量
unset ($foo1,?$foo2,?$foo3
);
?>

?

unset()?在函數(shù)中的行為會依賴于想要銷毀的變量的類型而有所不同。

如果在函數(shù)中?unset()?一個全局變量,則只是局部變量被銷毀,而在調(diào)用環(huán)境中的變量將保持調(diào)用?unset()?之前一樣的值。

<?php
function?destroy_foo
() {
???? global?$foo
;
???? unset($foo
);
}

$foo?=?'bar'
;
destroy_foo
();
echo?$foo
;
?>
上邊的例子將輸出: bar

?

如果在函數(shù)中?unset()?一個通過引用傳遞的變量,則只是局部變量被銷毀,而在調(diào)用環(huán)境中的變量將保持調(diào)用?unset()?之前一樣的值。

<?php
function?foo(&$bar
) {
???? unset($bar
);
????$bar?=?"blah"
;
}

$bar?=?'something'
;
echo?"$bar\n"
;

foo($bar
);
echo?"$bar\n"
;
?>
上邊的例子將輸出: something something

?

如果在函數(shù)中?unset()?一個靜態(tài)變量,則?unset()?將銷毀此變量及其所有的引用。

<?php
function?foo
() {
???? static?$a
;
????$a
++;
???? echo?"$a\n"
;
???? unset($a
);
}

foo
();
foo
();
foo
();
?>
上邊的例子將輸出: 1 2 3

?

如果您想在函數(shù)中?unset()?一個全局變量,可使用?$GLOBALS?數(shù)組來實現(xiàn):

<?php
function?foo
() {
???? unset($GLOBALS['bar'
]);
}

$bar?=?"something"
;
foo
();
?>

?

Note:?由于這是一個語言結(jié)構(gòu)而非函數(shù),因此它無法被變量函數(shù)調(diào)用。

參見?isset()?和?empty()。

defined

(PHP 4, PHP 5)

defined — Checks whether a given named constant exists

說明

bool?defined?(?string?$name?)

Checks whether the given constant exists and is defined.

Note: If you want to see if a variable exists, use?isset()?as?defined()?only applies to?constants. If you want to see if a function exists, use?function_exists().

參數(shù)

?

name

The constant name.

?

返回值

Returns?TRUE?if the named constant given by?name?has been defined,?FALSE?otherwise.

范例

?

Example#1 Checking Constants

<?php
/* Note the use of quotes, this is important.?? This example is checking
* if the string 'CONSTANT' is the name of a constant named CONSTANT */
if (defined('CONSTANT'
)) {
???? echo?CONSTANT
;
}
?>

array_key_exists

(PHP 4 >= 4.0.7, PHP 5)

array_key_exists — 檢查給定的鍵名或索引是否存在于數(shù)組中

說明

bool?array_key_exists?(?mixed?$key?,?array?$search?)

array_key_exists()?在給定的?key?存在于數(shù)組中時返回?TRUE。key?可以是任何能作為數(shù)組索引的值。array_key_exists()?也可用于對象。

?

Example#1?array_key_exists()?例子

<?php
$search_array?= array('first'?=>?1,?'second'?=>?4
);
if (array_key_exists('first',?$search_array
)) {
???? echo?"The 'first' element is in the array"
;
}
?>

?

Note:?在 PHP 4.0.6 中本函數(shù)名為?key_exists()

Example#2?array_key_exists()?與?isset()?對比

isset()?對于數(shù)組中為?NULL?的值不會返回?TRUE,而?array_key_exists()?會。

<?php
$search_array?= array('first'?=>?null,?'second'?=>?4
);

// returns false
isset($search_array['first'
]);

// returns true
array_key_exists('first',?$search_array
);
?>

參見?isset(),array_keys()?和?in_array()。

function_exists

(PHP 4, PHP 5)

function_exists — Return?TRUE?if the given function has been defined

說明

bool?function_exists?(?string?$function_name?)

Checks the list of defined functions, both built-in (internal) and user-defined, for?function_name?.

參數(shù)

?

function_name

The function name, as a string.

?

返回值

Returns?TRUE?if?function_name?exists and is a function,?FALSE?otherwise.

Note: This function will return?FALSE?for constructs, such as?include_once()?and?echo().

范例

?

Example#1?function_exists()?example

<?php
if (function_exists('imap_open'
)) {
???? echo?"IMAP functions are available.<br />\n"
;
} else {
???? echo?"IMAP functions are not available.<br />\n"
;
}
?>

?

注釋

Note: A function name may exist even if the function itself is unusable due to configuration or compiling options (with the?image?functions being an example).

method_exists

(PHP 4, PHP 5)

method_exists — 檢查類的方法是否存在

說明

bool?method_exists?(?object?$object?,?string?$method_name?)

如果?method_name?所指的方法在?object?所指的對象類中已定義,則返回?TRUE,否則返回?FALSE

?

Example#1?method_exists()?例子

<?php
$directory?= new?Directory('.'
);
var_dump(method_exists($directory,'read'
));
?>

上例將輸出:

bool(true)

?

參見?function_exists()?和?is_callable()。

轉(zhuǎn)載于:https://www.cnblogs.com/chyong168/archive/2011/11/20/2256035.html

總結(jié)

以上是生活随笔為你收集整理的php函数(检查变量、函数、键是否存在)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。