Replace variable by another variable in PHP - 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

Related

php global variable in functions

this is a very simple program, but when i run it, it just out put the first echo statement, which is 3, others are not displaying. i declared it global in the function, why it dose not work, strangely, when i was coding another program declaring a global array variable in a function it works perfectly, please explain it in detail, thanks
$a = 1;
$b = 2;
$c = $a + $b;
echo $c ."<br>";
function aaa()
{
global $a;
global $b;
$d = $a + $b;
echo $a ."<br>";
function ccc()
{
global $d;
$e = $c + 1;
echo $e;
}
}
The problem with your code is that you've put the function ccc inside the funciton aaa and that is not the correct way of doing what you're trying to do.
The correct way would be to create a Class aaa and then declaring ccc as its method.
$a = 1;
$b = 2;
$c = $a + $b;
$d = 0;
function aaa() {
global $a;
global $b;
global $d;
$d = $a + $b;
echo $a ."<br>";
}
function ccc() {
global $c;
global $d;
$e = $c + 1;
echo $e;
}
echo $c ."<br>";
aaa();
ccc();

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;
?>

Make a substitution between the values of two variables in 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

Insert variable into string at random position

See this code:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
?>
How can I insert $b into a random position of $a?
Assuming "casual" means random:
<?php
$a = rand(1, 10000000000);
$b = "abcdefghi";
//get a random position in a
$randPos = rand(0,strlen($a));
//insert $b in $a
$c = substr($a, 0, $randPos).$b.substr($a, $randPos);
var_dump($c);
?>
above code working: http://codepad.org/VCNBAYt1
Edit: had the vars backwards. I read "insert a into b,
I guess you could by treating $a as a string and concatenating it with $b:
$a = rand(1, 1000000);
$b= "abcd";
$pos = rand(0, strlen($a));
$a = substr($a, 0, $pos).$b.substr($a, $pos, strlen($a)-$pos);
and the results:
a=525019
pos=4
a=5250abcd19
a=128715
pos=5
a=12871abcd5
You should put {$b} on top of {$a} so that you can insert it to {$b}..
eg:
<?php
$b = "abcdefghi";
$a = rand(1, 10000000000);
$a .= $b;
echo $a;
?>
Sth like this :
<?php
$position = GetRandomPosition(); // you will have to implement this function
if($position >= strlen($a) - 1) {
$a .= $b;
} else {
$str = str_split($a, $position);
$a = $str[0] . $b . implode(array_diff($str, array($str[0])));
}
?>
Cast $a to string, then use strlen to get the length of $a. Use rand, with with the length of $a as the maximum, to get a random position within $a. Then use substr_replace to insert $b into $a at the position you've just randomized.

Categories