Forum Moderators: coopster

Message Too Old, No Replies

How do I give all variables the same value?

         

The Cricketer

1:45 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



To begin with in my script there is a set of variables which all have the same value - which is nothing.

$dog = '';
$cat = '';
$sheep = '';
$monkey = '';
$man = '';

The variable will be given values further on in the script. Is there a shorter way to code the above?

Cheers

mincklerstraat

2:10 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I usually break with coding conventions on this kind of thing and put all null variable initializations together in a single line:

$dog=''; $cat=''; $monkey=''; $i=''; $u=''; $weall='';

(sounds sort of like some kind of Bhuddist poem, no?)

You could also make an array of strings, loop through and set the variable named that string to '', but that would be just a tiny bit less efficient php-wise, and 'adding' variables to the list not really any easier, since it's so easy to add variables to a line of variables like this.

I like the one-line approach since I really like as much code as I can have on my screen (providing it's legible), and it's just so obvious what this line is doing that putting it into multiple lines doesn't help legibility, but takes up screenspace. Yeah, code folding is supposed to help with this kind of thing, but I've never picked up that habit, it tends to confuse me more when I'm trying to work fast.

Slade

2:32 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



$v1 = $v2 = '';

mincklerstraat

2:36 pm on Nov 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Slade: that's slick! and does the trick.

The Cricketer

3:21 pm on Nov 18, 2004 (gmt 0)

10+ Year Member



Thanks very much folks. Always appreciated.

mattx17

9:23 pm on Nov 19, 2004 (gmt 0)

10+ Year Member



You can also use unset, it allows you to specify as many variables as you want.

unset($foo,$bar,$i);