FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Declaring an array necessary?
Show: Today's Messages :: Unread Messages :: Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Declaring an array necessary? [message #184969] Fri, 21 February 2014 12:56 Go to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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 15:04 Go to previous messageGo to next message
Christian Stadler is currently offline  Christian Stadler
Messages: 2
Registered: February 2014
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
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
Message by Jerry Stuckle is ignored  [reveal message]  [reveal all messages by Jerry Stuckle]  [stop ignoring this user] Go to previous messageGo to next message
Re: Declaring an array necessary? [message #184972 is a reply to message #184970] Fri, 21 February 2014 16:32 Go to previous messageGo to next message
The Natural Philosoph is currently offline  The Natural Philosoph
Messages: 993
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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 16:55 Go to previous messageGo to next message
Gregor Kofler is currently offline  Gregor Kofler
Messages: 69
Registered: September 2010
Karma: 0
Member
add to buddy list
ignore all messages by this user
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 16:58 Go to previous messageGo to next message
M. Strobel is currently offline  M. Strobel
Messages: 386
Registered: December 2011
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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.
Message by Daniel Pitts is ignored  [reveal message]  [reveal all messages by Daniel Pitts]  [stop ignoring this user] Go to previous messageGo to next message
Re: Declaring an array necessary? [message #184976 is a reply to message #184973] Fri, 21 February 2014 17:06 Go to previous message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
add to buddy list
ignore all messages by this user
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
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: strpos() before str_replace()? Or, maybe strtr()?
Next Topic: Career Opportunities in Singapore (PHP Tech Lead)
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri May 24 12:16:37 EDT 2024

Total time taken to generate the page: 0.05208 seconds