Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #12878 > unrolled thread

Declaring an array necessary?

Started byThe Natural Philosopher <tnp@invalid.invalid>
First post2014-02-21 17:56 +0000
Last post2014-02-21 14:03 -0800
Articles 8 — 7 participants

Back to article view | Back to comp.lang.php


Contents

  Declaring an array necessary? The Natural Philosopher <tnp@invalid.invalid> - 2014-02-21 17:56 +0000
    Re: Declaring an array necessary? Christian Stadler <stadli@gmx.de> - 2014-02-21 21:04 +0100
      Re: Declaring an array necessary? Jerry Stuckle <jstucklex@attglobal.net> - 2014-02-21 15:48 -0500
      Re: Declaring an array necessary? The Natural Philosopher <tnp@invalid.invalid> - 2014-02-21 21:32 +0000
        Re: Declaring an array necessary? Gregor Kofler <usenet@gregorkofler.com> - 2014-02-21 22:55 +0100
          Re: Declaring an array necessary? Tim Streater <timstreater@greenbee.net> - 2014-02-21 22:06 +0000
    Re: Declaring an array necessary? "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2014-02-21 22:58 +0100
    Re: Declaring an array necessary? Daniel Pitts <newsgroup.nospam@virtualinfinity.net> - 2014-02-21 14:03 -0800

#12878 — Declaring an array necessary?

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-02-21 17:56 +0000
SubjectDeclaring an array necessary?
Message-ID<le840m$8kk$1@news.albasani.net>
I was just wondering. Php assumes a varibale exists as soon as it is 
assigned a value, but a function exists to create an array..
Sop can one just say

$arr['hello']='sexy';

or should there be

$arr=array();

first?


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [next] | [standalone]


#12879

FromChristian Stadler <stadli@gmx.de>
Date2014-02-21 21:04 +0100
Message-ID<5307b154$0$9525$9b4e6d93@newsspool1.arcor-online.net>
In reply to#12878
Am 21.02.2014 18:56, schrieb The Natural Philosopher:
> I was just wondering. Php assumes a varibale exists as soon as it is 
> assigned a value, but a function exists to create an array..
> Sop can one just say
> 
> $arr['hello']='sexy';
> 
> or should there be
> 
> $arr=array();
> 
> first?

Its generally a good idea to initialize a variable, before you change
its contents, unless you are 100% sure, that the variable hasn't been
initialized elsewhere or probably through whatever exploit and so on.
But honestly: Can you be 100% sure? ;-)

Another (quick and dirty) example would be :

| $tablecontents = '';
| foreach ($foo AS $bar => $moo) {
|   $tablecontents .= "\t<tr><td>$bar</td><td>$moo</td></tr>\n";
| }
| echo "<table>\n$tablecontents</table>\n";

If you leave $tablecontents = ''; away, this could result in unwanted
behaviour up to something even worse, like XSS-attacks.

PS: If someone can explain this in a better way, feel free to do so. But
please don't tell me, whats wrong in my post. Its just a quick and dirty
example and probably not the best way to explain this. But right now,
I'm not in the mood to recherche for hours and hours to ensure, that
every single letter I type in a *.c.l.php-group is 100% correct ...

Regards,
  Christian Stadler

[toc] | [prev] | [next] | [standalone]


#12880

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-02-21 15:48 -0500
Message-ID<le8e2d$m9r$1@dont-email.me>
In reply to#12879
On 2/21/2014 3:04 PM, Christian Stadler wrote:
> Am 21.02.2014 18:56, schrieb The Natural Philosopher:
>> I was just wondering. Php assumes a varibale exists as soon as it is
>> assigned a value, but a function exists to create an array..
>> Sop can one just say
>>
>> $arr['hello']='sexy';
>>
>> or should there be
>>
>> $arr=array();
>>
>> first?
>
> Its generally a good idea to initialize a variable, before you change
> its contents, unless you are 100% sure, that the variable hasn't been
> initialized elsewhere or probably through whatever exploit and so on.
> But honestly: Can you be 100% sure? ;-)
>
> Another (quick and dirty) example would be :
>
> | $tablecontents = '';
> | foreach ($foo AS $bar => $moo) {
> |   $tablecontents .= "\t<tr><td>$bar</td><td>$moo</td></tr>\n";
> | }
> | echo "<table>\n$tablecontents</table>\n";
>
> If you leave $tablecontents = ''; away, this could result in unwanted
> behaviour up to something even worse, like XSS-attacks.
>
> PS: If someone can explain this in a better way, feel free to do so. But
> please don't tell me, whats wrong in my post. Its just a quick and dirty
> example and probably not the best way to explain this. But right now,
> I'm not in the mood to recherche for hours and hours to ensure, that
> every single letter I type in a *.c.l.php-group is 100% correct ...
>
> Regards,
>    Christian Stadler
>

Christian,

Nope, nothing wrong with your post.  And I agree - it is almost always a 
good idea to initialize a variable before using it.

How many times have you seen in this group NOTIFY messages being turned 
off and/or ignored - then people complaining because the code doesn't 
work as expected?

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#12882

FromThe Natural Philosopher <tnp@invalid.invalid>
Date2014-02-21 21:32 +0000
Message-ID<le8gkj$41m$1@news.albasani.net>
In reply to#12879
On 21/02/14 20:04, Christian Stadler wrote:
> Am 21.02.2014 18:56, schrieb The Natural Philosopher:
>> I was just wondering. Php assumes a varibale exists as soon as it is
>> assigned a value, but a function exists to create an array..
>> Sop can one just say
>>
>> $arr['hello']='sexy';
>>
>> or should there be
>>
>> $arr=array();
>>
>> first?
>
> Its generally a good idea to initialize a variable, before you change
> its contents, unless you are 100% sure, that the variable hasn't been
> initialized elsewhere or probably through whatever exploit and so on.
> But honestly: Can you be 100% sure? ;-)
>
> Another (quick and dirty) example would be :
>
> | $tablecontents = '';
> | foreach ($foo AS $bar => $moo) {
> |   $tablecontents .= "\t<tr><td>$bar</td><td>$moo</td></tr>\n";
> | }
> | echo "<table>\n$tablecontents</table>\n";
>
> If you leave $tablecontents = ''; away, this could result in unwanted
> behaviour up to something even worse, like XSS-attacks.
>
> PS: If someone can explain this in a better way, feel free to do so. But
> please don't tell me, whats wrong in my post. Its just a quick and dirty
> example and probably not the best way to explain this. But right now,
> I'm not in the mood to recherche for hours and hours to ensure, that
> every single letter I type in a *.c.l.php-group is 100% correct ...
>
> Regards,
>    Christian Stadler
>
No that was a fine reply all in all.

Got the message of what you are trying to say - doesn't apply here 
though: the data going into the array is all guaranteed clean.


-- 
Ineptocracy

(in-ep-toc’-ra-cy) – a system of government where the least capable to 
lead are elected by the least capable of producing, and where the 
members of society least likely to sustain themselves or succeed, are 
rewarded with goods and services paid for by the confiscated wealth of a 
diminishing number of producers.

[toc] | [prev] | [next] | [standalone]


#12883

FromGregor Kofler <usenet@gregorkofler.com>
Date2014-02-21 22:55 +0100
Message-ID<le8i13$er0$1@dont-email.me>
In reply to#12882
Am 21.02.2014 22:32, The Natural Philosopher meinte:
> On 21/02/14 20:04, Christian Stadler wrote:
>> Am 21.02.2014 18:56, schrieb The Natural Philosopher:
>>> I was just wondering. Php assumes a varibale exists as soon as it is
>>> assigned a value, but a function exists to create an array..
>>> Sop can one just say
>>>
>>> $arr['hello']='sexy';
>>>
>>> or should there be
>>>
>>> $arr=array();
>>>
>>> first?
>>
>> Its generally a good idea to initialize a variable, before you change
>> its contents, unless you are 100% sure, that the variable hasn't been
>> initialized elsewhere or probably through whatever exploit and so on.
>> But honestly: Can you be 100% sure? ;-)
>>
>> Another (quick and dirty) example would be :
>>
>> | $tablecontents = '';
>> | foreach ($foo AS $bar => $moo) {
>> |   $tablecontents .= "\t<tr><td>$bar</td><td>$moo</td></tr>\n";
>> | }
>> | echo "<table>\n$tablecontents</table>\n";
>>
>> If you leave $tablecontents = ''; away, this could result in unwanted
>> behaviour up to something even worse, like XSS-attacks.
>>
>> PS: If someone can explain this in a better way, feel free to do so. But
>> please don't tell me, whats wrong in my post. Its just a quick and dirty
>> example and probably not the best way to explain this. But right now,
>> I'm not in the mood to recherche for hours and hours to ensure, that
>> every single letter I type in a *.c.l.php-group is 100% correct ...
>>
>> Regards,
>>    Christian Stadler
>>
> No that was a fine reply all in all.
>
> Got the message of what you are trying to say - doesn't apply here
> though: the data going into the array is all guaranteed clean.

RTFM.

http://www.php.net/manual/de/language.types.array.php

"Creating/modifying with square bracket syntax"

should answer your question. However, I still prefer to initialize an 
array explicitly. And if it is only for better readable code.

Gregor

[toc] | [prev] | [next] | [standalone]


#12886

FromTim Streater <timstreater@greenbee.net>
Date2014-02-21 22:06 +0000
Message-ID<210220142206045522%timstreater@greenbee.net>
In reply to#12883
In article <le8i13$er0$1@dont-email.me>, Gregor Kofler
<usenet@gregorkofler.com> wrote:

> Am 21.02.2014 22:32, The Natural Philosopher meinte:
> > On 21/02/14 20:04, Christian Stadler wrote:
> >> Am 21.02.2014 18:56, schrieb The Natural Philosopher:
> >>> I was just wondering. Php assumes a varibale exists as soon as it is
> >>> assigned a value, but a function exists to create an array..
> >>> Sop can one just say
> >>>
> >>> $arr['hello']='sexy';
> >>>
> >>> or should there be
> >>>
> >>> $arr=array();
> >>>
> >>> first?

> RTFM.
> 
> http://www.php.net/manual/de/language.types.array.php

Warning: that page has a Warnung on it.

> "Creating/modifying with square bracket syntax"
> 
> should answer your question. However, I still prefer to initialize an 
> array explicitly. And if it is only for better readable code.

I declare them explicitly and then initialise them later. I also agree
that this makes it more readable.

-- 
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted"  --  Bill of Rights 1689

[toc] | [prev] | [next] | [standalone]


#12884

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2014-02-21 22:58 +0100
Message-ID<bmq0gtF5t8gU1@mid.uni-berlin.de>
In reply to#12878
Am 21.02.2014 18:56, schrieb The Natural Philosopher:
> I was just wondering. Php assumes a varibale exists as soon as it is assigned a
> value, but a function exists to create an array..

I would rather say: PHP creates a variable as soon as you assign a value to one that
does not exist.

> Sop can one just say
> 
> $arr['hello']='sexy';
> 
> or should there be
> 
> $arr=array();
> 
> first?
> 

I would do a
	$arr = array()
before I start to store data from the database into it, because a query could give
zero records, and I don't want my script killed on the next foreach.

There is redundancy in many programming languages, in the sense that you can get the
same result with several different expressions. So I would not worry too much about
why array() exists.


/Str.

[toc] | [prev] | [next] | [standalone]


#12885

FromDaniel Pitts <newsgroup.nospam@virtualinfinity.net>
Date2014-02-21 14:03 -0800
Message-ID<73QNu.7865$Z04.5437@fx27.iad>
In reply to#12878
On 2/21/14 9:56 AM, The Natural Philosopher wrote:
> I was just wondering. Php assumes a varibale exists as soon as it is
> assigned a value, but a function exists to create an array..
> Sop can one just say
>
> $arr['hello']='sexy';
>
> or should there be
>
> $arr=array();
>
> first?
>
>

Necessary, no, but it is still a best practice.  It helps with 
maintenance later, as well as it clearly states your intent.

Imagine this scenario: You start with

function createMyArray($someData) {

   $arr['theData'] = $someData->getX();
   $arr['theOtherData'] = $someData->getY();

   return $arr;
}


A few weeks later, you get a bug report that you need to only sometimes 
add getX().

function createMyArray($someData) {

   if ($someData->isFoo()) {
     $arr['theData'] = $someData->getX();
   }
   $arr['theOtherData'] = $someData->getY();

   return $arr;
}

Then, you (or someone else) finds out getY() is optional...


function createMyArray($someData) {

   if ($someData->isFoo()) {
     $arr['theData'] = $someData->getX();
   }
   if ($someData->isBar()) {
     $arr['theOtherData'] = $someData->getY();
   }

   return $arr;
}


Oh no! You've just introduced a bug where if isFoo and isBar returns 
true, then $arr is not set!


Now, if the original author had just added "$arr = []"; things would 
have never progressed to this state.  It's called defensive programming. 
Doing certain things you know aren't strictly necessary now, but might 
be necessary, or clear up possible confusion.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web