...

View Full Version : include global scope



zodehala
03-09-2008, 08:08 PM
why doesnot following code run ?


$x = include("lang.php");

function zode(){
$GLOBALS $x;

//codes

}

Inigoesdr
03-09-2008, 08:23 PM
Because you don't call the function:

$x = include('lang.php');
zode();

function zode()
{
global $x;
// codes
}

p4plus2
03-09-2008, 08:41 PM
$x = include('lang.php');
zode($x);

function zode()
{
global $x;
// codes
}

should be:


$x = include('lang.php');
function zode()
{
global $x;
// codes
}
zode(); // you don't need $x here since its global and it should be called after after declared(better coding)


will save you from further errors

Inigoesdr
03-09-2008, 11:00 PM
It's not necessarily better coding, unless you're planning to port the code to another language. Calling the function "before" it's declared doesn't really matter in PHP because the parser loads all functions on the first pass. In other C-type languages you are required to declare a function before it's called, though. Then again, if you were using proper coding we wouldn't be requiring global here at all(which I did initially, but changed my mind to keep from confusing an apparently novice coder; that's why I left the extra variable on accident).

zodehala
03-09-2008, 11:28 PM
lang.php

$lang[user][first] = "zodehala";

index.php

$x = include('lang.php');
function zode()
{
global $x;
echo $lang[user][first] ;
}
zode();

output is blank page :eek: why ?

Inigoesdr
03-09-2008, 11:33 PM
output is blank page :eek: why ?

You don't need to assign the return of include() to a variable if you want to parse the page:

include('lang.php');
function zode()
{
global $lang;
echo $lang['user']['first'];
}
zode();

zodehala
03-10-2008, 12:08 AM
thanx and how can i use following constant in all function like lang.php


define('CSS' , '<link href="style.css" rel="stylesheet" type="text/css">',FALSE);

Inigoesdr
03-10-2008, 12:14 AM
Constants are global, so as long as it's defined before you use it(via define() or in an included file), you should be able to just:

echo CSS;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum