0

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']?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Yes, You can directly access $id in both files as saied by Karthik. Or you can use global variables

Comments

0
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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.