Jump to content

Question on swapping values and variables?


Go to solution Solved by phpnoob808,

Recommended Posts

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!

Just echo them as you need them to display

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?

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.

  • Solution

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. :/

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.