can someone please elaborate on code below that evaluates to 42!

//$var is an integer = 42
$var = "3 mice" + 39;

can someone please elaborate on code below that evaluates to 42!

//$var is an integer = 42
$var = "3 mice" + 39;

<?php
//$var is an integer = 42
$str ="mice"; // it is declared as a string is assigned
$int =39; // it declared as integer as a integer value is assigned 
echo "<b>str =\"3 mice\";</b> //it is declared as a string is assigned<br>";
echo "<b>int =39;</b> // it declared as integer as a integer value is assigned <br>";
echo "Now we can test the types for each variable using <b>gettype()</b> 
function<br>";
echo "The <b>gettype()</b> returns the type of the variable passed 
into the parameter
as a string<br>";
echo "For example<br>";
$type1=gettype($str);
$type2=gettype($int);
echo "Type of str is <b>$type1</b> <br>";
echo "Type of int is <b>$type2</b><br>";
echo "Now.........<br>";
echo "When <b>str</b> and <b>int</b> are added using a <b>'+'</b> 
operator and the result is assigning into <b>var</b>, the php 
interprets <b>str as string</b> and <b>int as integer</b><br> 
then it tryes to add the integer int with the given string 
value if the <b>str</b> contains any number in its own array 
at the first postition<br> If there is no number in the 
<b>str</b> it consider <b>str</b> as zero (0) and thus it adds
with int<br>It then assigns the <b>var</b> as <b>integer</b><br>";

$var=$str+$int;
echo "<b>var = str + int<br>";
echo "</b>The result will be <b>39<br>";
$type3=gettype($var);
echo "</b>The type of <b>var</b> is:<b>",$type3;
echo "<br><b>YOU CAN TEST FOR THESE....</b><br>";
$test1="no numbers" + 90;
$type4=gettype($test1);
echo "<br>test1=\"no numbers\"+90<br>";
echo "<b>Result=$test1---$type4";

$test2="if numbers goes here 5 then what??or at the end then??5" + 90;
$type5=gettype($test2);
echo "<br>test2=\"if numbers goes here 5 then what??or at the end then??5\"+90<br>";
echo "<b>Result=$test2---$type5</b>";
echo"<br><b>so it did not find numbers at the first position of the string and 
left this considering as string";
?>

N.B:You can download PHPEdit which can help you to debug PHP coding.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.