TanguyH TanguyH - 2 months ago 8
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.

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


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

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


But when I do this :

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

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

Greetz !

Answer

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

It should look like in this example:

    <?php
    $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'];