1

I am trying to comprehend how PHP's isset() and is_null() functions are different from one another.

I do understand their difference from the manual, but cannot come up with any example of when I absolutely need to use one function over another.

It seems I can use one or the other and it wouldn't matter, except that isset doesn't throw a Notice error if the reference didn't exist, while is_null would.

If I suppress such type of errors, then in terms of functionality these 2 functions would be exactly similar.

Have I understood it correctly?

aliteralmind
  • 19,847
  • 17
  • 77
  • 108
user1720897
  • 1,216
  • 3
  • 12
  • 27
  • 2
    My question isn't a duplicate. Please read through the whole question. What I am asking here is that if I suppress "Notice" errors , then in terms of functionality these 2 functions would be exactly similar. I only need people to confirm it or convince me otherwise – user1720897 Mar 01 '14 at 06:20
  • 2
    1) You shouldn't be suppressing errors. 2) These two functions are different as explained in the duplicate answer. – Kermit Mar 01 '14 at 06:25
  • Suppressing the errors is a test scenario – user1720897 Mar 01 '14 at 06:28
  • Which proves nothing and is completely irrelevant to the functionality of the two functions. – Kermit Mar 01 '14 at 06:29
  • I haven't set out to prove anything. It is only a means to understand how one is different from another. Like I said before, I do understand the difference between the 2, but given a specific scenario they seem to be the same. – user1720897 Mar 01 '14 at 06:34
  • Suppressing errors is not a realistic test case scenario. I have never heard QA tell devs to make you sure you test functionality at different error reporting levels. – Kermit Mar 01 '14 at 06:41
  • I am not saying its a test scenario for the people in QA. I am only trying to see if I can replace one function with another in specific scenarios. Please understand that the reason I am doing so is because it makes it easier for me to see the difference. I can say, this is where it behaves similarly and this is where it would behave differently. – user1720897 Mar 01 '14 at 06:49

1 Answers1

3

null means nothing, no value, whereas you use isset() say whether a variable isset, for example..

$var = null;

isset($var); //returns false, checks whether the variable isset, 
             //but it's null so will return false

is_null($var); //Returns true, checks whether the value of variable is null

From Docs :

If a variable has been unset with unset(), it will no longer be set. isset() will return FALSE if testing a variable that has been set to NULL.

Going further, we alter the variable value

$var = 'hello';
isset($var); //will return true as the variable is no more null
is_null($var); //will return false as variable holds a string now

If I suppress such type of errors

No you shouldn't, always turn on the error reporting, writing bad code won't help you anyways.

Mr. Alien
  • 153,751
  • 34
  • 298
  • 278
  • I am trying to understand the functions better. Suppressing the errors is a test scenario. Can you give me one example of when I have to use one over the other. – user1720897 Mar 01 '14 at 06:27
  • When posting a form, you want to check `if(isset($_POST...))` and not `is_null` because the variable can be *set* and pass your logic. – Kermit Mar 01 '14 at 06:31
  • @user1720897 `is_null()` checks the value of your var, where as `isset()` checks whether a variable isset and is not null – Mr. Alien Mar 01 '14 at 06:31