Mohammed H
Mohammed H

Reputation: 7048

How to check an index is exist and not null in php

I am always using the following code to check if an index exists and is not null:

if(!(isset($_POST['service']) and $_POST['service']))
   die('The service parameter is not available.');

Here I am checking two conditions. Is it possible to do it using a single buit-in function? eg:-

if(!isSetAndNotNull($_POST['service']))
   die('The service parameter is not available.');

Upvotes: 3

Views: 742

Answers (2)

Alvaro
Alvaro

Reputation: 41605

Rather than empty I would use !:

if(!$_POST['service'])
   die('The service parameter is not available.');

In case of using it with functions it avoid troubles.

Explanation here: https://stackoverflow.com/a/4328049/1081396

Upvotes: 1

Najzero
Najzero

Reputation: 3212

You should use empty() for that.

http://php.net/manual/en/function.empty.php

returns false for empty(false) or empty(null)

Upvotes: 6

Related Questions