 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
 |
|
 |
03-19-2008, 03:12 PM
|
#1 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Assigning variables on one line
How would I do a multiple variable assigning?
example
PHP Code:
$foo = 'bar' && $bar = 'foo';
Update: Well my true use would be to assign $foo and $bar to the same value without more lines. Only on one line and clustered together using a comma or whatever.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 03:21 PM
|
#2 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
I'm not entirely sure myself...
I can't really see why you wouldnt just want to..
PHP Code:
$foo = 'bar'; $bar = 'foo';
White space doesn't matter, you just need to be sure you end your statement.
|
|
|
|
03-19-2008, 03:23 PM
|
#3 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by dschreck
I'm not entirely sure myself...
I can't really see why you wouldnt just want to..
PHP Code:
$foo = 'bar'; $bar = 'foo';
White space doesn't matter, you just need to be sure you end your statement.
|
I understand that method, :] But I just want to do it in a cluster.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 03:31 PM
|
#4 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
I figured out a way for it to work:
PHP Code:
<?php $foo = $bar = 'foo';
?>
But this isnt two separate values, this is just clustering $foo into $bar which assigns to $foo but umm, I still want to do it clustered multipled variables, with different values.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 03:55 PM
|
#5 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
Quote:
Originally Posted by Orc
I figured out a way for it to work:
PHP Code:
<?php
$foo = $bar = 'foo';
?>
But this isnt two separate values, this is just clustering $foo into $bar which assigns to $foo but umm, I still want to do it clustered multipled variables, with different values.
|
Yeah, I knew you could do that, I just didn't think you wanted to assign the same value to more than one variable.
;p
|
|
|
|
03-19-2008, 03:57 PM
|
#6 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by dschreck
Yeah, I knew you could do that, I just didn't think you wanted to assign the same value to more than one variable.
;p
|
Ah well.. I will just do the original method:
PHP Code:
$foo = 'foo';
$bar = 'bar';
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 07:26 PM
|
#7 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
How about this?
php Code:
list($foo, $bar) = array('foo', 'bar');
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
|
|
|
03-19-2008, 07:27 PM
|
#8 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Wildhoney
How about this?
php Code:
list($foo, $bar) = array('foo', 'bar');
|
well that was my first thought.
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 08:13 PM
|
#9 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
|
Quote:
Originally Posted by Wildhoney
How about this?
php Code:
list($foo, $bar) = array('foo', 'bar');
|
yeah, that would work. you could also just load an array up:
PHP Code:
$myVars = array(
'foo' => 'bar',
'bar' => 'foo'
);
foreach($myVars as $key => $val) {
${$key} = $val;
}
if you just want to do a crap load of vars
|
|
|
|
|
The Following User Says Thank You to dschreck For This Useful Post:
|
|
03-19-2008, 08:31 PM
|
#10 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
If you were going to use an associative array, then extract() would be better than the foreach loop. Though, I'm still struggling to see the purpose here. Why not just assign the values 'normally'... keep it simple.
Extract
PHP Code:
$vars = array ( 'foo' => 'bar', 'baz' => 'foo' ); extract($vars); var_dump($foo, $baz);
|
|
|
|
03-19-2008, 08:33 PM
|
#11 (permalink)
|
|
The Prestige
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
|
Quote:
Originally Posted by Salathe
If you were going to use an associative array, then extract() would be better than the foreach loop. Though, I'm still struggling to see the purpose here. Why not just assign the values 'normally'... keep it simple.
Extract
PHP Code:
$vars = array
(
'foo' => 'bar',
'baz' => 'foo'
);
extract($vars);
var_dump($foo, $baz);
|
Less lines of code? :P
__________________
VillageIdiot can have my babbies ;d
|
|
|
|
03-19-2008, 11:57 PM
|
#12 (permalink)
|
|
The Contributor
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
|
Mmmm, can I know in which use case you need to do that ??
|
|
|
03-20-2008, 12:46 AM
|
#13 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 264
Thanks: 2
|
The speed boost gained by less line of code is insignificant as best, readability of your code also goes down loads - it looks nice and obfuscated but keep it simple.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
|
|
|
|
|
The Following User Says Thank You to TlcAndres For This Useful Post:
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|