您的位置:首页 > 编程语言 > PHP开发

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

2011-11-20 13:55 676 查看

empty

(PHP 4, PHP 5)

empty — 检查一个变量是否为空

描述

bool [b]empty[/b] ( mixed $var )
如果 var 是非空或非零的值,则 empty() 返回 FALSE。换句话说,""、0、"0"、NULLFALSE、array()、var $var; 以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE

除了当变量没有置值时不产生警告之外,empty() 是 (boolean) var 的反义词。参见转换为布尔值获取更多信息。

Example#1 [b]empty()isset() 的一个简单比较。[/b]

<?php
$var = 0;

// 结果为 true,因为 $var 为空
if (empty($var)) {
echo '$var is either 0 or not set at all';
}

// 结果为 false,因为 $var 已设置
if (!isset($var)) {
echo '$var is not set at all';
}
?>



Note: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。



Note: empty() 只检测变量,检测任何非变量的东西都将导致解析错误。换句话说,后边的语句将不会起作用: empty(addslashes($name))


参见 isset()unset()array_key_exists()count()strlen()

isset

(PHP 4, PHP 5)

isset — 检测变量是否设置

描述

bool [b]isset[/b] ( mixed $var [, mixed $var [, $... ]] )
如果 var 存在则返回 TRUE,否则返回 FALSE

如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节("\0")并不等同于 PHP 的 NULL 常数。


Note: 警告 isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用 defined() 函数。



<?php

$var = '';

// 结果为 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

?>


这对于数组中的元素也同样有效:

<?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: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。


参见 empty()unset()defined()array_key_exists() 和错误控制 @ 运算符。

unset

(PHP 4, PHP 5)

unset — 释放给定的变量

描述

void [b]unset[/b] ( mixed $var [, mixed $var [, $... ]] )
unset() 销毁指定的变量。注意在 PHP 3 中,unset() 将返回 TRUE(实际上是整型值 1),而在 PHP 4 中,unset() 不再是一个真正的函数:它现在是一个语句。这样就没有了返回值,试图获取 unset() 的返回值将导致解析错误。

Example#1 [b]unset() 示例[/b]

<?php
// 销毁单个变量
unset ($foo);

// 销毁单个数组元素
unset ($bar['quux']);

// 销毁一个以上的变量
unset ($foo1, $foo2, $foo3);
?>


unset() 在函数中的行为会依赖于想要销毁的变量的类型而有所不同。

如果在函数中 unset() 一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

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

$foo = 'bar';
destroy_foo();
echo $foo;
?>


上边的例子将输出:

bar


如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。

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

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

foo($bar);
echo "$bar\n";
?>


上边的例子将输出:

something
something


如果在函数中 unset() 一个静态变量,则 unset() 将销毁此变量及其所有的引用。

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

foo();
foo();
foo();
?>


上边的例子将输出:

1
2
3


如果您想在函数中 unset() 一个全局变量,可使用 $GLOBALS 数组来实现:

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

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



Note: 由于这是一个语言结构而非函数,因此它无法被变量函数调用。


参见 isset()empty()

defined

(PHP 4, PHP 5)

defined — Checks whether a given named constant exists

说明

bool [b]defined[/b] ( 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().



参数

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 — 检查给定的键名或索引是否存在于数组中

说明

bool [b]array_key_exists[/b] ( mixed $key , array $search )
array_key_exists() 在给定的 key 存在于数组中时返回 TRUE。key 可以是任何能作为数组索引的值。array_key_exists() 也可用于对象。

Example#1 [b]array_key_exists() 例子[/b]

<?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 中本函数名为 key_exists()



Example#2 [b]array_key_exists()isset() 对比[/b]

isset() 对于数组中为 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 [b]function_exists[/b] ( string $function_name )
Checks the list of defined functions, both built-in (internal) and user-defined, for function_name .

参数

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 [b]function_exists() example[/b]

<?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 [b]method_exists[/b] ( object $object , string $method_name )
如果 method_name 所指的方法在 object 所指的对象类中已定义,则返回 TRUE,否则返回 FALSE

Example#1 [b]method_exists() 例子[/b]

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


上例将输出:

bool(true)


参见 function_exists()is_callable()




                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: