How do I create a PHP global variable?

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

How do I create a PHP global variable?

Post 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';
} 
?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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';
}
}
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

How are you trying to access it in the other files? Have you tried echo'ing $GLOBALS['ajaxsupport'] ?
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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. :?
djot
Forum Contributor
Posts: 313
Joined: Wed Jan 14, 2004 10:21 am
Location: planet earth
Contact:

Post 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);

?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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);
?>
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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

Code: Select all

<?php

$my_var = 42;
Included into this file:
test.php

Code: Select all

<?php

include "included_file.php";

echo $my_var; //42
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post 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. :roll:

Thanks!
Post Reply