How to exchange values of two variables without using a third one in php

Asked

Viewed 1,283 times

0

My question is very objective.
I need to exchange values of two variables without using a third one in php.
NOTE: Using array/array is not an option.

Researching I found a solution in linguagem c here, but I couldn’t get through to php. I also found that solution, however there is a bug quirky good.

This is my code:

        $change_provider = "Desenvolvimento Web";
        $change_receiver = "SEO(Search Engine Optimization)";

        $change_provider ^= $change_receiver ^= $change_provider ^= $change_receiver;

        print_r($change_provider); //string(19) "SEO(Search Engine O" 
        print_r($change_receiver); //string(19) "Desenvolvimento Web"

Notice that my output is missing some characters at the end. How to solve this problem ? I appreciate anyone who can help.

  • I saw if you passed the solution site, and only number to do binary, if this tried string with XOR-Assignment, and see http://stackoverflow.com/questions/5126616/xor-operation-with-two-strings-in-java code

  • These solutions are in Java. They use many native libraries to work.

  • Yes, Java and does not understand the language? you can exchange information for your PHP PDO code

  • See if you can solve it: http://stackoverflow.com/questions/18356437/swap-two-variables-value-without-using-third-variable-in-php

  • I found 1 Portuguese this explained well and between http://answall.com/questions/47897/o-que-%C3%A9-o-operador-l%C3%B3gico-xor-em-php-quando-usa-o-que-ele-faz

  • @Kingrider I would have to duplicate the native java library to be able to do it using its logic. As for the other solution is the same one I’m using and not this data right using string.

  • @Papacharlie In the most well voted reply he uses array to make the exchange and this is not an option in my case. In the others it uses the same solution that I found and does not apply to string.

  • How is that not an option? See an example with string: http://ideone.com/ekQM7Q, using array is just to get the order reversed with list.

  • @Ricardomota follows demonstration: http://ideone.com/d0VFZL - on the fact to say that array does not apply in your case, I find unlikely, but when so, compensates for in the question what is the reason. It may simply be lack of use in the most appropriate way, or perhaps the answers will suit you better if the problem becomes clearer. If you can explain why the array doesn’t work, you can think of alternatives.

  • Thank you for the @Bacco solution. The intention is entirely academic, next I will remind of in addition to detail the specifications tell why these.

  • 1

    This helps us post answers that go against the problem. In this case, I don’t see any really good way to do it without a third variable or array, including pq, remembering that PHP stores references (HLL) for the values, both array and 3a variable are "cheap" solutions, which do not involve reallocation of memory with the values. You may even be able to answer the premise of the question without variable or array, but the solution would probably be much more expensive and complex.

Show 6 more comments

2 answers

4

Basically this:

list( $a, $b ) = array( $b, $a );

In the newer versions:

list( $a, $b ) = [ $b, $a ];


It is important to note that both the above solutions and the use of a third variable are effective in the sense that they do not depend on memory relocation, as there is only an exchange of references, not values.

Skip the question a bit, but just for the record, note that PHP references are at a higher level than C pointers, which are memory positions. Regardless, the exchange of position is inexpensive in both languages.


Your solution, XOR Swap is for numbers only:

$change_provider ^= $change_receiver ^= $change_provider ^= $change_receiver;

It is based on a mathematical property of the XOR:

How the Xor Swap algorithm works?


As you have already noticed in your test, it works with strings, but only because they are treated as a sequence of bytes, see mention in the @Omine reply

  • Your solution is more plausible so far @Bacco. Thank you.

2

Given the conditions, the most appropriate form would be using XOR Swap algorithm. That is, the same that you presented in the question using bit-by-bit operators.

However, to function as desired, within the context of the question, it needs that both variables have the same amount of bytes. That is, you can use strings and not just numerical values. What matters is the amount of bytes.

note: The test presented in the question does not return a bug. The variable with a larger string is cut because it is larger than the other variable.

  • 1

    Actually the definition of bug says it is a malfunction in the system, in which case as the result is not expected it can be considered a bug. You would have some example using XOR Swap ?

  • I disagree, because the way you put it returns the correct result and not a bug. The bug itself could be set at the system level but not the swap operation. Get it? By the way, do you understand any of the answer? rsrs have made it clear that you need to have equal amount of bytes.. this in itself explains your "bug"

  • To make it clearer. There is no solution within the conditions you described. There is no magic, unless the strings have the same amount of bytes and even then using a third variable is much faster and simpler than using array or bitwise Operators.

  • 1

    https://en.wikipedia.org/wiki/Software_bug You can read the first paragraph and see that an unwanted result is a bug instead.

  • But thanks for your help.

  • is not a bug... not within the context of what you presented....

  • 1

    You may think so, but my frame fits the definition.

  • If you consider a bug as when result is not expected, several operators would enter the list. Daniel explained the reason why it does not work. To work it is necessary that ambas as variáveis tenham a mesma quantidade de bytes, simple.

  • 1

    Completely @Papacharlie. Daniel explained why the result is not what you’d expect. But that doesn’t change the fact that the result is not expected. Soon it’s a bug.

  • rsrs Ricardo, take a look, if I do this $a = erf; php will return an unexpected result. You mean this is a php bug? rsrs understand ? That’s what you’re saying in the use of bitwise Operator... you’re saying it’s a bug in the bitwise Operator of php but it’s not... and I’ve made it clear that you can set it as a bug in the overall functioning of your script but not that it’s a bug of this php resource... get it? is mixing things up... and if you want to take the test, report it on the php website as a bug and you will see their response: http://bugs.php.net

  • It needs 2 variables of the same size and you use different sizes! The bug is really PHP?

  • I think you are confusing error with bug. the definition of bug is extremely clear: A software bug is an error, flaw, Failure or fault in a computer program or system that causes it to Produce an incorrect or Unexpected result, or to behave in Unintended Ways. https://en.wikipedia.org/wiki/Software_bug

  • You are interpreting literally and disregarding the context here, Ricardo. I’m sorry, but I have nothing more to say.

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.