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
– KingRider
These solutions are in Java. They use many native libraries to work.
– Ricardo Mota
Yes, Java and does not understand the language? you can exchange information for your PHP PDO code
– KingRider
See if you can solve it: http://stackoverflow.com/questions/18356437/swap-two-variables-value-without-using-third-variable-in-php
– Papa Charlie
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
@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.
– Ricardo Mota
@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.
– Ricardo Mota
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
.– Papa Charlie
@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.
– Bacco
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.
– Ricardo Mota
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.
– Bacco