Is it valid PHP to echo a non-existent variable?
I'm using this code in my individual pages to create a unique title and active tab.
<?php
$title = "Blanktree Design";
$home = "class=\"active\"";
include "header.php";
?>
And I'm using this code in my header.php to carry out these differences.
<title><?php echo $title; ?></title>
<ul id="nav">
<li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
<li <?php echo $about ?>><span>curious?</span><a href="/about">about</a></li>
<li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>
What I'm most curious of, since I'm brand new to PHP is... Is it valid to just echo a non-existent variable? I mean... it works and it doesn't error out. But I was just wondering if this is poor programming.
Thanks in advance.
RESOLVED
At the end of the day all the discussion is complete and this is what I've found to make the most sense in my specific scenario:
<?php
$title = "Blanktree Design";
$home = "class=\"active\"";
$about = $contact = "";
include "header.php";
?>
And calling the variables the same as before:
<ul id="nav">
<li <?php echo $home ?>><span>begin</span><a href="/">home</a></li>
<li <?php echo $about ?开发者_StackOverflow中文版>><span>curious?</span><a href="/about">about</a></li>
<li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>
Yes, it's always poor programming to echo (and any other usage) a non-existent variable.
And it's actually errors out - you just doesn't see it due to wrong PHP settings.
error_reporting(E_ALL);
at the top of the script will show ye these errors.
Although PHP scarcely allow usage of undefined variable, a good programming practice is to define every variable in the code before it's usage.
It will let you to avoid numerous mistakes and unexpected behaviors or even serious vulnerabilities.
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?
Of course not.
A real intention of this warning (as any other warning) is to let a programmer know that something is going wrong.
It's very easy really: PHP just tells you "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."
But by using isset() we just gag this warning and nothing more. Doing a disservice to youself.
Moreover, by initializing a variable, one can be sure of both variable existence and it's value.
a little example:
<?
while ($i < 10) {
echo $i." ";
}
echo "<br>";
while ($i < 10) {
echo $i." ";
}
?>
How many lines will print this snippet out? Only one.
Thus, every one script variable should be initialized before use.
A special matter is outside variables.
A programmer is unable to control it's behavior. And sometimes existence of a outside variable is a case of application logic. For example:
if (isset($_GET['id'])) {
//displays particular article
} else {
//displays list of articles
}
It's allowed to check this way.
But usage of raw outside variables should be strictly limited.
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.
Better programming practice would be to check if it's set first. Such as
if (isset($variable))
echo $variable;
It will throw a warning, see PHP: printing undefined variables without warning, but whether or not you see it depends on your level of error reporting.
精彩评论