php判斷變數常量是否存在

來源:互聯網
上載者:User

defined() 函數檢查某常量是否存在。

若常量存在,則返回 true,否則返回 false。

 代碼如下 複製代碼


if (defined('MYCONSTANT')) {   
    echo "常量MYCONSTANT存在";
}else{
    echo "常量MYCONSTANT不存在";
}
echo "<br/>";


isset函數是檢測變數是否設定。

1.若變數不存在則返回 FALSE
2.若變數存在且其值為NULL,也返回 FALSE
3.若變數存在且值不為NULL,則返回 TURE
4.同時檢查多個變數時,每個單項都符合上一條要求時才返回 TRUE,否則結果為

 代碼如下 複製代碼

<?php

$var = '';

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

?>

 

function_exists判斷函數是否存在

 代碼如下 複製代碼

<?php
if (function_exists('test_func')) {
    echo "函數test_func存在";
} else {
    echo "函數test_func不存在";
}
?>

filter_has_var函數

filter_has_var() 函數檢查是否存在指定輸入類型的變數。

若成功,則返回 true,否則返回 false。

 代碼如下 複製代碼

<?php
if(!filter_has_var(INPUT_GET, "name"))
 {
 echo("Input type does not exist");
 }
else
 {
 echo("Input type exists");
 }
?>

輸出為. Input type exists

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.