any one please help me, i want to know how to swap two variables say, a and b with out using a third vaiable

3
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by elanorejoseph

hi
you can swap a,b like

\$a=5;
\$b=7;
echo "befor swaping value"."a=".\$a."b=".\$b;
\$a=\$a+\$b;//\$a=12
\$b=\$a-\$b//12-7 so \$b hold 5
\$a=\$a-\$b//12-5 so \$a hold 7
echo "after swaping value"."a=".\$a."\$b=".\$b;

i hope you can understand my logic

Thanks

hi
you can swap a,b like

\$a=5;
\$b=7;
echo "befor swaping value"."a=".\$a."b=".\$b;
\$a=\$a+\$b;//\$a=12
\$b=\$a-\$b//12-7 so \$b hold 5
\$a=\$a-\$b//12-5 so \$a hold 7
echo "after swaping value"."a=".\$a."\$b=".\$b;

i hope you can understand my logic

Thanks

Thanks buddy.. i understood your logic :)

in javascript it would be done this way if someone would like to translate it to php

var a="bob";
var b="joe";

a=[a,b];b=a[0];a=a[1];

enjoy! :)

var a="bob";
var b="joe";

a=[a,b];b=a[0];a=a[1];

that should do it! :)

ya that too worked. and i think using this we can swap two strings too
THANK YOU :)

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.