PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old August 5th, 2004, 02:07 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
Reassigning variables?

$color = green;
$str = "My car is " . $color;

if ($color == red) { $final = $str; } else { $color = blue; $final = $str; }

echo $final;

I know the above doesn't work but in my else statement I'm trying to reassign the variable for $color. So that if the $color value coming in is "red" the echo statement will read "My car is red". If the $color value coming in is anything other than red, say "green", then the echo statement should be "My car is green"

Only I can't get it to work. Any suggestions?

Reply With Quote
  #2  
Old August 5th, 2004, 02:48 PM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
PHP Code:
 $color="green";

echo 
"My car is " $color


There are a couple of fundamentals you've missed. First, string values have to be quoted, so where you make reference to green, red, and blue without quoting them, you're not going to get accurate values. Second, once you've set one variable using another, going back and changing the other, nested, variable doesn't change the first. You need to determine the nested variable's value before using it to build the nesting variable (in this case $str). And third, you've got more code than is necessary. When a variable is used (and is likely ever to be used) only once, it's usually better not to set it as a variable, at least in a case such as this one. Also, there's no need to have an if block because you can just echo the value of $color rather than testing it, setting a variable, and then echoing the variable. Even if you want to set the $str variable, the if block is superfluous.
__________________
Please don't PM me asking for solutions outside the scope of a thread.
Keeping all responses in a thread stands to help others who come along later,
which is after all what this forum's all about.

Reply With Quote
  #3  
Old August 5th, 2004, 05:38 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
Well, I appreciate the input but I'm really looking for a way to do it. As in, it'd be nice to see some code that accomplishes what I hoped I explained: I need to test a variable coming in and then I want to reuse a single string over and over again by substituting in the correct variable values. I know my code is incorrect, I'm hoping someone can show me how to write the correct code.

Reply With Quote
  #4  
Old August 6th, 2004, 02:28 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
This is a bit taking the long way home, but you might try this, if you know the possible colors beforehand..

PHP Code:
 $green "My car is green";
$red "My car is red";
$blue "My car is blue";

$color green#or some other color of course

if ($color == red) { $final = $$color; } else { $color blue$final = $$color; }

echo 
$final


Still, the effect is the same as dhouston's idea.
I simply think that what you want isn't really possible, once you've made a string out of other strings, changing those other strings won't change the created string. The only way to do that is with references, but then you can't attach the "My car is: " part.
The code could look like something like this:
PHP Code:
 $color "green"
$str "My car is ";
$str2 = &$color;

if (
$color == "red") { $final $str.$str2; } else { $color "blue"$final $str.$str2; }

echo 
$final


You won't get any better than that, I'm afraid.
Still, the whole exercise seems a bit pointless to me, could you explain what you need it for, we might be able to come up with an overal better solution.

Reply With Quote
  #5  
Old August 6th, 2004, 07:20 AM
dhouston's Avatar
dhouston dhouston is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: May 2003
Location: Tennessee
Posts: 1,355 dhouston User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 7
Send a message via ICQ to dhouston
Actually, now that I think about it, what you're wanting to do is possible. I'm just not sure it makes sense to do it this way (makes for confusing code that's tricky to debug later). Check this out:

PHP Code:
 $var="one";
$var2 =& $var//Note the ampersand. We're assigning to $var2 a reference to $var.
                                                                                                                                                   
print "Var should be one: " $var2 "\n\n";
                                                                                                                                                   
$var="two"//Note that we're setting $var and not $var2...
                                                                                                                                                   
print "Var should be two: " $var2 "\n\n"//But we're printing $var2. 


If we make $var2 a reference to $var, then changing $var changes the value $var2 displays. This allows us to always use $var2 in our strings while changing $var. Again, I'm not sure it makes sense to do this because it's not very straightforward and is ultimately going to cost somebody a half hour of tracing variables to figure out how the value of $var2 is being changed when there's no code actually changing it.

Reply With Quote
  #6  
Old August 6th, 2004, 07:47 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
That's pretty much what I said, but I think what nemozob wants is a reference within a string.

syntax-wise, it'd look like this:
PHP Code:
 $color "green";
$str "My car is: ".&$color

unfortunately, that's not valid PHP.

Reply With Quote
  #7  
Old August 6th, 2004, 01:36 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
Thanks for input. I really appreciate it. But I think I didn't explain what I was looking for well enough, or maybe didn't ask the right question. The situation is probably better described like this:

3 cars are in a race: Honda, Toyota, Volvo and associated with each variable is one of two states, either 'winner' or 'loser'

So I want to spit out the $car and $state to HTML:

This will spit out the cars but obviously not their states:

$cars = array('Honda', 'Toyota', 'Volvo');
foreach ($cars as $car) {
echo "$car is the ;
}

But let's say the $winner comes in as Honda. How do I change the $state variable (without using a long series of if statements, because my actual arrays are much longer)? So that the final output is:

Honda is the winner
Toyota is the loser
Volvo is the loser

Reply With Quote
  #8  
Old August 6th, 2004, 02:06 PM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 5
Rather than giving different examples post us your code it'll make things far easier to sort out for you. That way we can give you a solution or suggest a 'next best' work around if not.

-KM-

Reply With Quote
  #9  
Old August 6th, 2004, 03:55 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
If that's what you want, I think my first suggestion might actually be of use here:

PHP Code:
 $Honda "winner";
$Toyota "loser";
$Volvo "loser";

$cars = array('Honda''Toyota''Volvo');
foreach (
$cars as $car) {
echo 
$car." is the ".$$car;



This should give the desired result.
Just assign the results to variables with the names values of the array (yeah, that's a horrible sentence) and you'd be fine.

You can even assign them with the foreach statement, if the array is variable.

PS: remember that variable names are case-sensitive.

Reply With Quote
  #10  
Old August 6th, 2004, 07:34 PM
Nemozob Nemozob is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 15 Nemozob User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 m 14 sec
Reputation Power: 0
PHP Code:
 $Honda "winner";
$Toyota "loser";
$Volvo "loser";

$cars = array('Honda''Toyota''Volvo');
foreach (
$cars as $car) {
echo 
$car." is the ".$$car;



This is what I was looking for...I adpated it to what's below. The $var is coming into the script so I can't preset any of the car variables. This will work, but this there a shorter way to do this?

[PHP]
$var = "a";

if ($var == "a") {$Honda = "winner";} else $Honda = "loser";
if ($var == "b") {$Toyota = "winner";} else $Toyota = "loser";
if ($var == "c") {$Volvo = "winner";} else $Volvo = "loser";

$cars = array('Honda', 'Toyota', 'Volvo');
foreach ($cars as $car) {
echo $car." is the ".$$car;
}
[PHP]

Thanks again for all the help!

Reply With Quote
  #11  
Old August 7th, 2004, 02:22 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 956 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2Folding Points: 650865 Folding Title: Super Ultimate Folder - Level 2
Time spent in forums: 6 Days 8 h 23 m 32 sec
Reputation Power: 4
Send a message via ICQ to Itsacon
Well, if it is possible to change the incoming $var into a numeric value, you might want to do this (Now $var is number of the car that won, instead of an alphabetic 'number')
PHP Code:
 $var "1";

$cars = array('Honda''Toyota''Volvo'); 

foreach (
$cars as $car)
    $
$car "loser"
${
$cars[$var-1]} = "winner";

foreach (
$cars as $car
    echo 
$car." is the ".$$car


It is now independent of the number of cars in the array. $cars can (and should) be defined elsewhere, and also be used in the function that determines the winner (and sends on $var to this function).

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingPHP Development > Reassigning variables?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump