Best unofficial Apache Server developers community
Username
Forgot password?
Sign in with Twitter account
Sign in with Facebook account

Php including files

0

62 views

Hi,

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

asked January 2, 2011 5:54 pm CST
posted via StackOverflow

2 Answers

0
 

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

answered January 2, 2011 6:23 pm CST
0
 

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.

answered January 2, 2011 6:23 pm CST

Your answer

Join with account you already have


Sign in with Twitter account
Sign in with Facebook account
Sign in with Google Friend Connect

Preview
Similar questions
Upload files with Apache
December 29, 2010