Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it valid PHP to echo a non-existent variable?
    text
    copied!<p>I'm using this code in my individual pages to create a unique title and active tab.</p> <pre><code>&lt;?php $title = "Blanktree Design"; $home = "class=\"active\""; include "header.php"; ?&gt; </code></pre> <p>And I'm using this code in my header.php to carry out these differences.</p> <pre><code>&lt;title&gt;&lt;?php echo $title; ?&gt;&lt;/title&gt; &lt;ul id="nav"&gt; &lt;li &lt;?php echo $home ?&gt;&gt;&lt;span&gt;begin&lt;/span&gt;&lt;a href="/"&gt;home&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php echo $about ?&gt;&gt;&lt;span&gt;curious?&lt;/span&gt;&lt;a href="/about"&gt;about&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php echo $contact ?&gt;&gt;&lt;span&gt;locate&lt;/span&gt;&lt;a href="/contact"&gt;contact&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>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.</p> <p>Thanks in advance.</p> <p><strong>RESOLVED</strong></p> <p>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:</p> <pre><code>&lt;?php $title = "Blanktree Design"; $home = "class=\"active\""; $about = $contact = ""; include "header.php"; ?&gt; </code></pre> <p>And calling the variables the same as before:</p> <pre><code>&lt;ul id="nav"&gt; &lt;li &lt;?php echo $home ?&gt;&gt;&lt;span&gt;begin&lt;/span&gt;&lt;a href="/"&gt;home&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php echo $about ?&gt;&gt;&lt;span&gt;curious?&lt;/span&gt;&lt;a href="/about"&gt;about&lt;/a&gt;&lt;/li&gt; &lt;li &lt;?php echo $contact ?&gt;&gt;&lt;span&gt;locate&lt;/span&gt;&lt;a href="/contact"&gt;contact&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre>
 

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