0
Q:

isset in php

<?php

  $a = 0;
// True because $a is set
if (isset($a)) {
  echo 
  "Variable 'a' is set.<br>";
}

$b = null;
// False because $b is 
  NULL
if (isset($b)) {
  echo "Variable 'b' is set.";
}
?>
 
4
/**
 * Returns a bool (true or false)
 */
isset($x);
/**
 * Examples
 */
$x = 'myValue';
if(isset($x)){
	echo 'x is set';
}
/**
 * this will echo out 'x is set'
 */


$x = null;
if(isset($x)){
	echo 'x is set';
}
/**
 * This will NOT echo out 'x is set'
 */


if(isset($y)){
 	echo 'y is set'; 
}
/**
 * This will NOT echo out 'y is set'
 */

9
$age = 0;
// Evaluates as true because $age is set
if (isset($age)) {
echo '$age is set even though it is empty';
}
1
The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL.

This function returns true if the variable exists and is not NULL, otherwise it returns false.
1
//with new features in new PHP versions like 7
//you can simplify writing of isset in the following way,
//old way of isset to display name if name variable is not null
echo isset($name) ? $name : "no name"
 //new and simple way with null coalescing operator
echo $name ?? "no name"
0

New to Communities?

Join the community