This is my case.(i changed my case)
I have variable let say $var1 from database and method test($var1)
$var1 = getFromDatabase();
//now $var1 contaion "This is new variable".$var2
Then in test method i will do simple string operation from the $var1 equation.
test($var1){
$var2 = "variable 2";
//I want to extract $var1 so that string will be combined
$value = $var1;
$value_expected = "This is new variable".$var2;
}
But $value will contain "This is new variable".$var2.
I expect $value contain This is new variable variable 2 from the $var1 equation.
Is there any solution? Is that possible to do?
Thank You.
Don't use quotes. $var1 = $var2 + $var3;
Quotes mean you're working with strings
Apart from that, you can't access local variables like that. Variables declared inside the function will not be accessible outside of it. And even if you could you would still not be getting what you expect because you're using $var2 and $var3 before initializing them.
What you're looking for is possible by passing functions around. For example:
function test( $var1 ) {
$var2 = 4;
$var3 = 5;
// this line calls the function named in $var1 with $var2 and $var3 as arguments
$value = call_user_func( $var1, $var2, $var3 );
// in later versions of PHP you can do this instead:
// $value = $var1( $var2, $var3 );
}
function addThem( $a, $b ) {
return $a + $b;
}
test( 'addThem' );
Notice that the method of combining variables is passed as a function (or at least, the closest PHP has to passing functions around; PHP is an odd language). Also the values it works on must be passed as parameters.
Related
In PHP, I have seen a lot of people using the this:
if($var1 = myfunction()){
//do something
}
That way, if true the variable already holds the value you need.
Why doesn't the same work with two variables and two functions?
if($var1 = myfunction() && $var2 = myfunction2()){
// Do something
}
When i tried using the above, I always got a "1". Even though both functions return a value. As soon as I removed the second part ($var2 = ) - it worked.
Why do I get a 1?
It's because of operator precedence
php interprets
$var1 = myfunction() && $var2 = myfunction2()
expression as
$var1 = ( myfunction() && ( $var2 = myfunction2() ) )
So:
$var2 = myfunction2() comes first
Then myfunction() && $var2 is evaluated
The result of the latter expression (which is always a boolean) is assigned to $var1
The solution - use parentheses
($var1 = myfunction()) && ($var2 = myfunction2())
The better solution: avoid such expressions
I have encountered the need to access/change a variable as such:
$this->{$var}
The context is with CI datamapper get rules. I can't seem to find what this syntax actually does. What do the {'s do in this context?
Why can't you just use:
$this->var
This is a variable variable, such that you will end up with $this->{value-of-$val}.
See: http://php.net/manual/en/language.variables.variable.php
So for example:
$this->a = "hello";
$this->b = "hi";
$this->val = "howdy";
$val = "a";
echo $this->{$val}; // outputs "hello"
$val = "b";
echo $this->{$val}; // outputs "hi"
echo $this->val; // outputs "howdy"
echo $this->{"val"}; // also outputs "howdy"
Working example: http://3v4l.org/QNds9
This of course is working within a class context. You can use variable variables in a local context just as easily like this:
$a = "hello";
$b = "hi";
$val = "a";
echo $$val; // outputs "hello"
$val = "b";
echo $$val; // outputs "hi"
Working example: http://3v4l.org/n16sk
First of all $this->{$var} and $this->var are two very different things. The latter will request the var class variable while the other will request the name of the variable contained in the string of $var. If $var is the string 'foo' then it will request $this->foo and so on.
This is useful for dynamic programming (when you know the name of the variable only at runtime). But the classic {} notation in a string context is very powerful especially when you have weird variable names:
${'y - x'} = 'Ok';
$var = 'y - x';
echo ${$var};
will print Ok even if the variable name y - x isn't valid because of the spaces and the - character.
I know it is possible in php to concatenate two strings like
$a .= $b;
which is equal to
$a = $a . $b;
Is it possible to do something with integers? but with math operations? I have two variables:
$var1 = 8;
$var2 = 2;
I need to do $var1 - $var2, but I don't want to create third variable to hold this calculation. I would try $var1 = $var1 + $var2 will this work? Is there another .= similar way of doing it?
OK, because everybody is confused now
// _Appending_ $var2 to $var1 and assign it to $var1
$var1 .= $var2;
// _Add_ $var2 to $var1 and assign the result to $var1
$var1 += $var2;
There is nothing more to remember than, that every binary operator is combineable with the assignment. Within the engine it's always expanded to
$var1 = $var1 . $var2; // You can imagine how the others looks like
In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic, array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression.
I think you need a string formatting.
$var1 = sprintf( "%d%d", $var1, $var2 );
which will make your $var1 as 82.
As in C and C++, PHP supports Compound_assignment_operators:
So, the line:
$var1 = $var1 + $var2;
is equivalent to:
$var1 += $var2;
For full list of those operators, take a look at Compound_assignment_operators
Yes
$var1 = 1
$var2 = -1;
$var1 += $var2; // $var1 = 0
Yes, Compound Assignment operators will work in php as C & Java.
you can try like this
$Var1+=$Var2;
$var1=$var1-$var2;
will work fine, but shorthand operators like
$var1=-$var2
will not work.
I will set -2 to $var1
Notation for shorthand operator is :
$var1-=$var2;
So I have 2 variables, var1, var2.
$var1 = "53,000,000" //- integer
$var2 = 10 //- string
In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1
I get $var2 = 0 .... Why ?
.. running on PHP 5.2.14
EDIT Accidentally typed in substr_replace instead of str_replace. Updated.
I had to add a couple semicolons, but here's the code:
$var1 = "53,000,000"; //- integer
$var2 = 10; //- string
//In the end I want to compare both, so I
$var1 = (int)str_replace(",","",$var1); // => 53000000 - integer
//Here's my issue .. if I do:
if($var1 > $var2)
$var2 = $var1;
var_dump($var1, $var2);
And here's my output:
int(53000000) int(53000000)
I used 5.2.6, but it shouldn't matter. Do you have any other code in between what you're showing?
Use str_replace() instead of substr_replace().
You've specified the wrong parameters for substr_replace, so $var1 is evaluated to 0. I guess you wanted to use str_replace.
No need for type casting. just do the str_replace
Here is code
$var1 = "53,000,000" ;
$var2 = 10;
$var1=str_replace(',','',$var1);
if($var1 > $var2)
$var2 = $var1;
echo $var2;
In perl I can write
($var1, $var2, $var3) = split(/:/, "foo:bar:blarg")
to split a string by colon and assign each array element to $var1, $var2 and $var3. Is there a similar syntax for this in php? I have to use the variables in a lot of my code so I want them to have meaningful names, and writing variable assignment code like
$array = split(/:/, "foo:bar:blarg");
$var1 = $array[0];
$var2 = $array[1];
//etc
is tedious when i have to do this a lot.
PHP has the list language construct to do that.
list($var1, $var2, $var3) = split(":", "foo:bar:blarg");