Declaring an array necessary? [message #184969] |
Fri, 21 February 2014 17:56 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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.
|
|
|
Re: Declaring an array necessary? [message #184970 is a reply to message #184969] |
Fri, 21 February 2014 20:04 |
Christian Stadler
Messages: 2 Registered: February 2014
Karma: 0
|
Junior Member |
|
|
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
|
|
|
Re: Declaring an array necessary? [message #184971 is a reply to message #184970] |
Fri, 21 February 2014 20:48 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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(at)attglobal(dot)net
==================
|
|
|
Re: Declaring an array necessary? [message #184972 is a reply to message #184970] |
Fri, 21 February 2014 21:32 |
The Natural Philosoph
Messages: 993 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
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.
|
|
|
Re: Declaring an array necessary? [message #184973 is a reply to message #184972] |
Fri, 21 February 2014 21:55 |
Gregor Kofler
Messages: 69 Registered: September 2010
Karma: 0
|
Member |
|
|
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
|
|
|
Re: Declaring an array necessary? [message #184974 is a reply to message #184969] |
Fri, 21 February 2014 21:58 |
M. Strobel
Messages: 386 Registered: December 2011
Karma: 0
|
Senior Member |
|
|
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.
|
|
|
Re: Declaring an array necessary? [message #184975 is a reply to message #184969] |
Fri, 21 February 2014 22:03 |
Daniel Pitts
Messages: 68 Registered: May 2012
Karma: 0
|
Member |
|
|
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.
|
|
|
Re: Declaring an array necessary? [message #184976 is a reply to message #184973] |
Fri, 21 February 2014 22:06 |
Tim Streater
Messages: 328 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
In article <le8i13$er0$1(at)dont-email(dot)me>, Gregor Kofler
<usenet(at)gregorkofler(dot)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
|
|
|