Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes, it's always poor programming to echo (and any other usage) a non-existent variable.<br> And it's actually errors out - you just doesn't see it due to wrong PHP settings.</p> <pre><code>error_reporting(E_ALL); </code></pre> <p>at the top of the script will show ye these errors.</p> <p>Although PHP scarcely allow usage of undefined variable, a good programming practice is <strong>to define every variable in the code before it's usage</strong>.<br> It will let you to avoid numerous mistakes and unexpected behaviors or even serious vulnerabilities. </p> <p>Most people, however, take this issue wrong, thinking that a variable should be checked for existence before use. But thinking over this topic a bit, you'll find it quite ridiculous - it seems the only intention of such checking is to gag an error message. So, the language developers invented such warning only to bother a programmer with unnecessary verification? </p> <p>Of course not.<br> A real intention of this warning (as any other warning) is to let a programmer know that something is going wrong.<br> It's very easy really: PHP just tells you <em>"Don't you forget to assign some value to this variable, bro? It seems you're gonna use it, so, you expect some value it it, but there isn't. Just to let ye know."</em> But by using isset() we just gag this warning and nothing more. Doing a disservice to youself. </p> <p>Moreover, by initializing a variable, one can be sure of both variable existence <strong>and it's value.</strong></p> <p>a little example:</p> <pre><code>&lt;? while ($i &lt; 10) { echo $i." "; } echo "&lt;br&gt;"; while ($i &lt; 10) { echo $i." "; } ?&gt; </code></pre> <p>How many lines will print this snippet out? Only one. </p> <p>Thus, <strong>every one</strong> script variable should be initialized before use.</p> <p>A special matter is <strong><a href="http://php.net/manual/en/language.variables.external.php" rel="nofollow">outside variables</a></strong>.<br> A programmer is unable to control it's behavior. And sometimes existence of a outside variable is a case of application logic. For example:</p> <pre><code>if (isset($_GET['id'])) { //displays particular article } else { //displays list of articles } </code></pre> <p>It's allowed to check this way.<br> But usage of raw outside variables should be strictly limited.<br> For example, if you're gonna to populate form fields with previously entered values, it's recommended to create another variable, say, $FORM, and initialize it's items with values, properly prepared from raw outside variable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload