Asked  7 Months ago    Answers:  5   Viewed   54 times

I'm trying to pass a variable from one include file to another. This is NOT working unless I declare the variable as global in the second include file. However, I do NOT need to declare it as global in the file that is calling the first include. For example:


front.inc:

$name = 'james';

index.php:

include('front.inc');
echo $name;
include('end.inc');

output: james


end.inc:

echo $name;

output: nothing


IF I declare global $name prior to echoing $name in end.inc, then it works properly. The accepted answer to this post explains that this depends on your server configuration: Passing variables in PHP from one file to another

I'm using an Apache server. How would I configure it so that declaring $name to be global is not necessary? Are there advantages/disadvantages to one vs. the other?

 Answers

15

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.

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.

Wednesday, March 31, 2021
 
SubniC
answered 7 Months ago
76

Something like this

<script type="text/javascript">
var markers = <?php echo json_encode($row) ?>;
</script>

Now the markers JavaScript variable will be available to any scripts following the above.

You can access associative entries from the PHP $row array using

var latlng = markers.latlng;

While we're here, you should protect your query from SQL injection. Whilst I always recommend using PDO and parameter binding, a quick fix for you would be

$ref = get_magic_quotes_gpc() ? stripslashes($_GET['ref']) : $_GET['ref'];
$sql = sprintf("SELECT * FROM `markers` WHERE `ref` = '%s'",
               mysql_real_escape_string($ref));
Wednesday, March 31, 2021
 
twk
answered 7 Months ago
twk
38

I answer this question:

will the echoed $variable1 be displayed as what the respective users have input to their browser or it will override the first user's input by the last user's input?

The echoed $variable1 will be displayed as what the respective users have input to their browser.

PHP makes thread per request, so different requests have different variables (include global).

I check this post.

As said by JasonB, PHP global variable has a very wide scope, and make code complex.

Wednesday, March 31, 2021
 
Anax
answered 7 Months ago
43

Change it

 $image = '$_GET[image_url]'; 

to

 $image = $_GET['image']; 
Saturday, May 29, 2021
 
erotsppa
answered 5 Months ago
55

So as I guessed you are using a framework as you said in the comments:

@Rizier123 Yes, I'm using Laravel. Does it matter? – Kai 6 mins ago

And if it matters? Yes it does.

Probably what is happening here is, that the code which you show us here is wrapped into another function somewhere else.

Means that the variables in the Sum() function are in global scope, but the other ones outside of it not, since they are probably in another function == another scope.


You can reproduce it with this code:

function anotherFunction() {
    $a = 1;
    $b = 2;

    function Sum() {
        $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
    } 

    Sum();
    echo $b;
}

anotherFunction();

And if you have error reporting on you will get:

Notice: Undefined index: a
Notice: Undefined index: b
2

Just put the error reporting at the top of your file(s) to get useful error messages:

<?php
    ini_set("display_errors", 1);
    error_reporting(E_ALL);
?>

To solve this now you have to declare the variables also in global scope, either with:

$GLOBALS["a"] = 1;
$GLOBALS["b"] = 2;

or like this:

global $a, $b;
$a = 1;
$b = 2;
Saturday, July 31, 2021
 
hnkk
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :