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
Related
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.
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
I am trying to reach the contents of a PHP file without the file actually outputting what it would usually do. Here is my test code:
File1 (test1.php)
<?php
ob_start();
include_once './test2.php';
$test = ob_get_contents();
echo $test;
?>
and here is file2 (test2.php)
<?php
$testVar = 'Name!';
?>
<div class="testClass"><?php echo $testVar?></div>
<p>Spam2</p>
and I want it to only do this because of the
echo $test
line NOT because the file is outputting the content.
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
due to the echo, but it returns this
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
<p>Spam2</p><div class="testClass">Name!</div>
<p>Spam2</p></body>
So how do I get it to only return the content once?
Don't echo $test;. PHP is executing as it should. Since ./test2.php shows Spam in HTML it appears on the page, then you assign the page contents to a variable and echo it. What do you expect?
If you have 2 files say: app/index.php and app/config.php you can just use the return keyword to return some content from the config.php file. And then, when you include the file whatever you returned from config.php can be saved to a variable.
Example:
First return whatever you want from the config.php file (could be an array, string, etc).
<?php
return ['name' => 'Spam'];
Then in the index.php:
<?php
$contents = include_once('config.php');
echo $contents;
How can I get expected output from example below?
Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.
content.php
<p>Hello <?php echo 'World!'; ?></p>
reader.php
<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>
Output of code above is:
Message from another file: Hello <?php echo 'World!'; ?>
Instead of (expected):
Message from another file: Hello World!
I think you are looking for <?php include('content.php');
file_get_contents — Reads entire file into a string
PHP.net file_get_contents - manual
The include statement includes and evaluates the specified file.
PHP.net include - manual
Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.
Example:
content.php
function get_content($world){
return '<p>Hello ' . $world . '</p>';
}
reader.php
<?php
require('content.php');
$content = get_content('world');
?>
<b>Message from another file:</b> <?php echo $content; ?>
Since you cannot use include (though I don't understand fully why), but want the file to be parsed and executed as PHP code, you can use eval
<b>Message from another file:</b> <?php eval(file_get_contents('content.php')); ?>
But the file content.php should not contain <?php and ?> tags, as stated at http://php.net/eval.
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.