PHP bizarre math - php

I'm getting unexpected values for variable calculations:
$var1 = $var2 * (((1 + $var3)^$var4)^$var5);
I've verified that $var2 is 3, $var3 is 0.1, $var4 is 1, $var5 is 1.1 so,
$var1 = 3*(((1+0.1)^1)^1.1) = 3.3316 but in PHP, $var1 = 3
if I change $var4 to 2,
$var1 = 3*(((1+0.1)^1)^1.1) = 3.6999 but in PHP, $var1 = 6
Why is this? Any ideas? I've tried explicitly declaring all variables as floats.

Note that ^ is not "power of". You may want to have a look at the pow function.
(^ is actually "bitwise exclusive or".)

Related

How to convert a string to integer in php

I have a variable $var1 = "21,855.00". I already tried using preg_replace and str_replace but I could not really achieve what I was looking for.
I wish to echo it as 21855. How can I do that using PHP?
Use intval()
$var1 = 21855.00;
$varInt= intval($var1);
You can also do
$varInt= (int)$var1;
You can also use floor() to round down
$varInt= floor($var1);
You can also use ceil() to round up
$varInt= ceil($var1);
You can also do
settype($var1, "integer");
haha i must be having a lot of time to waste today :)
Just taking note:
If its a literal string like:
$var1 = '21,855.00'; // string(9) "21,855.00"
You can also do it this way:
$var1 = '21,855.00';
$var2 = filter_var(strtok($var1, '.'), FILTER_SANITIZE_NUMBER_INT);
echo $var2; // 21855
Sample Output
Or with regex:
$var1 = '21,855.00';
$var2 = (int) preg_replace('/(?<=\d),(?=\d{3}\b)/','', $var1);
var_dump($var2); // int(21855)
You can do it in different ways;
$myFloat = 123456.78;
$firstWay = intval($myFloat);
$secondWay= (int) $myFloat;
You can use intval() as Hanky stated or cast the value to integer with using (int). I believe using (int) $myFloat is faster than using intval($myFloat)
You can try with this also -
$var1 = (int) 21855.00;
You could try intval()
eg:
$val = 21855.00;
$val_int = intval($val);

PHP variable behavior or convention

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.

Concatenate two integers in php

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;

PHP Datatype mismatch on comparison

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;

Finding a smaller value and swap values in php

$var1 = 22;
$var2 = 10;
echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var
echo '';
echo $var2 = ($var1 > $var2) ? $var1 : $var2; //greater var
I expect it to print 10 and 22 but it prints 10 and 10. any ideas what I am doing wrong?
Thanks
UPDATE
Thanks all.
$min = min($var1, $var2);
$max = max($var1, $var2);
$var1 = $min;
$var2 = $max;
#unicornaddict already solved your problem, but to make it simpler you can use the min and max functions PHP provides.
echo min($var1, $var2), '<br/>', max($var1, $var2);
You are re-assigning the variables in the echo.
// $var1 is being assigned minimum of 10,22 which is 10.
// after this $var1 and $var2 will both be 10.
echo $var1 = ($var1 < $var2) ? $var1 : $var2;
What you want it:
echo ($var1 < $var2) ? $var1 : $var2; // prints min.
echo '<br />';
echo ($var1 > $var2) ? $var1 : $var2; // prints max.
EDIT:
If you always want the smaller of the two values in $var1 you can do:
if($var1 > $var2) { // if $var1 is larger...swap.
list($var1,$var2) = array($var2,$var1);
}
You overwrite $var1 in your first comparison. So the second comparison compares 10 > 10.
$var1 = 22;
$var2 = 10;
echo $var1 = (10 < 22) ? 22 : 10; //smaller var -> $var1 now has the value 10
echo '<br />';
echo $var2 = (10 > 10) ? 22 : 10; //greater var -> 10 is not greater than 10, so $var2 gets a value of 10.
you assign 10 to $var1 with the first echo, so at the second they are both 10.
echo $var1 = ($var1 < $var2) ? $var1 : $var2; //smaller var
This assigns 10 to $var1. Now both variables contain 10. So what do you expect of the second line?
You need a temporary variable. Just use min,
echo min($var1, $var2);
Your question also mentions swapping the values, which the other answers don't seem to make any note of. Given your example code it looks like you want $var1 to contain the smaller of the two values and $var2 the bigger.
$var1 = 22;
$var2 = 10;
if ($var1 > $var2) {
list($var1, $var2) = array($var2, $var1);
}
// $var1 will now be smaller than (or equal to!) $var2

Categories