Romulus Romulus - 6 months ago 23
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:

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


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

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

EDIT:

You can do it like that:

<?php 

$count = 125;

?><!DOCTYPE html>
....

<title>Title count: <?php echo $count ?></title>
....