TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Assigning variables on one line (http://www.talkphp.com/general/2493-assigning-variables-one-line.html)

Orc 03-19-2008 03:12 PM

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.

dschreck 03-19-2008 03:21 PM

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.

Orc 03-19-2008 03:23 PM

Quote:

Originally Posted by dschreck (Post 12532)
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.

Orc 03-19-2008 03:31 PM

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.

dschreck 03-19-2008 03:55 PM

Quote:

Originally Posted by Orc (Post 12537)
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

Orc 03-19-2008 03:57 PM

Quote:

Originally Posted by dschreck (Post 12543)
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'


Wildhoney 03-19-2008 07:26 PM

How about this?

php Code:
list($foo, $bar) = array('foo', 'bar');

Orc 03-19-2008 07:27 PM

Quote:

Originally Posted by Wildhoney (Post 12549)
How about this?

php Code:
list($foo, $bar) = array('foo', 'bar');

well that was my first thought.

dschreck 03-19-2008 08:13 PM

Quote:

Originally Posted by Wildhoney (Post 12549)
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

Salathe 03-19-2008 08:31 PM

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); 


Orc 03-19-2008 08:33 PM

Quote:

Originally Posted by Salathe (Post 12553)
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

Gibou 03-19-2008 11:57 PM

Mmmm, can I know in which use case you need to do that ??

TlcAndres 03-20-2008 12:46 AM

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.


All times are GMT. The time now is 11:56 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0