I am wondering if there is a way to pass a variable to a file you are including via include()?
I tried this but got an error:
include("header_alt.php?img=hey");
Is there a way to do that?
Thanks!
Just define your variable in the first file ; for instance, in temp.php :
<?php
$my_var = 10;
include 'temp-2.php';
die;
?>
And use it in the second file ; temp-2.php :
<?php
var_dump($my_var);
?>
And it should work : I'm getting this output, from temp-2.php :
int 10
The query-string syntax, using stuff like ?img=hey is used when you are requesting some data from a distant server (like when you are using your browser to surf on websites), not when including a file that is on the same server.
Smarty provides a really good mechanism for this sort of thing. Plus, using Smarty just makes for better php applications.
http://www.smarty.net/manual/en/language.function.include.php
Variables can be passed to included
templates as attributes. Any variables
explicitly passed to an included
template are only available within the
scope of the included file. Attribute
variables override current template
variables, in the case when they are
named the same.
All assigned variable values are
restored after the scope of the
included template is left. This means
you can use all variables from the
including template inside the included
template. But changes to variables
inside the included template are not
visible inside the including template
after the {include} statement.
Related
I'm modifying a web app (care2x) which is uses smarty for templating.
as you all know smarty uses two files .php for logic code and .tpl for template.
In .php file some php variables are used to assign to smarty variables
for example
$this->smarty->assign('template-variable',$php-variable);
I can't figure out where $php-variable gets its value from?
is it same as passing values from controller to view in ci?
Any help is much appreciated.
I mean that $php-variable is variable from PHP because in variable names you cannot use - sign.
Those variables of course come simple from PHP, so what you should do is to search simply in PHP files this variable. They can be defined in the same file or in other files depending on system architecture.
The easiest way of course is when variable is defined just before assigning its value to Smarty as following
$phpVar = 2;
$this->smarty->assign('template-variable',$phpVar);
but in case it's not, you should probably use your editor or for example Total Commander to find this variable.
Without any more details and full code it's hard to say where it can be defined.
I am in a situation .
My php folder structure is like
UI
user
login.php
logout.php
jquery
somejs
css
somecss
blah.php
blah.php
Now to import any css ,js or any php file i am using the file path like
localhost/UI/user/index.php // example
Now i am trying to define a global variable on any page like
<?php
$somevar = "localhost";
GLOBAL $somevar;
?>
So that i could import any css js like
<?php echo $somevar ;?>/UI/user/index.php // example
Problem : It is working on that page only where i declared the variable as GLOBAL
I want to use the variable on each page and don't want to use include
Is there any other alternative to define a variable for files folder in php ?
You do not declare variable with global. You just make it available within you method or function body even it was set (or using your terminology "declared") outside of it. So there's no way to have the variable unless it is declared. And there's no way to do that without running the PHP code (simplification, but it does not matter here). And code is not coming from nowhere, hence the need of include or require of said code that sets variable.
You may try to use php.ini's auto_include_file to have your variables auto-included, but still, the PHP code needs to be used for that.
But you generally doing it wrong. move all global variables into class, set autoloader and access i.e. statically. The code will be much cleaner.
Well, my experience says .. global is used for accesing variables inside the function those which defined outside the function.
There can be many different solutions to the same, one i will suggest is use sessions or cookies. Store the data in session / cookies and access it across wherever required.
You have to use define() function for this purpose
config.php
<?php
define("host", "localhost");
?>
now include this page wherever you want to access HOST variable.
you can access this variable like this echo host;
You can use the special PHP-defined $GLOBALS array. The $GLOBALS array is an associative array with the name of the global variable being the key and the contents of that variable being the value of the array element. Notice how $GLOBALS exists in any scope, this is because $GLOBALS is a superglobal
You may not need to define a global variable to apply the css, js or any static files viz Images etc to your application. you may use relative path to include js and css files. You can refer to this article to know more about relative and absolute path.
This is a newbie question, and I know it.
Template structure is your usual index.php, with a few require_once()'s for the header/footer etc.
I define a var at the top of index.php before any of the require_once()'s for the base url, such as $url = 'http://url';
I then want to echo this out into all template files, header/index/footer etc, it works inside index.php as expected, but fails with a undefined var in all template files that are included in.
I know it's a var scope issue, but I'm totally perplexed how to fix it.
I'm aware that the manual says vars are available to included files, however they aren't. Could it be a issue with my local PHP install?
edit : Created a couple of test files, and a var is defined between 2 files, so why are they not working on my main site files?
Any helps gracefully recieved.
Many Thanks
if you use functions or methods (functions in classes) then you need to do global $variable inside the function. Otherwise you will not have access to it, you also could define it as constant. A constant is always global.
define('MYURL', $url);
You might want to use a PHP framework, if you not already do so.
If you use POST to send FORM data to a PHP page, are the POST values available to use in all the PHP pages that are INCLUDED in the POST TO with the PHP INCLUDE?
Example in the POSTED TO PAGE:
<?php include 'otherpage.php'; ?>
Would I be able to use the POST value that was sent to the 'POSTED TO Page' in 'otherpage.php'?
Yes
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.
PHP.net
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. So if you can access POST at the same line of your include then yes.
Yes, actually you should be able to access these variables.
On one of my pages I have a require_once('../path/to/url/page.php'); which works with no problems. The moment I add a query string require_once('../path/to/url/page.php?var=test'); it won't include the file anymore. It's just blank. Anyone have any ideas of why? Can you not use a query-string in a require?
Thanks,
Ryan
By using require_once('../path/to/url/page.php?var=test');, php will not make a new request to page.php, it will actually search for the file named page.php?var=test and include it, because in unix, you are allowed to have such a filename. If you want to pass a variable to that script, just define it: $var="test" and it will be available for use in that script.
require loads a File (from a file path) to include. It does not request that file through apache (or other webserver), therefore you cannot pass query strings in this way.
If you need to pass data into the file, you can simply define a standard php variable.
Example
<?php $a_variable = "data"; require_once('../path/to/url/page.php'); ?>
Note, the variable must be set before the include/require is called, otherwise it won't be available.
All answes true. But most importantly: since $_GET is a global, it's present' in all included files as well, so there's absolutely no use in passing those parameters with the include.
require only accepts paths it would be pointless to add any request since it doesn't make any
it simple includes the required code into the current one