Desmond Hume
Desmond Hume

Reputation: 8607

Are isset() and !== null equivalent?

Since isset appears to be a function (http://php.net/manual/en/function.isset.php), there might be some overhead for calling it. So I wonder if using !== null instead of isset would produce faster code while, most importantly, keeping the code's behavior exactly the same?

Upvotes: 3

Views: 3460

Answers (4)

Elliot Lings
Elliot Lings

Reputation: 1106

From PHP Manual:

isset — Determine if a variable is set and is not NULL

isset Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

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

The function call overhead is so small you probably don't need to worry about it. Read this post: Why are PHP function calls *so* expensive?

Note that isset is not a function (it has a special opcode for it), so it's faster.

Upvotes: 6

Hari Ramamurthy
Hari Ramamurthy

Reputation: 96

as above isset is to check if a variable is set and is not NULL. To make sure I usually use it as

if( isset( $var ) === TRUE )
{
 // Do what you want
}

This doesn't throw any unnecessary notices in PHP

Upvotes: 0

JohnnyFaldo
JohnnyFaldo

Reputation: 4161

What about $foo = NULL, a variable can be set, and also be null

Upvotes: 1

srain
srain

Reputation: 9082

isset is not a function, It is a Language construct in PHP, It is much faster.

isset determines if a variable is set and is not NULL.

Upvotes: 0

Related Questions