phpnoob808 Posted September 14, 2013 Share Posted September 14, 2013 Hi Folks! I'm new to the forums and the PHP language. This will be my new home for my programming career, so I hope to learn a lot here. Anyway, I have a head scratcher. $a = <YOUR FIRST NAME>; $b = <YOUR LAST NAME>; $a = $b; $b = $a; echo 'Name (last, first)'.$a.','.$b; The above program tries to swap the values assigned to the variables $a and $b in order to display your name in the format <last>, <first>. It will not work! What’s wrong with the above program and how can you fix it (without changing the first or last lines)? How can you fix this if you are allowed to change any of the lines? Why might be a problem with doing it this way? I've done the research and can't seem to find out why the output is stille 'last name, last name'. Any help will be greatly appreciated, thanks! Quote Link to comment Share on other sites More sharing options...
fastsol Posted September 14, 2013 Share Posted September 14, 2013 You're reassigning the vars and setting them to last name for both. Why even do that? Just echo them as you need them to display $a = <YOUR FIRST NAME>; $b = <YOUR LAST NAME>; echo 'Name (last, first)'.$b.','.$a; Quote Link to comment Share on other sites More sharing options...
requinix Posted September 14, 2013 Share Posted September 14, 2013 Just echo them as you need them to displayWhich works great not counting the "without changing the first or last lines" condition. $a = $b; $b = $a;Statements don't execute at the same time. One is executed and then the next is executed.In the first step you lose the value of $a because you overwrite it with the value of $b. In the second step nothing really changes because $a and $b now both have the same value. So the problem is losing what $a had. Any ideas as to how you can hold onto its old value? Maybe you can put it somewhere temporarily? Quote Link to comment Share on other sites More sharing options...
phpnoob808 Posted September 14, 2013 Author Share Posted September 14, 2013 Which works great not counting the "without changing the first or last lines" condition. $a = $b; $b = $a;Statements don't execute at the same time. One is executed and then the next is executed.In the first step you lose the value of $a because you overwrite it with the value of $b. In the second step nothing really changes because $a and $b now both have the same value. So the problem is losing what $a had. Any ideas as to how you can hold onto its old value? Maybe you can put it somewhere temporarily? Thank you so much for explaining why the value of $a just disappeared! As far as putting the value somewhere temporarily I'll tinker with the code somemore. Thanks. Quote Link to comment Share on other sites More sharing options...
kicken Posted September 14, 2013 Share Posted September 14, 2013 Something like this would work as a one-liner swap: list ($b,$a) = array($a, $b); Otherwise, you'd use a temp variable as in: $a = 'foo'; $b = 'bar'; $tmp = $a; $a = $b; $b = $tmp; Quote Link to comment Share on other sites More sharing options...
Solution phpnoob808 Posted September 14, 2013 Author Solution Share Posted September 14, 2013 Something like this would work as a one-liner swap: list ($b,$a) = array($a, $b); Otherwise, you'd use a temp variable as in: $a = 'foo'; $b = 'bar'; $tmp = $a; $a = $b; $b = $tmp; Thank you so much for the $temp function. We didn't go over it in class. :/ Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.