Sam SB Sam SB - 1 year ago 80
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" >

when I

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

it gets like:


while I want:


Why does my
turning to

Answer Source

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'];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download