Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

PHP include not seeing defined variable?

This is a question which I already know the answer. Its just for fun to see how many people can really code:

We have two files, one called index.php

one called test.php

index.php

---------------

<?php

$GLOBAL['test'] = "This will never work, as i have deliberately not sent it somewhere";

function test() {

// HINT

include 'test.php";

}

?>

---------

test.php

---------

<?php

$sometext = $GLOBAL['test'];

echo $sometext;

?>

Simple error, but can you spot what i have deliberately not done. I've given a hint, the missing bit of code is around there :P most coders will recognise the "error" is around line 5406 lol.

I make these questions so people can learn, my other php question i simply swapped the define and the call around. I was surprised how many people cant see simple mistakes, but it would drive someone insane as there are no actual errors.

Update:

The answer will need 2 things, otherwise the variable will never show. If i had included the first the answer would have been on the question lol.

Update 2:

@ juliepelletier you saw 1 1/2 of the 2 errors:

$globals was indeed missing the S and the function was not called, its deliberately written like that because its incomplete it needs just one more item before it would work.

Update 3:

As for my quotes that was a typo lol, i'll give you a bonus for that lol, you are of course correct i should have paid more attention. I'll award you best answer too, the bit you missed was the global was never passed to the function :) it should read:

<?php

$GLOBALS['test'] = "This will never work, as i have deliberately not sent it somewhere";

test($GLOBALS['test']);

function test($GLOBALS['test']) {

include 'test.php';

}

Update 4:

The reason it wouldnt work is because we didnt call the function, and we didnt pass the variable to the function.

if you just use include 'filename.php'; it will inherit the variables, however in a function it does not, and the values need to be passed.

1 Answer

Relevance
  • 5 years ago
    Favorite Answer

    Here are my observations on those awful lines of code:

    - $GLOBAL does not exist. You probably meant to use $GLOBALS.

    - Globals are to be avoided at all cost for code to be maintainable.

    - Your quotes surrounding test.php are mismatched.

    - You do not call the test() function so obviously the echo statement won't be called.

    - Including code from within functions is very disorganized and hardly maintainable.

    Conclusion: You should start by learning to do better code if you intend to teach to others.

Still have questions? Get your answers by asking now.