Parola chiave vuota PHP
Esempio
Controlla se una variabile è vuota:
<?php
$str = "";
if(empty($str)) {
echo "The string is
empty";
}
?>
Definizione e utilizzo
La empty
parola chiave agisce come una funzione che restituisce true se una variabile non esiste o se il suo valore è considerato vuoto. La empty
parola chiave valuta anche le espressioni che non sono in una variabile.
Un valore è considerato vuoto se il suo valore è uno dei seguenti:
- Una stringa vuota
- Un array vuoto
- Il numero intero 0
- Il numero in virgola mobile 0.0
- La stringa "0"
- Booleano falso
- nullo
Altri esempi
Esempio
Utilizzare empty
su una varietà di espressioni diverse:
<?php
// A variable that does not exist
if(empty($x)) {
echo '$x does not exist<br>';
}
// An empty integer
if(empty(0)) {
echo '0 is empty<br>';
}
// An empty float
if(empty(0.0)) {
echo '0.0 is empty<br>';
}
// An empty string
if(empty("")) {
echo '"" is an empty string<br>';
}
// null
if(empty(null)) {
echo 'null is empty<br>';
}
// A value that is not empty
if(empty('A')) {
echo '"A" is empty<br>';
} else {
echo
'"A" is not empty<br>';
}
?>
❮ Parole chiave PHP