0
<?php
    $hello = "ok";
    $hello+= "hello again";
    echo $hello;
    ?>

Hello. I'd like to understand why this code prints 0 at the end? Using the same method in javascript was working! Also, how do I make it that it change? I'd like the output to change according to any condition I make. Thanks

2 Answers 2

4

The string concatenation operator in PHP is ., not +. When you write

$hello += "hello again";

PHP attempts to convert $hello and "hello again" into numbers, both of which become 0, so at the end of your code the value of $hello is 0. Try this instead:

$hello = "ok";
$hello .= "hello again";
echo $hello;

Output

okhello again
Sign up to request clarification or add additional context in comments.

Comments

1

Use .= instead of += in PHP:

<?php
    $hello = 'ok';
    $hello .= ' hello again';
    echo $hello;
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.