Problem in Accessing Variable value across php files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ankit_sam
Forum Newbie
Posts: 2
Joined: Sun Nov 08, 2009 5:10 am

Problem in Accessing Variable value across php files

Post by ankit_sam »

Hi,
As the title says it all.
Problem in Accessing Variable value across php files.
I have two .php files.
I have simplied the code to give as an example.

here is the code of both php

functions.php

Code: Select all

<?php
 
function getValue()
{
$a=10;
}
?>
index.php

Code: Select all

<?php
require_once('functions.php')
getValue();
echo $a;
?>
Now, when i execute, 10 shuld be printed?
if not then how to do it.
because i have 4 variables in functions.php
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Problem in Accessing Variable value across php files

Post by requinix »

The problem isn't that you're using two files: the problem is about variable scope. Read this.

If you have a variable inside a function, and you want to access it outside the function, make the function return it. More information.

Code: Select all

function getValue()
{
    $a=10;
    return $a;
}

Code: Select all

require_once('functions.php')
$a=getValue();
echo $a;
ankit_sam
Forum Newbie
Posts: 2
Joined: Sun Nov 08, 2009 5:10 am

Re: Problem in Accessing Variable value across php files

Post by ankit_sam »

Thanks :)
Post Reply