Page 1 of 1

Declaring multiple variables simultaneously

Posted: Fri Mar 06, 2009 2:23 pm
by Paulo Renato
i'm migrating from PERL to PHP today, so i'm new at this. I've searched all over and didn't find an answer.

I would like to declare two variables simultaneously. In PERL we do like this:

Code: Select all

($variable1, $variable2) = ("First Value", "Second Value");
It would be the same as:

Code: Select all

$variable1 = "First Value";
$variable2 = "Second Value";
But i'm having a hard time reproducing it in PHP. Is it possible? Thanks in advance. :)

Re: Declaring multiple variables simultaneously

Posted: Fri Mar 06, 2009 2:43 pm
by Benjamin
You can do this:

Code: Select all

 
list($variable1, $variable2) = array('value1', 'value2');
 

Re: Declaring multiple variables simultaneously

Posted: Fri Mar 06, 2009 3:02 pm
by Mark Baker
Or if you wanted to set them to the same value, you could use:

Code: Select all

$variable1 = $variable2 = 'initial value';

Re: Declaring multiple variables simultaneously

Posted: Fri Mar 06, 2009 3:49 pm
by Paulo Renato
@astions
Thanks, that did the job. ^^
Didn't know it would need a function to accomplish that. /o\

@Mark Baker
Thanks also. That's not what i had in mind, but certainly will be useful in the future. Knowledge is never too much. :wink: