$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);
Result:
23 43
I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it
$a = "3dollars";
$b = 20;
echo $a += $b;
print($a += $b);
Result:
23 43
I have a question from this calculation.$a is a string and $b is number.I am adding both and print using echo its print 23 and print using print return 43.How is it
It casts '3dollars' as a number, getting $a = 3.
When you echo, you add 20, to $a, so it prints 23 and $a = 23.
Then, when you print, you again add 20, so now $a = 43.
$price variable with some numerical values. I told him to try gettype() and also add 1 to the variable. Even though the variable was a string the addition was performed to my great surprise. As soon as the string contained a dot to separate thousands the addition broke. This is the WORST thing I have ever encountered in my life as a programmer followed by Python's global keyword and I'm happy I didn't get involved with PHP after all.PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
In PHP 7, type declarations were added. This gives us an option to specify the expected data type when declaring a function, and by adding the strict declaration, it will throw a "Fatal Error" if the data type mismatches.
To specify strict we need to set declare(strict_types=1);. This must be on the very first line of the PHP file. Then it will show fatal error and if you didn't declare this strict then it convert string into integer.
If you need both the values, return them in an array