Make a substitution between the values of two variables in PHP - php

I was wondering if it could be possible to make a substitution between the values of two variables, in PHP.
I can explain it better:
<?php
$a = "Cat";
$b = "Dog";
// The strange/non-existent function I am talking about //
MakeSubstitution($a, $b);
// After this (non-existent) function the values of the variables should be:
// $a = "Dog"
// $b = "Cat"
?>
So, does it exist? I made searches but I found no results.
Thanks in advance.

Try this :
$a = "Cat";
$b = "Dog";
list($a,$b) = array($b,$a);
echo $a;
echo $b;

Handle them by reference in a function, and swap their values:
function swap ( &$a, &$b ) {
$t = $a; // Create temp variable with value of $a
$a = $b; // Assign to $a value of $b
$b = $t; // Assign to $b value of temp variable
}
$dog = "dog";
$cat = "cat";
swap($dog, $cat);
echo $dog; // Output 'cat'
Apparently you can use a bitwise operator too, and avoid the overhead of creating a temporary function/var/array:
$cat = "cat";
$dog = "dog";
$cat = $cat ^ $dog;
$dog = $cat ^ $dog;
$cat = $cat ^ $dog;
echo $cat . $dog; // Output 'dogcat'
Managed to find a great illustration of the bitwise approach: https://stackoverflow.com/a/528946/54680

Related

Replace variable by another variable in PHP

Here is my situation, let's say i have two variables:
$a = "Peter";
$b = "tall";
Then, i can combine this by
$c = " $a is very $b ";
which give me the string : "Peter is very tall"
However, if i have another variable $e passed to my function, and the string is " $a is very $b "
then, i print out $e, it just show me " $a is very $b ";
What i expected is "Peter is very tall", because $e is equal to $c.
This is the whole logic flow:
$e = " $a is very $b ";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
$c = " $a is very $b ";
echo $c //Peter is very tall
echo $e //$a is very $b
}
How can i achieve this function?
You could use sprintf sprintf documentation. The %s symbols are string tokens that get substituted in the format string (the 1st param to sprintf).
$e = "%s is very %s";
getMsg($e);
function getMsg($e){
$a = "Peter";
$b = "tall";
echo sprintf($e, $a, $b);
}
Or a less coupled version of the getMsg() function:
function getMsg($msg,$name,$is){
echo sprintf($msg, $name, $is);
}
getMsg("%s is very %s", "Peter", "tall");
Please try this code. I hope this is what you want.
<?php
// Your code here!
$a = "Peter";
$b = "tall";
$e = $a."is very".$b;
getMsg($e,$a,$b);
function getMsg($e,$a,$b)
{
$c = $a."is very".$b;
echo $c;
echo $e;
}
?>
Try this. It should work fine. You need to Define $a and $b outside the function.
function getString($a,$b,$e){
$c = " $a is very $b ";
echo $c;
echo '<br/>'.$e;
}
$a = "Peter";
$b = "tall";
$e = " $a is very $b ";
$response = getString($a,$b,$e);
OUTPUT :
Peter is very tall
Peter is very tall

number_format multiple vars at once in php

I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;

if a variable is referenced inside a function then what happens

function a(&$c, &$d){
$c = &$d;
}
$a = 1;
$b = 2;
a($a, $b);
echo $a;
output is 1,but shouldn't it be outputting 2 as $c is referencing $d. $c and $a reference to the same value,then $c refer to the value of $d which refer to $b so ultimately $a should refer to $b, isn't it correct?
For the operation you seek you must remove the ampersand reference used within the function a.
Example
function swap (&$one, &$two) {
$tmp = $one; // One is stored temporarily
$one = $two; // Two is stored in One
$two = $tmp; // Temporary data retrieved and stored in two
unset($tmp); // Temporary variable destroyed
}
// Set the variables
$a = 1;
$b = 2;
echo $a . " - " . $b . "<br />"; // See output as: 1 - 2
swap($a, $b);
echo $a . " - " . $b; // See output as: 2 - 1
in line
$c = &$d;
$a is referring to the address of $b but not to $b, then you can try this:
<?php
function foo(&$c, &$d){
$c = $d;
}
$a = 1;
$b = 2;
foo($a, $b);
echo $a;
?>

Passing variable names into unset function in PHP

I have this piece of code:
<?php
$a = "dog";
$b = "cat";
$c = "pig";
$params = array($a, $b, $c);
some_function($params);
unset($a, $b, $c);
//echo "<br>".$a."<br>".$b."<br>".$c;
?>
Is here any elegant way, how to pass only variable names (not evaluated variables) to unset function? Something like:
unset($params); //but this doesn't work
Instead of
unset($a, $b, $c);
Thanks for any help.
The only way is to define variable names instead of variable values, and unset them in $GLOBALS:
<?php
$a = "dog";
$b = "cat";
$c = "pig";
$myvar = 'another var';
$params = array('a', 'b', 'c', 'myvar');
myunset($params);
function myunset($params){
foreach($params as $v){
unset($GLOBALS[$v]);
}
}
var_dump($myvar); //NULL
?>

What is the difference between “=” and “=&” when assigning variables?

I am trying to figure out the difference between $a=&$b and $a=$b.
I know & make the variable to be a reference variable. But the following test gave me the same result. Can anyone explain the difference? Thanks.
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$a=5;
$b=6;
$a=$b;
echo $a; //6
First of all: you'll hardly ever need references, avoid the confusion of using them if you can.
$a=5; //assign value to a
$b=&$a; //make $b a reference to $a
$b=6; //assigning a value to $b assigns the same value to $a (as they point to the same location
echo $a; //6
$a=5; //assign a value to a
$b=$a; //set $b to the value of $a
$b=6; //set $b to another value leaves $a at it's original value
echo $a; //5
It matters more in a function when you send it in as a parameter.
For example:
<?php
function changeVariableWithReference(&$var)
{
$var += 1;
}
function changeVariableWithoutReference($var)
{
$var += 1;
}
$a = 5;
$b = 5;
changeVariableWithReference($a);
changeVariableWithoutReference($b);
print $a . ' ' . $b;
?>
The difference between $a = $b and $a =& $b is that with the former assignment operator the value is copied while with the latter reference operator the variable $a refers to the same value as $b.
You don’t see any difference when reading the value but you see it when writing the value:
$var = 123;
$copyOfVar = $var;
$referenceToVar =& $var;
$copyOfVar = 456;
echo 'var='.$var.'; copyOfVar='.$copyOfVar;
// "var=123; copyOfVar=456"
$referenceToVar = 456;
echo 'var='.$var.'; referenceToVar='.$referenceToVar;
// "var=456; referenceToVar=456"
When you use the & in an assignment, you can think of the new variable being a 'short-cut' to the original. If you don't use the &, it will be a 'copy' of the original.
$a = 5;
$b =& $a; // this $b is like a shortcut to $a. Change $b and $a will change too
$a = 5;
$b = $a; // this time, $b will be 5 - but not a shortcut to $a. Change $b and $a will still be 5.
A reference can be easily explained by simple graph. When you use copy by value ($a = $b) then something like that happens:
$a = 1000;
# $a -----> 1000
$b = $a;
# $a -----> 1000
# $b -----> 1000
# (two "pieces of memory" has been used)
But when you create a new reference to $a named $b then something like that happens:
$a = 1000;
$b =& $a;
# $a --\
# --> 1000
# $b --/
# (one "piece of memory" has been used but two different names ($a, $b) point on it)
You will get the ans, if you follow the following code (i have add some lines)
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$b = 8;
echo $a; //8
$a=5;
$b=6;
$a=$b;
echo $a; //6
$b = 20;
echo $a; //6
with & simple, variable $a points the variable $b
without &, $b just copy into $a

Categories