2010-03-19 5 views
12

Je suis parfois confondu à l'aide duquel l'un d'eux,Quelle fonction dois-je utiliser pour tester si un var est défini ou non?

que j'ai une fonction appelée getmember($id)

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; 
} 

} 

C'est parfois pas clair pour moi, parfois si un paramètre dans une fonction est définie comme function($var="")

Alors je

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

Que dois-je utiliser la prochaine fois que isset ? empty ? or ===''?

Répondre

10

Ici, vous allez, une ventilation complète de ce qui fonctionne et quand:

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

LA SORTIE:

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 

Quand je montre (bool)$variable ci-dessus, c'est la façon dont vous pouvez l'utiliser dans un conditionnel. Par exemple, pour vérifier si une variable est nulle ou vide, vous pouvez faire:

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

Mais il est préférable d'utiliser une fonction car il est un peu plus lisible. Mais c'est ton choix.

De même, vérifiez the PHP type comparison table. C'est essentiellement ce que je viens de faire ci-dessus, sauf beaucoup plus.

+0

complète et précise, +1 – dnagirl

+0

Yepp aussi un +1 de moi merci pour l'explication – streetparade

3

Si vous voulez simplement savoir si une variable est définie, utilisez isset()

Si vous voulez voir si elle a été initialisé, utilisez is_null()

Si vous voulez comparer sa valeur à autre chose, utiliser ==

+0

'empty' est meilleure que' is_null() 'comme parfois variables sont initialisés avec une chaîne de longueur zéro comme' $ x = ' '; ' – Andy

+0

Je pense que is_null est vrai dans la valeur = null ou il était unsetet avant? – streetparade

Questions connexes