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

Recommended Answers

All 4 Replies

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.