0
$var .= "hello";
echo $var;

Is the above block of code valid or will it generate notices / errors under some circumstances?

If no should $var = "" or just $var be added before the block of code?

2
  • 1
    as a best practice you need to declare $var = "" first. Commented Jun 5, 2017 at 4:36
  • You must declare your variable first before passing some value into it Commented Jun 5, 2017 at 4:37

1 Answer 1

2

Above block of code will generate notice the following notice:

Undefined variable: var

Variables should be defined before they are used in code, see example below:

<?php
    $var = '';
    $var .= "hello";
    echo $var;
?>
Sign up to request clarification or add additional context in comments.

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.