Page 1 of 1
How do I create a PHP global variable?
Posted: Fri Jun 01, 2007 1:30 am
by JAB Creations
I would like to make
$ajaxsupport a global variable or at least make it available to files that include the file it's declared on. I've read
this page but it's examples are backwards declaring a variable and then including another file? The examples on php.net seem only aimed to help those who pretty much know what they are doing already.
test.php
Code: Select all
<?php
include("array_ajax_support.php");
if $ajaxsupport == 0) {echo 'no AJAX support, serve content automatically';}
else if $ajaxsupport == 1) {echo 'AJAX support, serve content only if requested via AJAX';}
?>
includes-ajax-support.php
Code: Select all
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragents = array
(
'noajax' => array // $useragents['noajax']
(
'Opera/3',
'Opera 3', // Spoofing
'Opera/4',
'Opera 4', // Spoofing
'Opera/5',
'Opera 5', // Spoofing
'Opera/6',
'Opera 6', // Spoofing
'Opera/7',
'Opera 7', // Spoofing
'Opera/8.00',
'Opera 8.00', // Spoofing
'Opera/8.01',
'Opera 8.01', // Spoofing
),
);
$checkit = '';
$answer = '';
foreach ($useragents['noajax'] as $ua) {
$checkit = stristr($useragent,$ua);
if ($checkit === false) {
$answer = false;
} else {
$answer = true;
break;
}
}
if ($answer) {
// echo 'Your browser does <b>not</b> support AJAX, falling back...';
$ajaxsupport = '0';
} else {
// echo 'Your browser supports AJAX.';
$ajaxsupport = '1';
}
?>
Posted: Fri Jun 01, 2007 1:38 am
by JAB Creations
I tried the following based off of
thispage without success.
Code: Select all
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragents = array
(
'noajax' => array // $useragents['noajax']
(
'Gecko',
'Opera/3',
'Opera 3', // Spoofing
'Opera/4',
'Opera 4', // Spoofing
'Opera/5',
'Opera 5', // Spoofing
'Opera/6',
'Opera 6', // Spoofing
'Opera/7',
'Opera 7', // Spoofing
'Opera/8.00',
'Opera 8.00', // Spoofing
'Opera/8.01',
'Opera 8.01', // Spoofing
),
);
$checkit = '';
$answer = '';
foreach ($useragents['noajax'] as $ua) {
$checkit = stristr($useragent,$ua);
if ($checkit === false) {
$answer = false;
} else {
$answer = true;
break;
}
}
if ($answer) {
// echo 'Your browser does <b>not</b> support AJAX, falling back...';
something();
function something() {
global $ajaxsupport = '0';
}
} else {
something();
function something() {
global $ajaxsupport = '1';
}
}
?>
Posted: Fri Jun 01, 2007 1:46 am
by s.dot
How are you trying to access it in the other files? Have you tried echo'ing $GLOBALS['ajaxsupport'] ?
Posted: Fri Jun 01, 2007 1:51 am
by JAB Creations
I get the following error...
Parse error: parse error, unexpected '=', expecting ',' or ';' on line 43
So it's not setting it as a global. I'm pretty sure I'm not correctly setting this as a variable. What I've been reading is that I can set a global manually (even though register_globals is off) if I set a global inside of a function. Is this true? If so what am I doing wrong?
I added the echo and of course it's not going to do anything as I have some sort of error.

Posted: Fri Jun 01, 2007 2:04 am
by djot
Code: Select all
<?php
// sometimes this line below is also needed. Don't know why.
// Most times I need this line when including/requiring files.
//global $test;
function something() {
global $test;
$test = TRUE;
}
something();
var_dump($test);
?>
Posted: Fri Jun 01, 2007 3:44 am
by JAB Creations
I mimicked the code but it always returns true?
test.php
Code: Select all
<?php
include("array_ajax_support.php");
echo $GLOBALS['globalajaxsupport'];
?>
array_ajax_support.php
Code: Select all
<?php
$useragent = $_SERVER['HTTP_USER_AGENT'];
$useragents = array
(
'noajax' => array // $useragents['noajax']
(
'Gecko',
'Opera/3',
'Opera 3', // Spoofing
'Opera/4',
'Opera 4', // Spoofing
'Opera/5',
'Opera 5', // Spoofing
'Opera/6',
'Opera 6', // Spoofing
'Opera/7',
'Opera 7', // Spoofing
'Opera/8.00',
'Opera 8.00', // Spoofing
'Opera/8.01',
'Opera 8.01', // Spoofing
),
);
$checkit = '';
$answer = '';
foreach ($useragents['noajax'] as $ua) {
$checkit = stristr($useragent,$ua);
if ($checkit === false) {
$answer = false;
} else {
$answer = true;
break;
}
}
function sendajaxvar() {
if ($answer) {$ajaxsupport = '0';}
else {$ajaxsupport = '1';}
global $globalajaxsupport;
$globalajaxsupport = $ajaxsupport;
}
sendajaxvar();
var_dump($sendajaxvar);
?>
Posted: Fri Jun 01, 2007 3:57 am
by Chris Corbyn
In your first post you include "array_ajax_support.php", the subsequently show code from "includes-ajax-support.php". Is this a typing error?
You are already declaring a global if you make the variable outside of class/function scope. PHP executes statements in order so this code:
included_file.php
Included into this file:
test.php
Code: Select all
<?php
include "included_file.php";
echo $my_var; //42
Posted: Fri Jun 01, 2007 4:21 am
by JAB Creations
Even though I fixed my code by adding the beginning parenthesis for the original post I don't understand why it didn't work earlier?
So let me make this really simple: if I only declare a variable in a file (even without it being declared inside of a function) I can use it as a global variable so long as I don't do anything with that variable after it's initial declaration on the page it was declared?
It works perfectly now (both locally and on a couple live servers) though it would have been nice if it had worked the first time around.
Thanks!