# PHP 7.1 - Address PHPSadness #28?

- Thread: https://externals.io/message/87917
- Original archive: https://news-web.php.net/php.internals/87917
- Started by Scott Arciszewski on 2015-08-26T03:09:43+00:00
- Messages: 142
- Page 1 of 6: messages 1 to 25 (25 per page, add `?page=N` to the URL)
- Next page: https://externals.io/message/87917.md?page=2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #87917 — Scott Arciszewski — 2015-08-26T03:09:43+00:00

URL: https://externals.io/message/87917#87917

Hi everybody,

Would anyone be interested in adding another helper like
isset()/empty() simply called exists() which would return true if the
variable is defined in the current scope (i.e. without raising an
E_NOTICE)?

It should be a simple change to add this function but it's too late
for 7.0 so, if there is any interest, I would respectfully put it off
until 7.1.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #87918 — Sherif Ramadan — 2015-08-26T03:11:57+00:00

URL: https://externals.io/message/87917#87918 · In reply to: #87917

That's exactly what isset() does. isset/empty never raise errors.

On Tue, Aug 25, 2015 at 11:09 PM, Scott Arciszewski <scott@paragonie.com>
wrote:

> Hi everybody,
>
> Would anyone be interested in adding another helper like
> isset()/empty() simply called exists() which would return true if the
> variable is defined in the current scope (i.e. without raising an
> E_NOTICE)?
>
> It should be a simple change to add this function but it's too late
> for 7.0 so, if there is any interest, I would respectfully put it off
> until 7.1.
>
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises <https://paragonie.com

---

## #87919 — Scott Arciszewski — 2015-08-26T03:13:42+00:00

URL: https://externals.io/message/87917#87919 · In reply to: #87918

On Tue, Aug 25, 2015 at 11:11 PM, Sherif Ramadan
<theanomaly.is@gmail.com> wrote:
> That's exactly what isset() does. isset/empty never raise errors.
>
> On Tue, Aug 25, 2015 at 11:09 PM, Scott Arciszewski <scott@paragonie.com>
> wrote:
> > […]
>
http://phpsadness.com/sad/28
https://3v4l.org/2vrKG

Not quite.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #87920 — Sherif Ramadan — 2015-08-26T03:16:43+00:00

URL: https://externals.io/message/87917#87920 · In reply to: #87919

Yes, because all undefined variables in PHP are implicitly null. As far as
PHP is concerned, if it's null, it's by definition _not defined_. Also,
still not seeing where a NOTICE is involved here? Those functions never
give errors.

What is the usefulness of a function that merely returns true when a
variable is null?

On Tue, Aug 25, 2015 at 11:13 PM, Scott Arciszewski <scott@paragonie.com>
wrote:

> On Tue, Aug 25, 2015 at 11:11 PM, Sherif Ramadan
> <theanomaly.is@gmail.com> wrote:
> > […]
> http://phpsadness.com/sad/28
> https://3v4l.org/2vrKG
>
> Not quite.
>
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises <https://paragonie.com

---

## #87921 — Scott Arciszewski — 2015-08-26T03:18:21+00:00

URL: https://externals.io/message/87917#87921 · In reply to: #87920

Because $x exists.

https://3v4l.org/kUJtP
Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises


On Tue, Aug 25, 2015 at 11:16 PM, Sherif Ramadan
<theanomaly.is@gmail.com> wrote:
> Yes, because all undefined variables in PHP are implicitly null. As far as
> PHP is concerned, if it's null, it's by definition _not defined_. Also,
> still not seeing where a NOTICE is involved here? Those functions never give
> errors.
>
> What is the usefulness of a function that merely returns true when a
> variable is null?
>
> On Tue, Aug 25, 2015 at 11:13 PM, Scott Arciszewski <scott@paragonie.com>
> wrote:
> > […]

---

## #87922 — Sherif Ramadan — 2015-08-26T03:26:38+00:00

URL: https://externals.io/message/87917#87922 · In reply to: #87921

I get that it exists. I'm asking what is the usefulness of having a
function that tells you the variable is null? In theory this sounds great
and is actually very doable by checking the symbol table for the hash.
However, in practice, who actually has a use for this? Null is a special
value in PHP, so if you're assigning it to a variable explicitly you should
already know you've initialized that variable and can safely rely on if ($x
=== null) in your code.

I'm not confused about the differences. I'm questioning the usefulness of
such a function.

On Tue, Aug 25, 2015 at 11:18 PM, Scott Arciszewski <scott@paragonie.com>
wrote:

> Because $x exists.
>
> https://3v4l.org/kUJtP
> Scott Arciszewski
> Chief Development Officer
> Paragon Initiative Enterprises
>
>
> On Tue, Aug 25, 2015 at 11:16 PM, Sherif Ramadan
> <theanomaly.is@gmail.com> wrote:
> > […]
> as
> > […]
> give
> > […]

---

## #87923 — Marco Pivetta — 2015-08-26T03:33:13+00:00

URL: https://externals.io/message/87917#87923 · In reply to: #87922

You are probably looking for something like `array_key_exists($varName,
 `get_defined_vars()`);`: https://3v4l.org/XagEA

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/



> I get that it exists. I'm asking what is the usefulness of having a
> function that tells you the variable is null? In theory this sounds great
> and is actually very doable by checking the symbol table for the hash.
> However, in practice, who actually has a use for this? Null is a special
> value in PHP, so if you're assigning it to a variable explicitly you should
> already know you've initialized that variable and can safely rely on if ($x
> === null) in your code.
>
> I'm not confused about the differences. I'm questioning the usefulness of
> such a function.
>
> On Tue, Aug 25, 2015 at 11:18 PM, Scott Arciszewski <scott@paragonie.com>
> wrote:
>
> > […]
> scott@paragonie.com
> > […]
> the
> > […]
> off
> > […]

---

## #87924 — Stanislav Malyshev — 2015-08-26T04:36:57+00:00

URL: https://externals.io/message/87917#87924 · In reply to: #87917

Hi!

> Would anyone be interested in adding another helper like
> isset()/empty() simply called exists() which would return true if the
> variable is defined in the current scope (i.e. without raising an
> E_NOTICE)?

Not sure what the use case would be for this. For most practical
purposes, isset/empty look completely fine.

> It should be a simple change to add this function but it's too late
> for 7.0 so, if there is any interest, I would respectfully put it off
> until 7.1.

It is simple, but not everything that is simple should be done.
Especially when we talking about adding core functions.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #87926 — Rowan Collins — 2015-08-26T10:10:34+00:00

URL: https://externals.io/message/87917#87926 · In reply to: #87917

Scott Arciszewski wrote on 26/08/2015 04:09:
> Hi everybody,
>
> Would anyone be interested in adding another helper like
> isset()/empty() simply called exists() which would return true if the
> variable is defined in the current scope (i.e. without raising an
> E_NOTICE)?

My answer to this would be a very loud "no".

I have written at length about this on StackOverflow: 
http://stackoverflow.com/a/18646568/157957

In short, if your algorithm relies on knowing whether, anywhere in the 
preceding scope, a variable has been implicitly "declared" (in as much 
as that concept has any meaning in PHP), that algorithm is broken. If it 
relies on knowing whether a variable has been changed from its initial 
state, then isset() will do exactly what you want.

Note that many people looking for examples of this "problem" start 
mentioning array keys and object properties; we have functions for 
checking existence of those, so please don't waste time on such examples.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #87930 — Craig Francis — 2015-08-26T11:23:07+00:00

URL: https://externals.io/message/87917#87930 · In reply to: #87924

> 
> > […]
> 
> Not sure what the use case would be for this. For most practical
> purposes, isset/empty look completely fine.




As a web (not core) developer I've been bitten by this a couple of times.

Both were fairly quick to identify/fix, but enough to be a little annoying.

If you want some example of where I got this wrong...



I have a config array for defining the websites CSP header, it starts something like:

    $csp_directives = array(
            'default-src'  => array("'none'"),
            'form-action'  => array("'self'"),
            'style-src'    => array("'self'"),
            'img-src'      => array("'self'"),
            'script-src'   => array("'self'"),
        );

Depending on the website I may then add a key to define the 'report-uri', or I may set it to `NULL` to say that I don't want one. If it remains unset (key does not exist), then the framework provides a default. At first I used an isset() check, forgetting that setting to `NULL` would not be seen as "set".



When creating a URL builder that allowed the query string to be passed in as an array. If it defaulted to the current URL (with its query string values), then the passed in array might tell it to explicitly remove certain values by setting them to NULL. When I first wrote this, I did an isset() check when adding the current URL's query string to the array of query string values (which take precedence).



Neither were major problems, just a slight annoyance (i.e. identified and fixed before I committed the code). But I'm normally using an isset() as the proposed exists() function would work.









> Hi!
> 
> > […]
> 
> Not sure what the use case would be for this. For most practical
> purposes, isset/empty look completely fine.
> 
> > […]
> 
> It is simple, but not everything that is simple should be done.
> Especially when we talking about adding core functions.
> 
> -- 
> Stas Malyshev
> smalyshev@gmail.com

---

## #87931 — Rowan Collins — 2015-08-26T11:36:41+00:00

URL: https://externals.io/message/87917#87931 · In reply to: #87930

Craig Francis wrote on 26/08/2015 12:23:
>
> If you want some example of where I got this wrong...
>
> I have a config array [...]
>
> When creating a URL builder that allowed the query string to be passed in as an array [...]
>
> Neither were major problems, just a slight annoyance (i.e. identified and fixed before I committed the code). But I'm normally using an isset() as the proposed exists() function would work.

Both of these example involve not an undefined variable, but an 
undefined array key, which is a much less controversial scenario.

Fortunately, the function you are looking for already exists: 
http://php.net/array_key_exists

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #87932 — Craig Francis — 2015-08-26T13:53:51+00:00

URL: https://externals.io/message/87917#87932 · In reply to: #87931

> Fortunately, the function you are looking for already exists: http://php.net/array_key_exists



It does, although I seem to have edited that out of my reply... was trying to keep it short ;-)

At the moment I'm out of examples (bit busy at the moment), but I do know that when I'm doing an isset() check, I'm really wanting to know if the variable (or key) exists, where `NULL` may be a perfectly valid value.

So there is a slight annoyance (and typically a quick fix) to identify if the variable has just been set to NULL.









> Craig Francis wrote on 26/08/2015 12:23:
> > […]
> 
> Both of these example involve not an undefined variable, but an undefined array key, which is a much less controversial scenario.
> 
> Fortunately, the function you are looking for already exists: http://php.net/array_key_exists
> 
> Regards,
> -- 
> Rowan Collins
> [IMSoP]

---

## #87933 — Rowan Collins — 2015-08-26T15:06:55+00:00

URL: https://externals.io/message/87917#87933 · In reply to: #87932

Craig Francis wrote on 26/08/2015 14:53:
> 
>
> > […]
> It does, although I seem to have edited that out of my reply... was trying to keep it short ;-)

You could have kept the reply even shorter by omitting examples for 
which the language already provides the solution ;)


> At the moment I'm out of examples (bit busy at the moment), but I do know that when I'm doing an isset() check, I'm really wanting to know if the variable (or key) exists

Stop. As soon as you say "or key" you completely defeat your argument. 
The whole thing is ONLY worth discussing if you can come up with an 
example of an actual variable having unknown existence.


> where `NULL` may be a perfectly valid value.

It's not that `NULL` isn't a valid value; it's that "doesn't exist" isn't 
a meaningful state for a variable. It's like checking if the current 
line number is greater than 100, it shouldn't mean anything to the 
compiled program. See the SO answer I linked earlier for more thought 
experiments along these lines.

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #87935 — Stanislav Malyshev — 2015-08-26T15:44:33+00:00

URL: https://externals.io/message/87917#87935 · In reply to: #87930

Hi!

> Depending on the website I may then add a key to define the
> 'report-uri', or I may set it to `NULL` to say that I don't want one.

 `NULL` is not the best value to for this, but the function you're looking
for is `array_key_exists()`.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #87936 — Craig Francis — 2015-08-26T17:07:10+00:00

URL: https://externals.io/message/87917#87936 · In reply to: #87933

> Craig Francis wrote on 26/08/2015 14:53:
> > […]
> 
> You could have kept the reply even shorter by omitting examples for which the language already provides the solution ;)


I provide examples to help explain that I (and I suspect most developers) default to using isset which works on either.

Just because there is a function, which does not exactly roll off the tongue (plus the fun of the needle/haystack ordering issue), does not mean it gets used (even if it is the correct thing to use).




> > […]
> 
> Stop. As soon as you say "or key" you completely defeat your argument. The whole thing is ONLY worth discussing if you can come up with an example of an actual variable having unknown existence.


I say "or key" because developers use the isset function on both $var and $var['key']... yes, I know they are different, but when you are coding in PHP an isset check is an isset check (well it isn't, because the variable may technically exist).

If this is a problem, maybe PHP should raise a notice when isset is used on arrays?




> > […]
> 
> It's not that `NULL` isn't a valid value; it's that "doesn't exist" isn't a meaningful state for a variable. It's like checking if the current line number is greater than 100, it shouldn't mean anything to the compiled program. See the SO answer I linked earlier for more thought experiments along these lines.


I think you have been spending too much time in C.

Most of the PHP code bases I've worked on set variables to `NULL` at some point (and they are not calling unset, because that isn't the intention).

---

## #87937 — Craig Francis — 2015-08-26T17:08:43+00:00

URL: https://externals.io/message/87917#87937 · In reply to: #87935

> > […]
> 
> `NULL` is not the best value to for this, but the function you're looking
> for is `array_key_exists()`.



What would be the best value?

And the `array_key_exists()` function has already been mentioned (and is what I'm using... but I cut that bit out to keep the reply shorter, as mentioned in my previous email).

---

## #87938 — Ryan Pallas — 2015-08-26T18:35:34+00:00

URL: https://externals.io/message/87917#87938 · In reply to: #87937

On Wed, Aug 26, 2015 at 11:08 AM, Craig Francis <craig@craigfrancis.co.uk>
wrote:

> 
>
> > […]
>
>
>
> What would be the best value?
>
> And the `array_key_exists()` function has already been mentioned (and is
> what I'm using... but I cut that bit out to keep the reply shorter, as
> mentioned in my previous email).
>
>
What I would do is use false to me I don't want this, and null/not set to
mean do the default, and a value to mean this is what I want. Then you end
up with:

if (!isset($val)) {
   // do the default
} elseif (!empty($val)) {
   // do something with $val
}
// else do nothing because I don't want this thing

On the other hand, if I'm designing something using null as a special case
value, then I would just use array_key_exists and is_null (these calls
exist a few times in my code) but generally speaking I try not to give null
special meaning.

---

## #87939 — James Gilliland — 2015-08-26T18:38:09+00:00

URL: https://externals.io/message/87917#87939 · In reply to: #87936

On Wed, Aug 26, 2015 at 12:07 PM, Craig Francis <craig@craigfrancis.co.uk>
wrote:

>
> 
>
> > […]
> wrote:
> > […]
>


> > […]
> a meaningful state for a variable. It's like checking if the current line
> number is greater than 100, it shouldn't mean anything to the compiled
> program. See the SO answer I linked earlier for more thought experiments
> along these lines.
>
>
> I think you have been spending too much time in C.
>
> Most of the PHP code bases I've worked on set variables to `NULL` at some
> point (and they are not calling unset, because that isn't the intention).
>
>
I think there is a misunderstanding here that is drawing this out.
Assigning meaning to `NULL` in any language is wrong. That is why it should
be treated as if the variable didn't exist and isset is appropriate.

This is a common pitfall in database design as well where you can allow
 `NULL` as a field value. Assigning meaning to to that `NULL` value can quickly
lead to problem.

---

## #87940 — Rowan Collins — 2015-08-26T18:44:23+00:00

URL: https://externals.io/message/87917#87940 · In reply to: #87936

Craig Francis wrote on 26/08/2015 18:07:
> I provide examples to help explain that I (and I suspect most developers) default to using isset which works on either.
>
> Just because there is a function, which does not exactly roll off the tongue (plus the fun of the needle/haystack ordering issue), does not mean it gets used (even if it is the correct thing to use).


That's all very well, but what is the Internals community supposed to do 
about it? Does the documentation need to be improved to point out that 
there is a better function to use for this case?

> I say "or key" because developers use the isset function on both $var and $var['key']... yes, I know they are different, but when you are coding in PHP an isset check is an isset check (well it isn't, because the variable may technically exist).
>
> If this is a problem, maybe PHP should raise a notice when isset is used on arrays?


It's only a problem in the same way that using `file_exists()` is a 
problem when you actually wanted the functionality of `is_readable()` - 
both functions exist, and it's not the language's responsibility to 
guess which you meant.



> > […]
>
> I think you have been spending too much time in C.
>
> Most of the PHP code bases I've worked on set variables to `NULL` at some point (and they are not calling unset, because that isn't the intention).


Perhaps my double negative made it unclear, so I will restate: there is 
nothing wrong with setting a variable to NULL. There is also nothing 
wrong with calling unset() on a plain variable, e.g. to make it obvious 
to readers of the code that this variable should not be used below this 
point.

There IS something wrong with any code which needs to distinguish which 
of those two things happened, at run-time, because such code would be 
incredibly fragile and hard to follow.

You could, if you wanted, emulate a boolean variable by using $foo=null 
for true, and unset($foo) for false, but why not simply have a boolean 
variable?

[Incidentally, I know barely any C, but program PHP for a living.]

Regards,
-- 
Rowan Collins
[IMSoP]

---

## #87941 — Bishop Bettini — 2015-08-26T18:50:34+00:00

URL: https://externals.io/message/87917#87941 · In reply to: #87917

On Tue, Aug 25, 2015 at 11:09 PM, Scott Arciszewski <scott@paragonie.com>
wrote:

> Would anyone be interested in adding another helper like
> isset()/empty() simply called exists() which would return true if the
> variable is defined in the current scope (i.e. without raising an
> E_NOTICE)?
>

Agree with Rowan as he mentioned here:
http://stackoverflow.com/a/18646568/2908724

Perhaps this idiom will solve your use case?

array_key_exists('x', compact('x')));

https://3v4l.org/TLdZI

Cheers,
bishop

---

## #87942 — Stanislav Malyshev — 2015-08-26T19:02:30+00:00

URL: https://externals.io/message/87917#87942 · In reply to: #87936

Hi!

> Just because there is a function, which does not exactly roll off the
> tongue (plus the fun of the needle/haystack ordering issue), does not
> mean it gets used (even if it is the correct thing to use).

As it looks to me, in order to fix the fact that people are
not using existing function (which completely satisfies the
requirements), because people do not read the docs, the proposal is to
create another function (which nobody knows about without reading the
docs) that does the same? If so, I'm not sure how that is supposed to
help anything.

-- 
Stas Malyshev
smalyshev@gmail.com

---

## #87943 — Lester Caine — 2015-08-26T19:38:10+00:00

URL: https://externals.io/message/87917#87943 · In reply to: #87942

> > […]

> As it looks to me, in order to fix the fact that people are
> not using existing function (which completely satisfies the
> requirements), because people do not read the docs, the proposal is to
> create another function (which nobody knows about without reading the
> docs) that does the same? If so, I'm not sure how that is supposed to
> help anything.

We have had discussions before over the various 'complaint' sites and
how in many cases the 'complaints' get more prominence than the actual
current 'solution'. Yet again, what is needed rather than a manual
describing each function individually since for example one would not
look under 'array_' for a solution to this problem, is a 'best practice'
example which can help even those of us who are stuck with legacy
practices which COULD be improved with better modern samples.

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

## #87987 — Craig Francis — 2015-09-01T09:29:10+00:00

URL: https://externals.io/message/87917#87987 · In reply to: #87940

Hi Rowan, Ryan, James, Bishop, Stas, Lester,

I've been giving this some thought over the weekend, and I agree with what you are all saying, but I think there is an element of confusion in the PHP programming community that needs to be addressed (somehow).

Yes, we probably should not be giving special meaning to NULL, as James mentions (although I probably still will).

And to be fair, I still can't think of an example where isset() can be used on a standard variable. All my examples are really array based, and better addressed with `array_key_exists()` - where Rowan does a great job of explaining the different examples on stackoverflow:

  http://stackoverflow.com/a/18646568/2908724

Trying to get to route of the confusion though, I think it might be down to how the code can be read. So if we take:

<?php
  $a = 1;
  $b = NULL;
  var_dump($a);
  var_dump($b);
  var_dump($c);
  var_dump(isset($a));
  var_dump(isset($b));
  var_dump(isset($c));
?>

Or using an array instead:

<?php
  $v['a'] = 1;
  $v['b'] = NULL;
  var_dump($v['a']);
  var_dump($v['b']);
  var_dump($v['c']);
  var_dump(isset($v['a']));
  var_dump(isset($v['b']));
  var_dump(isset($v['c']));
?>

In both cases (and ignoring the undefined variable error), we get:

  int(1)
  `NULL` 
  `NULL` 

  bool(true)
  bool(false)
  bool(false)

But if we ignore the manual for a minute:

  http://php.net/isset

I think a good comparison to what that code reads as, vs what PHP does, can be summarised as:

  Read as: The variable is set (exists).

  Executed as: The variables *value* is set.

Where `NULL` is not considered a set value.

So when we have:

  if (isset($v['a'])) {
  }

It's not saying that the key 'a' is set (exists) on the array $v.

Instead its saying that the key 'a' has a set value (not NULL) on the array $v.

Where this is made more of a common problem because there are many array_* functions, and they rarely come to mind when your not thinking of an array like operation.

This situation might be improved by re-wording the manual a bit.

Having headings like "This also work[s] for elements in arrays", makes it sound all inclusive and *the* function to use.

Whereas I'm starting to get the feeling that isset() is actually a function that probably should be avoided (I'll skip this tangent for now).

Personally I still like the idea of an exists(), because I feel that is how many programmers treat and use the isset() function - simply because they do use `NULL` as a valid value, and either haven't read the manual, or forget the exception that is mentioned on line 1 (something I've done a couple of times).

Although I realise it will take many years before anyone can start using it.

Craig










> Craig Francis wrote on 26/08/2015 18:07:
> > […]
> 
> 
> That's all very well, but what is the Internals community supposed to do about it? Does the documentation need to be improved to point out that there is a better function to use for this case?
> 
> > […]
> 
> 
> It's only a problem in the same way that using `file_exists()` is a problem when you actually wanted the functionality of `is_readable()` - both functions exist, and it's not the language's responsibility to guess which you meant.
> 
> 
> 
> > […]
> 
> 
> Perhaps my double negative made it unclear, so I will restate: there is nothing wrong with setting a variable to NULL. There is also nothing wrong with calling unset() on a plain variable, e.g. to make it obvious to readers of the code that this variable should not be used below this point.
> 
> There IS something wrong with any code which needs to distinguish which of those two things happened, at run-time, because such code would be incredibly fragile and hard to follow.
> 
> You could, if you wanted, emulate a boolean variable by using $foo=null for true, and unset($foo) for false, but why not simply have a boolean variable?
> 
> [Incidentally, I know barely any C, but program PHP for a living.]
> 
> Regards,
> -- 
> Rowan Collins
> [IMSoP]

---

## #87989 — Scott Arciszewski — 2015-09-01T09:38:24+00:00

URL: https://externals.io/message/87917#87989 · In reply to: #87987

> Hi Rowan, Ryan, James, Bishop, Stas, Lester,
>
> I've been giving this some thought over the weekend, and I agree with what you are all saying, but I think there is an element of confusion in the PHP programming community that needs to be addressed (somehow).
>
> Yes, we probably should not be giving special meaning to NULL, as James mentions (although I probably still will).
>
> And to be fair, I still can't think of an example where isset() can be used on a standard variable. All my examples are really array based, and better addressed with `array_key_exists()` - where Rowan does a great job of explaining the different examples on stackoverflow:
>
>   http://stackoverflow.com/a/18646568/2908724
>
> Trying to get to route of the confusion though, I think it might be down to how the code can be read. So if we take:
>
> <?php
>   $a = 1;
>   $b = NULL;
>   var_dump($a);
>   var_dump($b);
>   var_dump($c);
>   var_dump(isset($a));
>   var_dump(isset($b));
>   var_dump(isset($c));
> ?>
>
> Or using an array instead:
>
> <?php
>   $v['a'] = 1;
>   $v['b'] = NULL;
>   var_dump($v['a']);
>   var_dump($v['b']);
>   var_dump($v['c']);
>   var_dump(isset($v['a']));
>   var_dump(isset($v['b']));
>   var_dump(isset($v['c']));
> ?>
>
> In both cases (and ignoring the undefined variable error), we get:
>
>   int(1)
>   `NULL` 
>   `NULL` 
>
>   bool(true)
>   bool(false)
>   bool(false)
>
> But if we ignore the manual for a minute:
>
>   http://php.net/isset
>
> I think a good comparison to what that code reads as, vs what PHP does, can be summarised as:
>
>   Read as: The variable is set (exists).
>
>   Executed as: The variables *value* is set.
>
> Where `NULL` is not considered a set value.
>
> So when we have:
>
>   if (isset($v['a'])) {
>   }
>
> It's not saying that the key 'a' is set (exists) on the array $v.
>
> Instead its saying that the key 'a' has a set value (not NULL) on the array $v.
>
> Where this is made more of a common problem because there are many array_* functions, and they rarely come to mind when your not thinking of an array like operation.
>
> This situation might be improved by re-wording the manual a bit.
>
> Having headings like "This also work[s] for elements in arrays", makes it sound all inclusive and *the* function to use.
>
> Whereas I'm starting to get the feeling that isset() is actually a function that probably should be avoided (I'll skip this tangent for now).
>
> Personally I still like the idea of an exists(), because I feel that is how many programmers treat and use the isset() function - simply because they do use `NULL` as a valid value, and either haven't read the manual, or forget the exception that is mentioned on line 1 (something I've done a couple of times).
>
> Although I realise it will take many years before anyone can start using it.
>
> Craig
>
>
>
>
>
>
>
>
> 
>
> > […]
>
>
> --
> 
> 
>

The pieces are already in place, if this is a desired feature we just
have to bring them together.

* `array_key_exists()`
* `get_defined_vars()`

I do agree that it would be years before anyone got to use it (either
7.1.0 or 8.0.0). I don't see that as a bad thing, this was low-hanging
fruit.

Scott Arciszewski
Chief Development Officer
Paragon Initiative Enterprises <https://paragonie.com

---

## #87992 — Lester Caine — 2015-09-01T10:40:31+00:00

URL: https://externals.io/message/87917#87992 · In reply to: #87987

> Personally I still like the idea of an exists(), because I feel that is how many programmers treat and use the isset() function - simply because they do use `NULL` as a valid value, and either haven't read the manual, or forget the exception that is mentioned on line 1 (something I've done a couple of times).
> 
> Although I realise it will take many years before anyone can start using it.

For those of us starting from a base of SQL data, `NULL` has always been
'not set' and so a binary field can have three values 'yes', 'no' and
'don't know' so when that data is transferred into PHP it has always
been natural to maintain that distinction so `NULL` === not set.

That this data exists in an array in PHP is a natural development, but
the object orientated/class approach would be that the data is loaded
into an object and the values in that object are no longer considered as
array elements, but discrete variables with all the overheads of getters
and setters, so an 'isset' to check if the value HAS been converted from
 `NULL` to a live value is what is NOW the preferred style of working?
While the older style of working would simply directly read the elements
of the array?

-- 
Lester Caine - G8HFL
-----------------------------
Contact - http://lsces.co.uk/wiki/?page=contact
L.S.Caine Electronic Services - http://lsces.co.uk
EnquirySolve - http://enquirysolve.com/
Model Engineers Digital Workshop - http://medw.co.uk
Rainbow Digital Media - http://rainbowdigitalmedia.co.uk

---

Next page: https://externals.io/message/87917.md?page=2
