Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

global variable throughout all php files 1

Status
Not open for further replies.

miguelleeuwe

Programmer
Oct 17, 2007
242
NL
Hello,

I'm very new to php programming. What I need is some sort of global variable that I can access from more than one php file.
I read that it's posible to read a variable "out of scope" using the 'global' word, but only seems to work within the same php-file. I can't seem to get that behaviour for a variable declared in another php-file.

thanks in advance
Miguel

regards,
Miguel L.
 
Hi

Huh ?
Code:
[blue]master #[/blue] cat php1.php
<?php

$v1='one';

include 'php2.php';

echo "in php1 : '$v1' '$v2'\n";

[blue]master #[/blue] cat php2.php
<?php

$v2='two';

echo "in php2 : '$v1' '$v2'\n";

[blue]master #[/blue] php php1.php 
in php2 : 'one' 'two'
in php1 : 'one' 'two'

Feherke.
 
global does not really mean make this variable globally available. it is a bit of a misnomer.

global makes a variable that is in the global scope, available to the function or method scope.

Code:
$a = 'foo';

function test(){
  echo '$a = ' . $a . '<br/>'; //will output a blank assignment

  global $a;
  echo '$a = ' . $a' ; // will output $a = foo;
}

you must globalise a global variable in each function or method that you want to use it.

global variables are also addressable as $GLOBALS superglobal array.

you can also use definitions to make 'variable' assignments available everywhere in an application.
 
I may be totally off, but perhaps what you need is a session variable. This will maintain its value through out any script that calls the session_start() function.



----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
thank you all very much.

I think I'll go for the "session variable", if I can make it work:

in page1 I do this:
----
//variable de sesión $lenguaje
session_start();
session_register ("lenguaje");
$lenguaje = "ES";

in page2 I do this:
------------------
// session_start(); necessary???
echo $lenguaje;

It doesn't seem to work.
page2 is a html file that is opened through 'href' link.

Any idea what's wrong?

Thank you.
Miguel

regards,
Miguel L.
 
you must call session_start() once per session. not once per page.

you do not need to use session_register.

session variables are addressed as $_SESSION['variable name']

Code:
<?php
if (session_id() == '') session_start();
if (!isset($_SESSION['lenguaje'])) $_SESSION['lenguaje'] = 'ES'; //set default language
if (isset($_GET['lenguaje'])) $_SESSION['lenguaje'] = strtoupper(trim($_GET['lenguaje'])); //allow override of the language via the url parameters
?>

then at the top of every page that is called directly by the user add this
Code:
<?php require_once 'setLanguage.php'; ?>

you could also set a php.ini directive for auto_prepend_file = setLanguage.php

the language variable is, after calling this file, available in the variable $_SESSION['lenguaje']

 
thank you all very much! mmm maybe a "php for dummies" might be an interesting option for me ;-)



regards,
Miguel L.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top