how to pass a variable in php include, i am new in php
This is what I was trying to do
include_once 'task_include.php?id=pot';
get an error like this
Warning: include_once(task_include.php?id=pot): failed to open stream: No such file or directory
There is no way to pass variable in the include_once.
There is no way to pass variable in the include_once.
Why don't u perform get and post methods.
use form action.
include loads a local php file an "pastes" the content into the actual scope....
it isn't that nice, but for your case you could do this
$id="pot";
include_once "task_include.php";
inside task_include.php there will be the global variable $id (not $_POST['id'] or something).
it isn't that nice, but i think, it would fit your needs
Related
Suppose, I have an index.php file like this:
<?php
$id=2;
include('php/config.php');
require_once "php/variables.php";
?>
Is there any way to pass the variable $id via url parameter to config.php and variables.php files so that I can perform some tasks based on the variable after getting it
by $_GET['id']?
You can use $id in both file, because you are already including those two file in your current file so you can able to access $id, try to print echo $id; in both files and you can see the value.
Yes, You can directly access $id in both files as saied by Karthik.
Or you can use global variables
include('php/config.php');
require_once "php/variables.php";
The include and require_once functions, take a filesystem path by default; not a URL. Consequently "URL parameters" (and the corresponding $_GET superglobal) are irrelevant here, because you are not passing a URL.
(Yes, you can pass a URL to these functions if you have the appropriate fopen wrappers set and this will trigger an HTTP request - but this is most probably not what you want to do here.)
The include and require_once functions include the referenced document in-place and therefore inherit the current scope. Consequently the $id variable will be available as-is to these included scripts (as the other answers have already pointed out).
I'm using require_once to call a script that will echo back HTML for a the footer of each page. I have each page submitting a variable in the URL for the PHP file to retrieve via $_GET, but because it doesn't see a file with the name of the full URL (including variable) it isn't working. Any work arounds here?
ERROR:
Warning: require_once(footer/footer.php?page=homepage) [function.require-once]: failed to open stream: No such file or directory in /home/content/05/10838405/html/index.php on line 434
If I remove the '?page=homepage' it works perfectly.
Just use a variable
$page = 'homepage';
require_once 'footer/footer.php';
Then in our footer.php you can use $page
GET parameters are a part of HTTP URLs – but the local file system of your server does “speak” HTTP.
So footer/footer.php?page=homepage would literally mean a file of that name (which does not exist, and the characters ? and = are “illegal” as parts of a file/directory name in many file systems).
Since you are asking for a workaround – two easy possibilities (in both cases, only requiring the file by it’s actual name, footer/footer.php, of course):
you re-write the code inside that file to use a “normal” variable to decide what it should output – $page. (Although that might lead to problems with the variable scope, if $_GET['page'] is used inside a function in that file.)
In your script that does the including, you set the GET parameter yourself – $_GET['page'] = 'homepage'; before including the file. This is perfectly legal in PHP, from a syntactic standpoint – semantically, one might see that differently; and also it might lead to problems if in other code further down the line that GET parameter gets evaluated as well, and the value homepage is not expected in that context.
require_once is a local include, its not going via a http request so things like query strings won't work.
do something like:
$page = 'homepage';
require 'footer.php'
$page will be available to footer.php
Edit: Beaten like a ginger stepchild
Additional Reading: PHP Manual: Require Once
The reason you are having this problem is because require_once will look for a file called 'footer.php?page=homepage' which doesn't exist on your filesystem. Romainberger's solution is good.
I am trying to do an include(dirname(FILE), '/../file.php?variable=VAR'), but it is unable to find the location when I use the variable.
The directory is located one above the current one where the file is. Is it possible to pass the variables through the include?
You can't, though you won't need this at all, because $_GET's scope is global [i.e. accessible from every script you run].
Just to be clear:
include($path) searches the filesystem for a resource, then processes it.
http://somesite.com/index.php?key=value is a URL, whose part after the ? sign is called query.
No.
By using include, you include the source code from the file as it is. I guess you want to define a function inside the included file and call it to get the result.
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