John Davidson

string and integer sum calculate in php?

0 comments
Message:


<?php
$a = '32abc';
$b = 1;
echo $a + $b;

Output : 33
?>

Here string $a = '32abc'; calculate with int $b = 1, how take 32 from string word in php. and output show 33 . is there any good explanation on it?



My Answer:
In PHP, when you try to perform a mathematical operation (such as addition) between a string and an integer, PHP will attempt to convert the string to a number.

In this case, the string '32abc' will be converted to the number 32 because it starts with a valid number. The 'abc' part of the string will be ignored during the conversion.

So when you add 32 (from the string) to 1 (the integer), you get 33 as the result.

It's important to note that this behavior may not always be desired or predictable, so it's best to ensure that your variables are of the correct type before performing mathematical operations on them.

Rate this post

3 of 5 based on 7533 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us