Speedy Wap
Speedy Wap

Reputation: 488

Php including files

I wanted to know how files are included in php.

What I mean by that is that how the variables from the included files are imported and how the included file is able to use the variables from the main file.

I was creating a templating system and the problem was that I had an index file which included the other files such as config.php etc. But then depending on the page requested the index.php contained the template files too. But for some some reason I wasnt able to use the variables from the config file from in the template.php file

Upvotes: 0

Views: 132

Answers (2)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385104

Variable scope is inherited.

You can even return a value from an included file! Otherwise you may mostly consider the function to be simple, text-based (lexical) insertion.

See http://php.net/manual/en/function.include.php for more information.

Upvotes: 1

andrewk
andrewk

Reputation: 3871

below is vars.php

<?php

$color = 'green';
$fruit = 'apple';

?>

below is test.php

<?php

echo "A $color $fruit"; // A

include 'vars.php';

echo "A $color $fruit"; // A green apple

?>

simple one from http://php.net/manual/en/function.include.php

declaring global variables

<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;
?>

take a look at this http://php.net/manual/en/language.variables.scope.php

Upvotes: 1

Related Questions