PHP using a variable from before it's defined - php

I have a file named page.php and it is something like this;
<? include "header.php"; ?>
<body>
...
</body>
<? include "header.php"; ?>
Between the <body> tags, I run a sql query and get a variable called as $title.
I want to show that variable as the html title of my page which is in the header.php .
How can I do this?
I simply tried what i said, but it didn't work.

Run it before the <body> tags, save the results to a variable, and process this variable wherever you want (as a title and in the <body> tags).

You should try to avoid mixing PHP code with your HTML. Run the SQL query before you include header.php, store the results in an variable, and store the title in a variable. For example:
<?php
// SQL code here, store the data in e.g. $data and title in $title
include "header.php";
?>
<body>
<?php
// output page using $data
?>
</body>
<?php include "header.php"; ?>
Then your header.php would include something like <title><?php echo $title; ?></title>

Related

How to change printed variable in php?

I put a sub file in the main file, but the sub file affects the variables in the main file.
Simple example:
In my code include can not be before echo.
Index.php
<?php
$heading= "Heading";
echo "<h1>$headeing</h1>";
include_once('specific_data.php');
?>
Specific_data.php
$heading = "Specific Heading";
Real output:
<h1>Heading</h1>
Desired output:
<h1>Specific Heading</h1>
You echo the value before include os before you assign the new value
try move the echo after the include
<?php
$heading= "Heading";
include_once('specific_data.php');
echo "<h1>$headeing</h1>";
?>
U can reate MVC stracture
For example
index.php
include_once 'view.php';
$heading='custom head';
return view('specific_data.php',compact('heading'));
view.php
function view($file,$vars=[])
{
extract($vars);
include_once $file.'.php';
}
specific_data.php
<h1>this variable get from index and will render <?=$heading?></h1>
Exec index.php from browser you will see following code
this variable get from index and will render custom head

How to declare $title in my html code?

So I have header.php file with my header and page1.php, page2.php, page3.php with my content. Every page starts from
<?php include_once("header.php") ?>
But title is always static in my code. I tried to add <title><?php $title_value ?></title> to my header.php, and
<?php $title="Contacts"; include_once("header.php") ?>
to my page1.php but title is invisible. It seems like $title didn't exist. How can I fix it and add different titles to my pages?
<?php $title_value ?> does nothing. You need to echo/print the value in the header.php.
<title><?php echo $title_value; ?></title>
In header.php you also should include a check incase you forget a title in a file.
$title_value = empty($title_value) ? 'Default Title' : $title_value;
You only declared the variable.
You didnt print or echo it.
Use:
<?= $title; ?>
Or:
<?php echo $title; ?>
Or:
<?php print $title; ?>
first you have to echo the value of the title like that:
<?php echo($my_var) ;?>
// or
<?=$my_var?>
after to change the title of your page u can pass a $_GET param to your html title value ( not the better way but the easier )
ex:
// on the fist page
<a href="/myurl?title=page2">
// on the second page
<?php echo($_GET['title']) ;?>

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.
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>
....

Is it possible to echo a variable which is declared below the echo in PHP?

The below code will not display any output as the variable is declared below the echo as PHP gets executed line by line. Is there any way to search for the variable in the whole page and then execute the code?
<?php
include "header.php";
$title = "Test";
?>
header.php
<html>
<head>
<title><? echo $title ?></title>
</head>
You need to learn how compilers/interpreters works. PHP is interpreted language and The binary that lets you interpret PHP is compiled.
PHP run from top to bottom.
so its like
<?php // start from here
echo "$title"; <-- $title is undefined here
$title = "Test"; <-- now you declared $title with value so it goes in memory now
//end
So you need to first check weather $title is set or not than respond according to it
if(isset($title)){
echo $title;
}
According to your logic, I suggest you to use contants like below:
Create a separate file, let's say constant.php and include it on all other pages
<?
define("TITLE", "This is title");
?>
Use it like below:
<?php echo TITLE;?>
Thanks

How can I access a variable in one block of PHP code in another block of PHP code in the same file?

I have something like this:
PHP code at the start:
<?php
$variable="example";
?>
Then HTML code:
<html>
<head>
<title>Title</title>
</head>
<body>
Then again PHP:
<?php
// code comes here, and I want to access variable $variable here.
?>
And then HTML code ends:
</body>
</html>
Is it possible to do this somehow? I don't want to create another file; I need to do this in this file.
Not Required unless if you are accessing it under functions ( as it will lose their scope)
test1.php
<?php
$var = 1;
//.. your code...
?>
<html>.....
<?php
echo $var; // prints 1
whereas the below code won't work...
<?php
$var = 1;
function displayVar()
{
echo $var; // You will get a notice .. !
}
Just do what you stated above and it will work.
<?php
$variable = 'Hello';
?>
<html>
<head>
</head>
<body>
<?php echo $variable; ?>
</body>
</html>
The above example will display a simple webpage with 'Hello' as the content. This is one of best strength of PHP actually.
try this
echo ($variable);
or
print($variable);
If it is the same file, yes it is possible, unless the variable is in a function. But this is a very simple question, that you could have tested yourself.

Categories