अन्य पहले से ही इस सवाल का जवाब दे चुके हैं। मैं केवल कुछ अन्य उदाहरण देना चाहता हूं, आपको अवगत होना चाहिए, सभी PHP के प्रकार की जुगलिंग के कारण होते हैं। सभी निम्नलिखित तुलना सच वापस आ जाएगी:
- 'एबीसी' == 0
- 0 == बातिल
- '' == बातिल
- 1 == '1 वर्ष?z '
क्योंकि मैं इस व्यवहार खतरनाक पाया, मैं अपने बराबर विधि लिखा था और अपनी परियोजनाओं में उपयोग:
/**
* Checks if two values are equal. In contrast to the == operator,
* the values are considered different, if:
* - one value is null and the other not, or
* - one value is an empty string and the other not
* This helps avoid strange behavier with PHP's type juggling,
* all these expressions would return true:
* 'abc' == 0; 0 == null; '' == null; 1 == '1y?z';
* @param mixed $value1
* @param mixed $value2
* @return boolean True if values are equal, otherwise false.
*/
function sto_equals($value1, $value2)
{
// identical in value and type
if ($value1 === $value2)
$result = true;
// one is null, the other not
else if (is_null($value1) || is_null($value2))
$result = false;
// one is an empty string, the other not
else if (($value1 === '') || ($value2 === ''))
$result = false;
// identical in value and different in type
else
{
$result = ($value1 == $value2);
// test for wrong implicit string conversion, when comparing a
// string with a numeric type. only accept valid numeric strings.
if ($result)
{
$isNumericType1 = is_int($value1) || is_float($value1);
$isNumericType2 = is_int($value2) || is_float($value2);
$isStringType1 = is_string($value1);
$isStringType2 = is_string($value2);
if ($isNumericType1 && $isStringType2)
$result = is_numeric($value2);
else if ($isNumericType2 && $isStringType1)
$result = is_numeric($value1);
}
}
return $result;
}
आशा इस किसी अपने आवेदन अधिक ठोस बनाने में मदद करता है, मूल लेख हो सकता है यहां मिला: Equal or not equal
यह दिलचस्प है कि कितने लोगों ने प्रश्न को सही तरीके से पढ़ या समझ नहीं पाया। –