Sam SB Sam SB - 4 months ago 6
PHP Question

PHP: "-" turns to "0"

This is the form I have:

<form action="data.php" method="get">
<input type="text" name="num1"><br>
<input type="text" name="num2"><br>
<input type="submit" >
</form>


when I

echo $_GET['num1'] . + "-" . + $_GET['num2'];


it gets like:

505


while I want:

5-5


Why does my
-
turning to
0
?

Answer

The + is the addition operator, not the concatenation operator. That's .. The + forces PHP to type juggle the dash into an integer so it can do the math you asked for and as a result the dash becomes zero.

You should remove them as you are already using the right operator:

echo $_GET['num1'] . + "-" . + $_GET['num2'];

should become

echo $_GET['num1'] . "-" . $_GET['num2'];
Comments