It is not possible to call a variable before it is declared. This is applicable to any programming language (as understood by the author himself; some information about variable declaration in PHP in the documentation here). What is done is the declaration of a variable (constant, function...), then its use.
Knowing this, there are several ways to solve the problem.
Tag manipulation <title></title>
javascript (!)
Perhaps the first that comes to mind is to use javascript for tag manipulation <title></title>
. However, what should be noted in this case is that Javascript is a language client-side
, in other words, it is interpreted alongside the "client". Thus, the system will suffer several damages if requested in other environments, in search engines (Google, Bing...) is an example of this. Soon, it becomes unviable.
<!-- template.php -->
<!doctype html>
<html lang="pt-br">
<head>
<title></title>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
<!-- home.php -->
<script>document.title = 'Home';</script>
Templates systems in PHP
Another would be the use of templates. Several are available in the market. I could quote the blade
of Laravel (example of version 5.6), which can also be applied to other projects. Examples: jenssegers/Lade, spatie/Laravel-Blade, and several others.
Use of your own template engine
For simple and fast handling, I recommend using friendly urls with .htaccess
, and thus doing the url processing and, finally, the inclusion of the template that includes the file php
.
Explanation:
# exemplo de estrutura simples de arquivos
├- .htaccess
├- templates/
└- template.php
├- views/
└- home/
└- home.php
├- controllers/
└- HomeController.php
└- App
└- Core.php
O . htaccess:
This file will be responsible for handling the url. In it you will handle the url and finally have the name of the class and method you should call. I have separated here a good question (here at stackoverflow) regarding.
The App Core.php class:
This class will receive the information contained in the url and finally should reach the file controllers/HomeController.php
(or other, according to the url), and finally, this, will be responsible for setting the variables, both for templates/template.php
, how much to views/home/home.php
(or other files, again, depending on the url).
There could be a method similar to:
public function index() {
$title = 'Bem vindo!';
return $this->view(array('view'=>'views/home/home.php', 'title'=>$title));
}
Finally, the method of view
search the file templates/template.php
and within this there will be a include for views/home/home.php
(as called by HomeController::index
). The template file could look like:
...
<title><?php echo "{$data['title']} - Meu site"; ?>
</head>
<body>
<?php require_once($data['view']) ;?>
...
Of course! Making the appropriate adaptations.
What are you trying to do necessarily? Could you go a little deeper? Abs
– Guilherme Agostinelli
Your question is unclear because it is so general. Have some concrete example that you want to make work?
– Sergio
Check out the issues I made above in the question...
– Tiago Boeing
Rewrite the code and use output_flush solves? I don’t program in PHP, I just imagine this.
– Gustavo Rodrigues
I’ve tried before and it doesn’t work for what I tested...
– Tiago Boeing