PHP pass variable to include from the include call
You can define a platform variable before the include and the included file will have access to it:
index.php
<?php
$platform = 'desktop';
include 'contact_form.php';
?>
contact_form.php
Platform is: <?php echo $platform; ?>
<input id="name_<?php echo $platform; ?>" type="text">
<input id="email<?php echo $platform; ?>" type="email">
// EDIT
Since you specifically mentioned "at the include call" I thought I would mention that it could be possible to do:
include 'http://example.com/contact_form.php?platform=desktop'
But if you wanted to dynamically change the platform you'd still need (1) to define a platform variable and (2) using this remote format requires allow_url_include enabled. You can read more about this at the manual page for include.
Passing a variable from one php include file to another: global vs. not
The parent file has access to variables in both included files
When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php
. That is how PHP works with includes.
So, in your example, since you've set a variable called $name
in your front.inc
file, and then included both front.inc
and end.inc
in your index.php
, you will be able to echo
the variable $name
anywhere after the include
of front.inc
within your index.php
. Again, PHP processes your index.php
as if the code from the two files you are including are part of the file.
The included file doesn't have access to the other included file
When you place an echo
within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.
In other words, to do the behavior you're expecting, you will need to define it as a global.
How to pass arguments to an included file?
index.php:
<?php
$my_header = 'PHP Pass Variable to IncludePHP Pass Variable to Includeaaaa';
include 'header.php';
?>
and header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="favicon.ico" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content=" <?php echo $my_header ?> " />
<meta name="Description" content=" <?php echo $my_header ?> " />
<title> <?php echo $my_header ?> </title>
<link rel="stylesheet" type="text/css" href="reset.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"></script>
</head>
It's not an ideal solution, but I understand it's your first steps in php.
PS. Your Doctype doesn't match the code. I've adjusted your header html to be XHTML.
Passing variables to the same include that appears multiple times
You can create data that you can use over and over again by using the following function.
function get_user_card($params = array()){
$output = '';
extract($params);
if( isset($user_avatar) && isset($user_username) && isset($user_name) ) {
ob_start();
?>
<div class="user-card">
<img src="<?php echo $user_avatar; ?>" alt="">
<p class="user-card__username"><?php echo $user_username; ?></p>
<p class="user-card__name"><?php echo $user_name; ?></p>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
}
return $output;
}
$user_1 = get_user_card(
array(
"user_avatar" => "user-1-avatar.jpg",
"user_username" => "username-1",
"user_name" => "User 1"
)
);
echo $user_1;
how to pass a variable through the require() or include() function of php?
require()
and include()
will open the file corresponding to the path/name they receive.
Which means that, with your code, you would have to have a file called diggstyle_code.php?page=1
on your disk. That's obviously not the case, so it fails.
Quoting the Variable scope page of the PHP Manual:
The scope of a variable is the context within which it is defined. For the most part all PHP variables only have a single scope. This single scope spans included and required files as well.
In your case, you don't need to pass the variable. If you have a variable in your current script, it will also exist in the script you include, outside of functions, which have their own scope.
In your main script, you should have:
$page_no = 10;
require 'diggstyle_code.php';
And in diggstyle_code.php
:
echo $page_no;
// Or work with $page_no the way you have to
Remember that including/requiring a file is exactly the same as copy-pasting its content at the line it's required.
volt : pass variable to include from parent template
This code are ok, I test it and get correct name in the view:
I have the partial like you:
And finally I get this in my view:
Passing variables to another page with include
Your "footer" file exists only inside the getThemePart function. As such, it does not "know" anything that was declared outside.
You will notice that even the {$theme}
object is actually undefined (you might make it work by using $this
instead).
What you need to do is pass some kind of reference to the "world" or environment that the theme element needs to know (this would be a sort of Dependency Injection):
$env = [ 'lang' => 'en', ... ];
You can store it inside the theme:
private $env;
public function setEnv($env) {
$this->env = $env;
}
Now if you initialize the theme with $theme->setEnv($env)
, inside the includes you will be able to call {$this->env['lang']}
. Or you can build an ancillary function, or maybe even better, declare a magic getter that would allow you to write $this->lang
.
Personally, I usually prefer a different approach and replace sequences like {{name}}
with the content of $this->env[name]
using preg_replace_callback()
.
How to pass variable to another page without include
Session is what you are looking for. A session variable can store a value and use this value on all pages of your project.
First thing to do is to start session on each file on your project. You can do this like this example
<?php
session_start(); //declare you are starting a session
$_SESSION['newspaper'] = "New York Times"; //Assign a value to the newspaper session
?>
On the other file you can use the value of the session by trying something like this
<?php
session_start(); //always start session don't forget!!
echo $_SESSION['newspaper'];
// This will echo New York Times
?>
Related Topics
How to Tell If a File Is Text Using PHP
Posting Multidimensional Array with PHP and Curl
Can You Put PHP Inside PHP with Echo
Bcrypt and Randomly Generated Salts
How to Convert String to Boolean PHP
PHP MySQL Update If Exist or Insert If Not
Fatal Error: Class 'Ziparchive' Not Found In
Simplest Way to Increment a Date in PHP
Convert All Types of Smart Quotes with PHP
PHP Flush/Ob_Flush Not Working
500 Internal Server Error, How to Debug
PHP Pdo Prepare Repetitive Variables
PHP - Secure Member-Only Pages with a Login System
How to Validate a Credit Card in PHP
How Send Message Facebook Friend Through Graph API Using Accessstoken
Passing a Variable from One PHP Include File to Another: Global VS. Not