Milos
Milos

Reputation: 328

PHP functions return null instead of boolean

In one of PHP questions on assessmentee.com, following code:

$a = "";
echo gettype($a);
echo empty($a);
echo is_null($a);
echo isset($x);

returns only "string1"

Why don't we have three bolean values returned, one for each of three functions: empty(), is_null() and isset()?

Upvotes: 1

Views: 1396

Answers (3)

Two
Two

Reputation: 656

echo gettype($a);  // Outputs a "string" because the datatype used is a string
echo empty($a);    // Outputs true, because the criteria that it is an empty string
echo is_null($a);  // Outputs false, "" isn't null, this is probably blank
echo isset($x);    // Outputs false because isset means "is set"

Upvotes: 1

Eric Day
Eric Day

Reputation: 152

You get all the results :string, true, false & false

echo gettype($a);  // outputs "string"
echo empty($a);    // outputs 1 (true)
echo is_null($a);  // outputs false, or "" in echo
echo isset($x);    // outputs false, or "" in echo

Your could try running it this way to see the different resuls:

echo gettype($a),'-',empty($a),'-',is_null($a),'-',isset($x),'-';

output: string-1---

Upvotes: 1

Sam Dean
Sam Dean

Reputation: 404

echo gettype($a);  // outputs "string"
echo empty($a);    // outputs true, in your environment this is 1
echo is_null($a);  // outputs false, "" isn't null, in your environment this is probably blank
echo isset($x);    // outputs false, in your environment this is probably blank

Upvotes: 5

Related Questions