1

This is about RAW php coding. I have a page - "config.php" and there are some declarations here.

//config.php

<?php
$myvalue = "This is a test";
include("functions.php");
?>

//functions.php

<?php
function testFunction()
{
   $dd = $myvalue;
   echo $dd;
}
?>

And when I call "testFunction()" from another page like "page.php" I want it to echo "This is a test". How can I do this? "config.php" is included to "page.php". Hope the scenario is understandable.

//page.php

<?php
include('config.php');
testFunction();
?>
Imrul.H
  • 5,760
  • 14
  • 55
  • 88
  • if you want to check if your function exists/defined you can use [function_exists](http://php.net/function_defined) like `` –  Oct 18 '11 at 14:38

6 Answers6

2

In the first line of your function, add:

global $myvalue;

Your $myvalue-variable is transfered into functions.php when you include that php-file after declaring the variable, but you have to tell your function to use this global variable.

Your function would look like this:

function testFunction()
{
   global $myvalue;
   $dd = $myvalue;
   echo $dd;
}
poplitea
  • 3,585
  • 1
  • 25
  • 39
  • How can you say the variable is not function specific? – hakre Oct 18 '11 at 14:43
  • Not academically the right words; just trying to explain how this is a global variable as opposed to a local variable within the function (hence the "function specific"-term). Now removing that parenthesis... – poplitea Oct 18 '11 at 14:49
  • Well the other suggestion is to make it a local variable by using a function parameter and the calling the function appropriately. – hakre Oct 18 '11 at 15:47
  • @hakre I know, see comment on your post. – poplitea Oct 18 '11 at 15:57
2

What you're colliding with is PHP Variable Scope.

Variables ($foo) are only accessible outside or inside the function() declaration (but do not cross this boundary unless you use the global modifier). However, constants can be seen throughout.

Example:

<?php
  $foo = 'bar';
  define('CONST_FOO', 'bar');

  echo $foo;      // output: bar
  echo CONST_FOO; // output: bar

  function Foo(){
    echo $foo;      // undefined, out of scope
    echo CONST_FOO; // output: bar
  }
  function Bar(){
    global $foo; // now $foo can be seen

    echo $foo;      // output: bar
    echo CONST_FOO; // output: bar
  }
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
2

You should specify the parameters a function uses with the function:

functions.php:

<?php
function testFunction($value)
{
   echo $value;
}
?>

Then in page.php:

<?php
include('config.php');
testFunction($myvalue);
?>

This will make your code more modular.

See as well: Is include()/require() with “side effects” a bad practice?

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • I agree passing variables as arguments is generally a much better solution (though that was not strictly what was asked here). Upvote for useful answer. – poplitea Oct 18 '11 at 15:10
2

$myValue is global, and by default cannot be seen from within the function scope. You could explicitly make it available like this:

<?php
function testFunction()
{
    global $myValue;
    $dd = $myvalue;
    echo $dd;
} 

Or you could define a constant:

<?php
define ('MY_VALUE', 'This is a test');

// functions.php
function testFunction()
{
    $dd = MY_VALUE;
    echo $dd;
} 

Or you could make it a static or const member of a config class:

<?php
// config.php
class Config {
    public static $myValue = 'This is a test';
    const myConst = 'This is a test';
}

// functions.php
require_once 'config.php';
function testFunction()
{
    $dd = Config::$myValue;
    echo $dd;

    $dd = Config::myConst;
    echo $dd;
} 
Daren Chandisingh
  • 2,157
  • 1
  • 13
  • 17
1

You need to declare that myValue is global, or use the $_GLOBALS array like this:

<?php
function testFunction()
{
   $dd = $_GLOBALS['myvalue'];
   echo $dd;
}
?>
qbert220
  • 11,220
  • 4
  • 31
  • 31
  • Though it's an _option_, I advise against using globals as so many things can go wrong. Also, it may just be verbiage, but "must" wouldn't be an accurate statement. – Brad Christie Oct 18 '11 at 15:36
0

Isn't it? : http://www.php.net/manual/en/function.isset.php

... and of course declare global variable

idm
  • 1,728
  • 2
  • 19
  • 26