variable para verificar si una variable var está configurada o no
Enfoque 1: uso del método() : el () devuelve verdadero si la variable se declara y su valor no es igual a NULL.
Sintaxis:
bool isset( mixed $var [, mixed $... ] )
Ejemplo :
PHP
<?php // PHP program to check whether // a variable is set or not $str = "GeeksforGeeks"; // Check value of variable is set or not if(isset($str)) { echo "Value of variable is set"; } else { echo "Value of variable is not set"; } ?>
Producción
Value of variable is set
Enfoque 2: Usar ! Método vacío() : el método vacío() devuelve verdadero si la variable se declara y su valor es igual a vacío y no a un conjunto.
Sintaxis:
bool empty( $var )
Ejemplo :
PHP
<?php // PHP program to check whether // a variable is set or not $str = "GeeksforGeeks"; // Check value of variable is set or not if(!empty($str)) { echo "Value of variable is set"; } else { echo "Value of variable is not set"; } ?>
Producción
Value of variable is set
Enfoque 3: Usando ! Método is_null() : El método is_null() devuelve True si la variable se declara y su valor es igual a Null y no a un conjunto.
Sintaxis:
bool empty( $var )
Ejemplo :
PHP
<?php // PHP program to check whether // a variable is set or not $str = "GeeksforGeeks"; // Check value of variable is set or not if(!is_null($str)) { echo "Value of variable is set"; } else { echo "Value of variable is not set"; } ?>
Producción
Value of variable is set
Publicación traducida automáticamente
Artículo escrito por SHUBHAMSINGH10 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA