php variable changes - php

FIXED!
I have 2 php pages that both define the variable "title" as something different. However, echoing the variable on both pages results in the first page's variable's value being displayed on both pages. Any idea why and how I could get the variable to change for each page?
first php page:
<?php
$title = "Posts";
echo $title;
?>
This displays "Posts".
second php page:
<?php
$title = "New Posts";
echo $title;
?>
This also for some reason displays "Posts". Shouldn't this page display "New Posts"?

If you are including the second page on the first page before you define $title on the first page, then the included value will be overwritten.
Are all of your variables defined in the global namespace? If so, this problem will be inevitable when you're including PHP files within other PHP files.
You could resolve the problem by properly encapsulating your variables within a class or namespace; for example:
In file one:
<?php
namespace included;
$title = "original title!";
?>
And in file two:
<?php
namespace including;
require_once "file_one.php";
$title = "new title!";
echo \included\$title;
echo \including\$title;
echo $title;
?>
Which will display:
original title!
new title!
new title!

Related

insert content into Template through Variable

I want to use my index.php page as my template for all my other pages. So I'm printing it out with the code below.
echo file_get_contents("index.php");
I've added this piece of code into the template (index.php) where i want to display the contents. of whichever page im on.
<?php
echo $index_content;
?>
So when I use
echo file_get_contents("index.php");
to get my page template, on for example users.php. In the users.php file I want to use the code below
$index_content = echo "string";
to then print out my page contents where I added this variable
<?php
echo $index_content;
?>
My problem is when I say $index_contents = echo ("string");
it's not printing anything out. onto my template. or it prints the stuff out but at the end or the beginning of the template. not where i've inserted my variable. Why wont it echo out my stuff where I've inserted my variable.
file_get_contents() give you the source of your file.
If I get you right you want to use include instead. Also don't echo in a variable but assign the value and echo it in the template.
users.php:
$content = 'what ever';
include 'template.php';
other.php:
$content = 'other page';
include 'template.php';
template.php:
echo $content
If you call users.php output will be "what ever". If you call other.php output will be "other page".
You are storing the return value of "echo" in $index_content, which is empty.
Just omit the echo when assigning the string to the variable.
The other problem is, with file_get_contents you don't evaluate the php expression where you echo out the $index_content.
Instead, you should use include('index.php') in users.php, and set the variable $index_contents before that.

defining page titles specifically

I'm trying to echo out dynamic php titles depends on page for seo purposes.
I successfully did this the pages I call from database depends on their id's.
Like that:
if (isset($_GET["category_id"])) {
$query = $handler->query("SELECT * FROM categories WHERE category_id = ".$_GET['category_id']." ");
while($r = $query->fetch()) {
$title = $r["title"];
}
}
And this is how I echo out:
<title><?php if (isset($_GET["category_id"])) { echo $title; echo " |"; } ?> mypage.com</title>
result:
on category.php?category_id=1 Page title is: "Category 1 | mypage.com"
*
But there are pages which is not static.
for example: index.php, login.php.
*
I want to figure out how to edit my code below to print "Login" on login.php between title tags.
<title>
<?php
if (isset($_GET["category_id"])) {
echo $title; echo " |";
}
?> mypage.com
</title>
EDIT
my login.php is like that:
include("header.php");
content.
So I need to define $title for login.php in header.php
I need to add some codes to header.php when user will see different title on login.php, index.php etc.
I'm able to do it category.php?category?id=1 already with the code above, but I need to also make it for login.php, index.php and so on.
There are several ways to do this within the code you outlined.
First, I'm going to simplify some of your code a bit. This also potentially makes it slightly faster:
echo "<title>$title | mypage.com</title>";
This assumes that $title is going to be set, either by the query from when $_GET['category_id'] is set, or from the file that calls it. The great thing about includes is that they can pass variables. So in the login.php and any other file where you are not doing a GET, just specify $title in that file.
Login.php:
$title = 'Login';
include("header.php");
content.
Which would display page title of "Login | mypage.com".

useing $page_title within a echo

I want this to work, it doesn't currently, so, if its possible, what do I need to change:
<?php echo $page_title_LEADER; ?>
In the config file I have
define('ENGLAND_LEADER', 'Bob Smith:');
define('SPAIN_LEADER', 'Stan Smith:');
which when I use:
<?php echo ENGLAND_LEADER; ?>
works fine as you would expect, what I'm trying to do is use the page title to auto fill the COUNTRY name part of COUNTRY_LEADER, so I don't have to manually change the name of the country each time.
NB I do have the $page_title set in the page
You can use the constant function for this
<?php echo constant($page_title . '_LEADER') ?>

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

in PHP, Alter the value of a variable from an included file before the value is computed

I currently have a header.php page which stores all the styles and positions of my header and layout. I then have a contactus.php page which includes the header.php page.
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
I want to alter the style of the link that the person has clicked, so that it pops out indicating to the person which page they are on. Currently, when I try altering the value of $classnamehere inside of contactus.php (by simply assigning it a new value) to change the class printed inside the <a href> tag, nothing happens. It executes the include command, and outputs the value that was stated inside header.php, ignoring my attempts to change the value on the new page.
Is there any way to change the value within contactus.php only, while still keeping the initial value inside header.php so that all the other pages can still use the 'default' style?
Edit: Inside contactus.php, can I change the value of a variable obtained (included) from header.php without actually 'changing' the global variable?
Looking at your code:
-----CONTACTUS.PHP:
<?php
include 'header.php'
$classnamehere='"linkStyleTwo"' //Here is where I want to update the value
?>
The main links at the top of my page are styled in a certain way using:
------HEADER.PHP:
<?php
$classnamehere= '"linkStyleOne"' ?>
<a href="http://www.google.com" class= <?php echo $classnamehere ; ?>......
your code does the following:
contactus.php is executed.
header.php is included, setting the $classnamehere to
'"linkStyleOne"' and creating the actual link with classname
linkStyleOne.
After that $classnamehere is set to linkStyleTwo
This means that you have to assign classname BEFORE including the header.php.
Instead of including it in contactus.php you could do the logic within header.php:
<?php
if ($currentPage) == 'contact') {
//Set this class when user is on a specific page
$classnamehere='"linkStyleTwo"';
}
else {
$classnamehere= '"linkStyleOne"';
}
?>
and just do this in contactus.php
<?php
$currentPage = 'contact';
include 'header.php'
?>
change this
class= <?php echo '"$classnamehere"' ; ?>.
to
class= "<?php echo $classnamehere ; ?>"
Or use it this way
<?php echo '"', $classnamehere ,'"' ; ?>
Or this way(im not sure this works)
<?php echo '"{$classnamehere}"' ; ?>
If you include code, all variables within included code are global and therefore all chnages are reflected in all parts.
in header.php
$a = 10;
in contactus.php
include "header.php"
$a = 0;
And there is no way of changing $a only inside contactus. All code after $a change will be using the new value.
You can create temporary variable $a_copy inside contactus and then change this variable:
$a_copy = $a;
....class= <?php echo '"$a_copy"'...
You should create a PHP class and include it in your header, or make it your header. Invoke it on the page then assign the value for the class variable which you can then echo a separate class function which uses the variable values you require. I can provide an example if you're unfamiliar with the process.
Header.php simple example:
<?php
class Template
{
private $active_nav;
private __construct()
{
}
public function set_active($page = '') // set page test value
{
$this->active_nav = $page;
}
public function get_active() // return page test value
{
return $this->active_nav;
}
public function nav_links() // this could be changed to return an entire header or part or whatever -- change the scope accordingly
{
// active link CSS is linSktyleOne
$current_page = $this->get_active();
$nav_links = '<ul>';
$nav_links .= '<li>Index</li>';
$nav_links .= '<li>Contact</li>';
$nav_links .= '</ul>';
return $nav_links;
}
}
contact.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('contact');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
index.php simple example:
<?php
require_once('includes/header.php');
$template = new Template;
$template->set_active('index');
// put all your html or other code here
echo $template->nav_links();
?> <!-- blah blah code finished --> <?
// you can always echo a footer out here through a function in the template as well
Does all that help make more sense? You only add 1 line of code to each page, and if you really wanted to get fancy you could remove the set function invocation on each page by using a substring of the$_SERVER['REQUEST_URI'] value for a conditional test and put the condition to set the value in the constructor of the class.

Categories