Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass a variable through the require() or include() function of php?

Tags:

php

when I use this:

require("diggstyle_code.php?page=$page_no");

the warning is :failed to open stream: No error in C:\xampp\htdocs\4ajax\gallery_core.php on line 198

and the error is:

Failed opening required 'diggstyle_code.php?page=1' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\4ajax\gallery_core.php on line 198

value of the variable $page_no is collected beforehand.

But if I omit the '?page=$page_no part' from the argument of the require function, then no error or warning is shown.

I need to pass the variable when I use the require() function.

like image 564
sof_user Avatar asked Feb 26 '11 16:02

sof_user


People also ask

What are the difference between include () and require () file in PHP?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

How do you pass a variable to another function in PHP?

You need to instantiate (create) $newVar outside of the function first. Then it will be view-able by your other function. You see, scope determines what objects can be seen other objects. If you create a variable within a function, it will only be usable from within that function.

What is the main difference between require () and require_once () in PHP?

The basic difference between require and require_once is require_once will check whether the file is already included or not if it is already included then it won't include the file whereas the require function will include the file irrespective of whether file is already included or not.


3 Answers

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.

like image 182
Pascal MARTIN Avatar answered Sep 18 '22 10:09

Pascal MARTIN


this should work, but it's quite a dirty hack:

$_GET['page'] = $page_no;
require('diggstyle_code.php');

you probably want to refactor your code to use functions and/or objects and call them from your files instead of including them (spaghetti code alert)

like image 40
knittl Avatar answered Sep 21 '22 10:09

knittl


require doesn't pull the file from the web server - it should refer to a file on the filesystem instead.

Calling include or require just tells PHP to paste the contents of the given file in your code at this place, nothing more than that.

like image 33
Blagovest Buyukliev Avatar answered Sep 20 '22 10:09

Blagovest Buyukliev