Bir var ayarlamak veya değilse hangi fonksiyon test etmek için kullanmak gerekir?

3 Cevap

Bazen, bunların hangi birini kullanarak karıştı

I getmember($id) adında bir işlevi var demek

function getmember($id)
{

// now this is the confusing part 
// how do i test if a $id was set or not set?

//solution 1
if(empty($id))
{
return false;
}


// solution 2

if(isset($id))
{
return false;
}

}

Bir işlev içinde bir parametre gibi ayarlanır bazen eğer, bazen benim için açık değil function($var="")

Sonra do

if($var ==="")
{
return false;
} 

Ne bir dahaki sefere kullanmak gerekir isset ? empty ? or ===''?

3 Cevap

Burada, tam olanı dökümü ve ne zaman gitmek:

<?
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";
?>

THE OUTPUT:

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

Ben göstermek zaman (bool)$variable Yukarıda, size bir koşullu kullanmak nasıl olduğunu. Örneğin, bir değişken null veya boş olup olmadığını kontrol etmek için, bunu yapabilirsiniz:

if (!$variable)
    echo "variable is either null or empty!";

Ama bu biraz daha okunabilir olduğundan bir fonksiyonu kullanmak en iyisidir. Ama bu sizin seçiminiz.

Also, check the PHP type comparison table. Bu çok daha dışında, ben sadece yukarıda ne yaptığını temelde.

Sadece bir değişken tanımlı ise, kullanmak isset() bilmek istiyorum

Eğer başlatılmadı eğer, kullanmak görmek istiyorsanız is_null()

Eğer karşılaştırmak isterseniz başka bir şeye değer, kullanım ==

Aynı Değil:

isset: Determine if a variable is set and is not NULL http://ch.php.net/manual/en/function.isset.php

empty: Determine whether a variable is empty http://ch.php.net/manual/en/function.empty.php

$a===$b: TRUE if $a is equal to $b, and they are of the same type. http://ch.php.net/manual/en/language.operators.comparison.php