|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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? |
|
#2
|
||||
|
||||
|
PHP Code:
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. |
|
#3
|
|||
|
|||
|
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.
|
|
#4
|
||||
|
||||
|
This is a bit taking the long way home, but you might try this, if you know the possible colors beforehand..
PHP Code:
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:
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. |
|
#5
|
||||
|
||||
|
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:
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. |
|
#6
|
||||
|
||||
|
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:
unfortunately, that's not valid PHP. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
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- |
|
#9
|
||||
|
||||
|
If that's what you want, I think my first suggestion might actually be of use here:
PHP Code:
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. |
|
#10
|
|||
|
|||
|
PHP Code:
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! |
|
#11
|
||||
|
||||
|
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:
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). |
![]() |
| Viewing: Dev Articles Community Forums > Programming > PHP Development > Reassigning variables? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|