PHP multiplication - php

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';
}

Related

why result of this Comparing is true

in this simple PHP code
why php parser return true?
$text="51.406ABC917";
$floatval = floatval($text);//51.406
if($floatval==$text){
$result_compare = true;//php parser return true
}else{
$result_compare = false;
}
It's about Type Juggling and PHP type comparison tables, and Comparison Operators. Just check it.
Type of Operand 1:string, resource or number
Type of Operand 2: string, resource or number
Translate strings and resources to numbers, usual math.
You could avoid convertion to float by adding typecasting to string.
if((string)$floatval==$text){
$result_compare = true;
}else{
$result_compare = false; //php parser return false
}
== will compare the data ,use === to compare data and datatype
$floatval==$text
in the above comparison you are comparing a float value with a string
try with === instead of ==
Look here . You should never compare floats for equality.
You need use the epsilon technique.
For example:
if (abs($forstFloat - $secondFloat) < epsilon) {
echo 'they are equal!!'
}
where epsilon is constant representing a very small number.
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
Take this as example
<?php
var_dump(0 == "a"); // 0 == 0 -> true
var_dump("1" == "01"); // 1 == 1 -> true
var_dump("10" == "1e1"); // 10 == 10 -> true
var_dump(100 == "1e2"); // 100 == 100 -> true
switch ("a") {
case 0:
echo "0";
break;
case "a": // never reached because "a" is already matched with 0
echo "a";
break;
}
?>
Reference
Try this,
$text="51.406ABC917";
$floatval = floatval($text);//51.406
if($floatval===$text){
$result_compare = 'true';//php parser return true
}else{
$result_compare = 'failed';
}
LOGIC:
$a == $b Equal TRUE if $a is equal to $b after type juggling.
$a === $b Identical TRUE if $a is equal to $b, and they are of the same type.
Ref: http://www.php.net/manual/en/language.operators.comparison.php
Before comparison $text is converted to a float , with same result as floatval(),
then you compare $floatval==$text, so result is pretty predictable - TRUE
It's normal, when you compare variables from 2 different types, first them to be converted to closest same type.
I would suggest, when comparing float, use similar construct
if ( abs($floatval - floatval($text)) < 0.001 ) {..}
compare difference between 2 floats, instead them. Cause if u have 2 numbers, 45 and 45.00001 , php will think they differ.
What do you wanna accomplish ? Why you think this result is wrong ?
try this:
<?php
$text="51.406ABC917";
$floatval = floatval($text);//51.406
if(strval($floatval)==$text){
$result_compare = true;//php parser return true
}else{
$result_compare = false;
}
?>
If first part of string is numbers, it will be converted to numbers otherwise it will be zero (0). Yes, to compare value and type of variable, use === instead of ==.
But in your case, you already convert string to float by floatval($text); then === same as ==.
The problem is how php convert string to number by floatval($text);
This is how php convert string to numbers:
<?php
$foo = 1 + "10.5"; // $foo is float (11.5)
$foo = 1 + "-1.3e3"; // $foo is float (-1299)
$foo = 1 + "bob-1.3e3"; // $foo is integer (1)
$foo = 1 + "bob3"; // $foo is integer (1)
$foo = 1 + "10 Small Pigs"; // $foo is integer (11)
$foo = 4 + "10.2 Little Piggies"; // $foo is float (14.2)
$foo = "10.0 pigs " + 1; // $foo is float (11)
$foo = "10.0 pigs " + 1.0; // $foo is float (11)
?>
Full document here: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

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

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";
}

php syntax $a = $b = 1;

Is
$a = 1;
$b = $a;
equal to writing this?
$a = $b = 1;
Will the second example always put 1 as value to both $a and $b, even if $a and $b already has a value assigned to them?
Quoting the documentation:
The value of an assignment expression is the value assigned. That is,
the value of "$a = 3" is 3. This allows you to do some tricky things:
<?php
$a = ($b = 4) + 5; // $a is equal to 9 now, and $b has been set to 4.
?>
So, to answer your question, the result of the assignment $b = 1 is 1, and therefore, $a = $b = 1 would assign the value of $b = 1--which is to say 1--to $a.
That being said, abusing this can lead to code that is hard to read.
Yes, PHP will put 1 in $b then put $b value in $a, i.e. 1.
There is no ambiguity as the first assignment is $b = 1, the next is $a = $b.

php: operator precedence

I found this question at http://www.phpinterviewquestions.com/php-interview-questions/operator-precedence/
Following operations are true or false? (Operator Precedence)
$one = true;
$two = null;
$a = isset($one) && isset($two);
$b = isset($one) and isset($two);
echo $a.'<br>';
echo $b;
I tried the above code. But only $b gets echoed as 1 (which is true). $a is not getting echoed. What could be the reason? I was expecting $a to be 0 (false).
It's not about precedence, it's about implicit type casting
Use var_dump($a); instead of echo $a;
$a actually is false, but being echo'ed false is casted to empty string.

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