0

In php, why "1"+"1" when added and echo gives 2 isn't putting inside a double quote makes the number string?

<?php
$x = "1";
$y = "3";

$z = $x + $y;


echo($z); // this print 4

?>

Also, if we take this example, since, the value in the variable is inside the double quote and if you use a single quote to print the below expression why it prints $x + $y = $z

<?php
$x = "1";
$y = "3";

$z = $x + $y;

echo '$x + $y = $z'; // prints  $x + $y = $z  

?>
sndp
  • 9
  • 2
  • Also to add strings together you use `.` rather than `+` (https://stackoverflow.com/questions/8336858/how-to-combine-two-strings-together-in-php). – Nigel Ren May 31 '20 at 07:23

1 Answers1

0

Because you are using "+" here:

$z = $x + $y;

When you are doing this PHP will convert to int if possible to make the addition. If you want to store "1 + 3" instead you can go like that:

$z = $x . ' + ' . $y;
Urudin
  • 286
  • 2
  • 14