0

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 ?>><span>curious?</span><a href="/about">about</a></li>
   <li <?php echo $contact ?>><span>locate</span><a href="/contact">contact</a></li>
</ul>
  • Are you talking about `$about` and `$contact`? – glomad Mar 04 '11 at 01:30
  • 1
    Turn on Error Reporting. `Notice: Undefined variable: myvar`. Also, there are better methods of determining the active page. – drudge Mar 04 '11 at 01:31
  • Could you link to a better example of this or give a better example? This was the simplest thing I could think of, but then again... I'm new to PHP. – Robert McMahan Mar 04 '11 at 01:39

3 Answers3

4

Better programming practice would be to check if it's set first. Such as

if (isset($variable))
  echo $variable;
Michael Ridley
  • 10,378
  • 3
  • 22
  • 16
  • So this is a simple fix to it. I'll just add this to each. isset is just a simple function to see if the variable exists? Processing power on this is negligible I assume? – Robert McMahan Mar 04 '11 at 01:36
  • It is negligible overhead, yes. I'm not sure why Col Shrapnel says it's not better to check. If you're unsure if something is set, I think it's best to check. – Michael Ridley Mar 04 '11 at 01:40
  • @Robert don't you think it's quite odd to add a code with only purpose of fighting an error message? – Your Common Sense Mar 04 '11 at 01:46
  • @Michael: this is code smell if I've ever seen it. A developer should always know _exactly_ what variables are floating around. Not putting you down though... :) – Jonah Mar 04 '11 at 01:50
  • @Col. Shrapnel I would assume it would be best to not create a place for the error in the first place. This is the way I would usually go about JQuery or HTML coding, and I would assume the same basic rules apply to PHP. I was just unsure of best way to go about it. – Robert McMahan Mar 04 '11 at 01:57
  • @Jonah - I agree that it would be best to know what variables are defined but maybe in some cases you don't know like if it's $_REQUEST['whatever'] that may or may not be defined. In any case, I was just answering the original question about the best way to deal with scenarios where you suspect something is not defined :-) – Michael Ridley Mar 04 '11 at 02:00
  • @Michael: certainly, checking array indexes is definitely a valid use of isset(). But when it comes to variables, it's best to keep a stricter mindset in how you manage them. – Jonah Mar 04 '11 at 02:05
4

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.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • So in this instance it would be better to just assign blank values such as $home = ""; at the beginning of those pages where those variables aren't going to be used? – Robert McMahan Mar 04 '11 at 01:48
  • @Robert yes, it will make you sure that no unexpected value can be printed out. – Your Common Sense Mar 04 '11 at 02:08
  • 1
    @Robert and it will make this error message work for you! I was in search for the proper words and it seems I've finally found it. Such an error message is your friend, not foe. If you forget to set $contact variable - it will tell you! What can be more handy? but by using isset() you will just gag this warning and nothing more – Your Common Sense Mar 04 '11 at 02:13
  • Thank you for all your help in this. I really appreciate it :) Understanding is key! I've updated my initial post with the resolution. – Robert McMahan Mar 04 '11 at 02:16
2

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.

Community
  • 1
  • 1
Brandon Frohbieter
  • 17,563
  • 3
  • 40
  • 62