TanguyH TanguyH - 11 months ago 43
PHP Question

$_POST echoing 0 when containing strings

I made a basic HTML form working with post (it's a login form). The form itself works great, but when I try to work with the information contained in $_POST it goes wrong.

// case : nothing submitted
if (!isset($_POST['submit'])) {
<div class="login">
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input type="text" id="username" placeholder="Username" name="username" />
<input type="password" id="password" placeholder="Password" name="password" />
<button name="submit">Submit</button>
// case : input form submitted
} else {

The problem is, it works fine when I do this ( above

echo $_POST['username'];
echo $_POST['password'];

But when I do this :

echo $_POST['username'] + " " + $_POST['password'];

It echoes 0. As
is a function that connects to my DB, the request isn't executed. Does someone have any idea of the possible cause ?

Greetz !

Answer Source

You must use dots when concatenating in PHP. You probably "took" pluses from JS.

It should look like in this example:

    $a = "Hello ";
    $b = $a . "World!"; // now $b contains "Hello World!"
    $a = "Hello ";
    $a .= "World!";     // now $a contains "Hello World!"

And your example, if the variables are good:

  echo $_POST['username']." ".$_POST['password'];