NYCPHP Meetup

NYPHP.org

[nycphp-talk] Accessing non-existent array elements ok?

Kristina Anderson ka at kacomputerconsulting.com
Sun Aug 17 18:30:16 EDT 2008


Ah... OK.  so the end result of this is that $result will be either the 
value of $a['c'] -- if $a['c'] is set, or NULL -- if $a['c'] is NOT 
set.  But in either case, it does not change the value of $a['c'], only 
sets the value of $result.

I feel like going back and taking another look at that code with new 
eyes, now.

Thanks.  -- Kristina



> Kristina,
> 
> That code is not testing two conditions. It's testing one condition
> and assigning one value to $result if $a['c'] is set or another value
> to $result if $a['c'] is not set. It is the shorthand equivalent to
> writing
> 
> 
> if (isset($a['c'])
> {
>     $result = $a['c] ;
> }
> else
> {
>      $result = NULL;
> }
> 
> . Both do the same thing, but one is a lot faster to write. So you can
> either write all that out or just
>  $result = isset($a['c']) ? $a['c'] : NULL;.
> 
> I hope that clarifies things.
> 
> On Sun, Aug 17, 2008 at 6:10 PM, Kristina Anderson
> <ka at kacomputerconsulting.com> wrote:
> > Hi all.  I have a question regarding the below syntax
> >
> > $result = isset($a['c']) ? $a['c'] : NULL;
> >
> >
> > This is testing two conditions, first if $a['c'] is set, and then, 
if $a
> > ['c'] is NULL...correct?
> >
> > What would $result be if $a['c'] is set but is not NULL?  The same 
as
> > if $a['c'] was not set?  or...?
> >
> > I've seen similar syntax before, in some code Tedd & I were working
> > with a couple of months back...and just want to make sure I fully
> > understand what is actually happening with it.
> >
> > Thanks.
> >
> > -- Kristina
> >
> >> On Sun, Aug 17, 2008 at 4:49 PM, Michael B Allen <ioplex at gmail.com>
> > wrote:
> >> > Is it ok to access non-existent array elements?
> >> >
> >> > Will accessing an array element using a key that does not exist
> > always
> >> > return NULL?
> >>
> >> it depends on how you have warnings configured. you might be 
getting
> >> them in your log, for example... i believe the result of accessing 
a
> >> non-existent array element is undefined.
> >>
> >> if i were you, i'd prob'ly ask if it exists first:
> >>
> >> <?php
> >>
> >> $result = isset($a['c']) ? $a['c'] : NULL;
> >>
> >> /** OR **/
> >>
> >> $result = array_key_exists('c', $a) ? $a['c'] : NULL;
> >>
> >> ?>
> >>
> >> see:
> >>
> >> http://us2.php.net/function.isset
> >> http://us2.php.net/function.array-key-exists
> >>
> >>
> >> justin
> >> --
> >> http://justinhileman.com
> >> _______________________________________________
> >> New York PHP Community Talk Mailing List
> >> http://lists.nyphp.org/mailman/listinfo/talk
> >>
> >> NYPHPCon 2006 Presentations Online
> >> http://www.nyphpcon.com
> >>
> >> Show Your Participation in New York PHP
> >> http://www.nyphp.org/show_participation.php
> >>
> >>
> >
> >
> > _______________________________________________
> > New York PHP Community Talk Mailing List
> > http://lists.nyphp.org/mailman/listinfo/talk
> >
> > NYPHPCon 2006 Presentations Online
> > http://www.nyphpcon.com
> >
> > Show Your Participation in New York PHP
> > http://www.nyphp.org/show_participation.php
> >
> _______________________________________________
> New York PHP Community Talk Mailing List
> http://lists.nyphp.org/mailman/listinfo/talk
> 
> NYPHPCon 2006 Presentations Online
> http://www.nyphpcon.com
> 
> Show Your Participation in New York PHP
> http://www.nyphp.org/show_participation.php
> 
> 




More information about the talk mailing list