Declaring multiple variables simultaneously

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Paulo Renato
Forum Newbie
Posts: 2
Joined: Fri Mar 06, 2009 2:06 pm

Declaring multiple variables simultaneously

Post 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. :)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Declaring multiple variables simultaneously

Post by Benjamin »

You can do this:

Code: Select all

 
list($variable1, $variable2) = array('value1', 'value2');
 
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Declaring multiple variables simultaneously

Post by Mark Baker »

Or if you wanted to set them to the same value, you could use:

Code: Select all

$variable1 = $variable2 = 'initial value';
Paulo Renato
Forum Newbie
Posts: 2
Joined: Fri Mar 06, 2009 2:06 pm

Re: Declaring multiple variables simultaneously

Post 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:
Post Reply