In header.php, there is a variable $title defined in the same location (depending on the URL of the page). In one of the inner pages, I want to override $title since it is taken from the database there. For example: in header.php there is
if (...) {$title = "Section 1";}
Then echo $title
In inner.php-
include(header.php); ... SELECT ..... $title = "Page 1";
Of course this won't work. Tried using globals with no success. How to override $title variable in inner.php (header.php)? Thanks for the advice, I don't know much about PHP.
Tried using globals or even functions, but not sure
P粉2040797432024-01-11 00:18:23
Computers will do what you tell them in the order you tell them. If you say this:
The computer will perform each step in sequence. In step 2 it will read "Part 1" and in step 3 there is no way to make it "unsay" and say something different.
This is basically what your current code is doing, with a few extra steps in between:
Step 5 cannot affect step 3; this has already happened.
The usual way to avoid this is to divide the program into two stages: the first stage Prepare the data , get the data from the database and make decisions about the page title and so on; the second stage Display data and make decisions based only on acquired data.
So, in your case, you can split the data part of "header.php" into separate "startup.php" with the following steps:
Titles in headers are now displayed after all logic has been run, and can be displayed as "Page 1" instead of "Section 1".