यहां आपको क्या काम करता है की एक पूरी टूटने और जब:
<?
echo "<pre>";
$nullVariable = null;
echo 'is_null($nullVariable) = ' . (is_null($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nullVariable) = ' . (empty($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nullVariable) = ' . (isset($nullVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nullVariable = ' . ($nullVariable ? 'TRUE' : 'FALSE') . "\n\n";
$emptyString = '';
echo 'is_null($emptyString) = ' . (is_null($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($emptyString) = ' . (empty($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($emptyString) = ' . (isset($emptyString) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$emptyString = ' . ($emptyString ? 'TRUE' : 'FALSE') . "\n\n";
//note that the only one that won't throw an error is isset()
echo 'is_null($nonexistantVariable) = ' . (@is_null($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'empty($nonexistantVariable) = ' . (@empty($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo 'isset($nonexistantVariable) = ' . (isset($nonexistantVariable) ? 'TRUE' : 'FALSE') . "\n";
echo '(bool)$nonexistantVariable = ' . (@$nonexistantVariable ? 'TRUE' : 'FALSE') . "\n\n";
?>
उत्पादन:
is_null($nullVariable) = TRUE
empty($nullVariable) = TRUE
isset($nullVariable) = FALSE
(bool)$nullVariable = FALSE
is_null($emptyString) = FALSE
empty($emptyString) = TRUE
isset($emptyString) = TRUE
(bool)$emptyString = FALSE
is_null($nonexistantVariable) = TRUE
empty($nonexistantVariable) = TRUE
isset($nonexistantVariable) = FALSE
(bool)$nonexistantVariable = FALSE
जब मैं (bool)$variable
ऊपर दिखाने के लिए, कि आप इसे कैसे में इस्तेमाल कर सकते हैं है एक सशर्त उदाहरण के लिए, यदि किसी वैरिएबल शून्य या खाली है की जाँच करने के लिए, तुम कर सकते हो:
if (!$variable)
echo "variable is either null or empty!";
लेकिन यह क्योंकि यह एक छोटे से अधिक पठनीय है एक समारोह का उपयोग करने के लिए सबसे अच्छा है। लेकिन यह तुम्हारी पसंद है।
इसके अलावा, the PHP type comparison table देखें। यह मूल रूप से मैंने अभी ऊपर किया है, और भी छोड़कर।
व्यापक और सटीक, +1 – dnagirl
Yepp भी एक +1 मुझ से स्पष्टीकरण – streetparade