TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 03-19-2008, 03:12 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default 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
Orc is offline  
Reply With Quote
Old 03-19-2008, 03:21 PM   #2 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

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.
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 03-19-2008, 03:23 PM   #3 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by dschreck View Post
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
Orc is offline  
Reply With Quote
Old 03-19-2008, 03:31 PM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

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
Orc is offline  
Reply With Quote
Old 03-19-2008, 03:55 PM   #5 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Quote:
Originally Posted by Orc View Post
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
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
Old 03-19-2008, 03:57 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by dschreck View Post
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
Orc is offline  
Reply With Quote
Old 03-19-2008, 07:26 PM   #7 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

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.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 03-19-2008, 07:27 PM   #8 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
How about this?

php Code:
list($foo, $bar) = array('foo', 'bar');
well that was my first thought.
__________________
VillageIdiot can have my babbies ;d
Orc is offline  
Reply With Quote
Old 03-19-2008, 08:13 PM   #9 (permalink)
The Contributor
 
dschreck's Avatar
 
Join Date: Nov 2007
Location: California
Posts: 82
Thanks: 0
dschreck is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
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
__________________
Where I Ramble: http://www.iwilldomybest.com/
What I do: Zynga Game Network
Senior Software Engineer at CityVille
dschreck is offline  
Reply With Quote
The Following User Says Thank You to dschreck For This Useful Post:
Orc (03-19-2008)
Old 03-19-2008, 08:31 PM   #10 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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); 
Salathe is offline  
Reply With Quote
Old 03-19-2008, 08:33 PM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
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
Orc is offline  
Reply With Quote
Old 03-19-2008, 11:57 PM   #12 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

Mmmm, can I know in which use case you need to do that ??
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
Old 03-20-2008, 12:46 AM   #13 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

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.
TlcAndres is offline  
Reply With Quote
The Following User Says Thank You to TlcAndres For This Useful Post:
Orc (03-20-2008)
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design