How to convert a string to a decimal exactly and vice versa - php

I want to start off by saying that I'm sorry if this question has already been asked- I looked around and there was nothing that matched my query. Basically I want to know how to convert the string "100.0" or "100." to the floats 100.0 and 100.0 and also how to make sure the floats 100.0 and 100. don't equal each other (the same goes for situations like 100. and 100 and 100.0 and 100 Thanks!
Edit: To clarify the not equaling thing here's an example:
Let's say you have a variable $a = 100. and $b = 100.0 I want to make sure $a doesn't equal $b

If you have defined $a and $b like:
$a = 100;
$b = 100.0;
.. then they aren't the same. $a is an integer and $b is a float. You can see this using:
var_dump($a, $b);
But however, as they both are numerice types, you need to compare them using the strict comparison operator ===:
if($a === $b) {
echo "equal";
} else {
echo "not equal";
}
If you have them defined as strings:
$a = "100";
$b = "100.0";
then even the simple equal operator == would work:
if($a == $b) {
echo "equal";
} else {
echo "not equal";
}

Related

PHP - Is operator order LTR or RTL?

If I have a conditional statement
if (A > B || B > C)
Which statement is going to be evaluated first: "A > B" or "B > C"?
Does same order is applied to math statements:
$var = $value1 + $value2 + $value3;
Thanks,
Alex.
In PHP the script is evaluated from left to right unless parenthesis are used, if they are used it evalutes then in logical order. In addition please remember that no code in the if condition block(including evaluators) are ran past the first failing statement. This example will only execute the second echo $a and it's value will be 0
$a = 0;
if(1 == 0 && $a = 5)
{
echo $a;
}
echo $a;
This statement will have $a value of 5 and will execute the statement. Interestingly, the reason that the code will execute is because the $a = 5 assignment in the if sets $a = 5 or " 5 = 5".
if(1 == 1 && $a = 5)
{
echo $a;
}
Also note there are else and else if statements if you have not looked into it
$a =2
if($a == 2)
{
}
else if($a > 2){
echo ">".$a;
}
else{
echo "its none of the conditions";
}
The reason that you use two equals signs is to compare the value type insensitive vs one equal which would be assigning the value. There is also three equals which would compare the type and value example This would evaluate to true :
$a = 2;
if($a == "2")
The following would not be true because you are comparing a String to integer.
$a = 2;
if($a === "2")
Regarding your second questions the same is true of String operators but your syntax is INVALID.
This Example Would say Hellow World:
echo "hellow"."world";
This Example IS NOT DOING CONCATENATION(Though it would do addition if they are integers)
echo "hellow" + "world";

Wrong PHP string equality

For some reason, PHP decided that if:
$a = "3.14159265358979326666666666"
$b = "3.14159265358979323846264338"
$a == $b is true.
Why is that, and how can I fix that?
It ruins my code.
The Problem
PHP converts strings (if possible) to numbers (source). Floating points have a limited precision (source). So $a == $b because of rounding errors.
The Fix
Use === or !==.
Try it
<?php
$a = "3.14159265358979326666666666";
$b = "3.14159265358979323846264338";
if ($a == $b) {
echo "'$a' and '$b' are equal with ==.<br/>";
} else {
echo "'$a' and '$b' are NOT equal with ==.<br/>";
}
if ($a === $b) {
echo "'$a' and '$b' are equal with ===.<br/>";
} else {
echo "'$a' and '$b' are NOT equal with ===.<br/>";
}
?>
Results in
'3.14159265358979326666666666' and '3.14159265358979323846264338' are equal with ==.
'3.14159265358979326666666666' and '3.14159265358979323846264338' are NOT equal with ===.
Note
When you want to do high precision mathematics, you should take a look at BC Math.
You could use === in the equality test.
$a = "3.14159265358979326666666666";
$b = "3.14159265358979323846264338";
if($a===$b)
{
echo "ok";
}
else
{
echo "nope";
}
This code will echo nope.
Comparing with == is a loose comparison, and both strings will be converted to numbers, and not compared right away.
Using === will perform a string comparison, without type conversion, and will give you the wanted result.
You can find more explanations in the PHP manual:
PHP type comparison tables
Comparison Operators
Try using $a === $b instead; you should never use == for string comparison.
Read PHP: Comparison Operators
If you compare a number with a string or the comparison involves
numerical strings, then each string is converted to a number and the
comparison performed numerically. These rules also apply to the switch
statement. The type conversion does not take place when the comparison
is === or !== as this involves comparing the type as well as the
value.
Others have recommended BC Math, but if you are doing floating point comparisons, the traditional way of comparing numbers is to see if they are the same to a reasonable error level
$epsilon = 1.0e-10;
if (abs($a - $b) < $epsilon) then {
// they're the same for all practical purposes
}
You should not to compare a float variable like that.
Try this:
bccomp($a, $b, 26)

PHP multiplication

It is my first question, so hi all..
Is it possible to satisfy this condition?
Where $a and $b are some defined variables
if(!is_numeric($a * $b.'')) { ... }
EDIT: Second conditon
$a * $b can't be INF
You can satisfy the condition with specific values of $a and $b. For example, $a = $b = 1e200, or $a = 1e400, $b = 0.
If the product of $a and $b overflows to infinity or is a not-a-number, the concatenation with an empty string will produce the string INF or NAN:
php > var_export(1e200*1e200);
INF
php > var_export(is_numeric(1e200*1e200));
true
php > var_export(is_numeric(1e200*1e200.''));
false
php > var_export(1e400*0);
NAN
php > var_export(is_numeric(1e400*0));
true
php > var_export(is_numeric(1e400*0 .''));
false
Tested using 32-bit PHP
$a = 9.9E300;
$b = 9.9E300;
if(!is_numeric($a * $b.'')) {
echo 'non-numeric';
} else {
echo 'numeric';
}

Confusion over the value of a variable after running a PHP program

I'm studying for my finals and I came across this question:
consider this following PHP code, write the output after executing it
<?php
$a=3;
$b=$a++;
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
When I output it in my text editor I get a < 3, but I don't understand why though?
I thought a is assigned to 3 and also b is assigned to a++ 3 and 3==3 so should a==3 be printed out?
No, you are using post-increment operator on $a. So, $b will be assigned a value of 3, and later, when the statement is executed, $a will increment itself by one, and become 4. So, you'll now be comparing $a as 4 and $b as 3.
Hence you get the result a > 3
The $a++ incrementation happens after the expression gets evaluated, while ++$a would happen before.
So in your case, $b was first set to 3, and then $a was increased.
$a++ tells the variable $a explicitely to increase, no matter if you assigning to another variable or not!
This gives the possibility to do things like if ($a++ > 10) { // ... in loops.
For your case you would have to take $b = $a + 1;
<?php
$a=3;
$b=$a++;
// $b = 3 and $a = 4 now
IF($a>$b)
{
echo "a>$b";
}
else if ($a == $b)
{
echo "a=$b";
}
else
{
echo "a < $b";
}
?>
I tested your code and I get:
a>3
which makes sense
$a is 3 but is increased to 4 when you do $a++
$b is just $a before the ++ action so it stays 3
Think of $a++ as $a = $a + 1 then it makes sense

Operator precedence issue in Perl and PHP

PHP:
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
echo $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
echo $a.'-'.$b;
Output 6-16-2.I don't understand the 1 here.
Perl :
$a = 2;
$b = 3;
if($b=1 && $a=5)
{
$a++;
$b++;
}
print $a.'-'.$b;
$a = 2;
$b = 3;
if($a=5 and $b=1)
{
$a++;
$b++;
}
print $a.'-'.$b;
Output 6-66-2, I don't understand the second 6 here.
Anyone knows the reason?
Actually I know && has higher precedence than and,but I still has the doubt when knowing this before hand.
UPDATE
Now I understand the PHP one,what about the Perl one?
Regarding Perl:
Unlike PHP (but like Python, JavaScript, etc.) the boolean operators don't return a boolean value but the value that made the expression true (or the last value) determines the final result of the expression† (source).
$b=1 && $a=5
is evaluated as
$b = (1 && $a=5) // same as in PHP
which is the same as $b = (1 && 5) (assignment "returns" the assigned value) and assigns 5 to $b.
The bottom line is: The operator precedence is the same in Perl and PHP (at least in this case), but they differ in what value is returned by the boolean operators.
FWIW, PHP's operator precedence can be found here.
What's more interesting (at least this was new to me) is that PHP does not perform type conversion for the increment/decrement operators.
So if $b is true, then $b++ leaves the value as true, while e.g. $b += 1 assigns 2 to $b.
†: What I mean with this is that it returns the first (leftmost) value which
evaluates to false in case of &&
evaluates to true in case of ||
or the last value of the expression.
First example
$a = 2;
$b = 3;
if($b=1 && $a=5) // means $b = (1 && $a=5)
{
var_dump($b); //bool(true) because of &&
$a++;
$b++; //bool(true)++ ==true, ok
}
echo $a.'-'.$b;
hope you will not use those codes in production)
I'm noob in perl but i can suggest a&&b returns a or b (last of them if all of them converted to bool), not boolean, then $b = (1 && $a=5) returns $b=5 (is 5)
here's the issue: 1 && 5 returns 5 in perl. you get the result you expect if you code the conditional as if(($b=1) && ($a=5))
For Perl, fig. 2: and has a very low priority in perl, it's not a synonym of &&'s. Therefore the sample is executed as (($a = 5) and ($b = 1)) which sets $a and $b to 5 and 1 respectively and returns a value of the last argument (i.e. 1).
After ++'s you get 6-2.
refer to http://sillythingsthatmatter.in/PHP/operators.php for good examples

Categories