Romulus Romulus - 2 years ago 87
PHP Question

PHP display $variable in HTML before it is defined

I have a webpage called search.php

I want it to have a title/tab tag that looks like this:

<title><?php echo $number_count; ?> items found - Mysearch </title>

But the variable $number_count is 0 until later php scripts are called to query the database and display the items one at a time. At the end of the HTML I can easily display
<p> <?php echo $number_count; echo "items found" ?> </p>
and it works with the correct count.

Answer Source

It must be defined first.
Best (and only in this case) approach to do it is to do calculations first, then generate website output.


You can do it like that:


$count = 125;

?><!DOCTYPE html>

<title>Title count: <?php echo $count ?></title>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download