20
$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

1

6 Answers 6

21

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.

Sign up to request clarification or add additional context in comments.

5 Comments

How its getting 3 from "3dollar" ?
That's just how PHP handles string to int conversions. It pick out the leading number and ignores the rest. Please read the link posted by Phpenix as a comment to your question.
A more appropriate link: php.net/manual/en/…
This is one of the worst thing i've ever could suppose about php!!! Here we have a situation. PHP: "Okay, give me your variables. Well, int and string. And what do you want to do? To add? Let's see what I can do... Okay! Take the sum! What? This sum is wrong? Why? Data from string had lost without any warning, and the sum is not associative? Listen, you give me bullsh!t, I've returned bullsh!t for you. What do you want else? A warning? Wanna warning - go to strict typisation, and fall in love with she!
I have to agree with @IgorYudnikov. I haven't touched PHP since school but today I was asked to help a Joomla guy figure out why he couldn't compare a $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.
5

The right way to add (which is technically concatenating) strings is

$a = 7;
$b = "3 dollars";
print ($a . $b);  // 73 dollars

The + operator in php automatically converts string into numbers, which explains why your code carried out arimethic instead of concatenation

Comments

3

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.

Comments

0

PHP treats '3dollars' as a integer 3 because string starting with integer and participating in arithmetic operation, so

    $a = "3dollars";
    $b = 20;
    echo $a += $b;
    it echo 23;   //$a=$a+$b;

    now $a = 23 + 20;
    print($a += $b);    //$a=$a+$b;
    it print 43;

Comments

0

If you need both the values, return them in an array

1 Comment

question asks for concatenation of string and number.
-1

Since You have created a variable for the two, it stores the result of each, so when you added $a to 20 it will echo 23 which stores in the system, them when you print $a which is now 23 in addition to $b which is 20. You will get 43.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.