Use array variable from an includes?
Moderator: General Moderators
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Use array variable from an includes?
I have a variable array I'd like to use that is located in an includes file. It looks something like $names['male'].
Also I am aware that at one point they turned a major feature off (globals?) and I'm not sure how this would relate.
Also I am aware that at one point they turned a major feature off (globals?) and I'm not sure how this would relate.
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Re: Use array variable from an includes?
What have you tried?JAB Creations wrote:I have a variable array I'd like to use that is located in an includes file. It looks something like $names['male'].
It doesn't.JAB Creations wrote:Also I am aware that at one point they turned a major feature off (globals?) and I'm not sure how this would relate.
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
I'm trying to build an array that I can check if a browser does not support AJAX. I include this file and I'm not sure how to refer to the variable other then as "$useragents['noajax']" after including the file. I echo the variable and all it will say is array.
Then I also want to simply check if the browser's useragent is in this list, that way I can serve AJAX capable browsers AJAX content and fall back for browsers without AJAX support.
example.php
Then I also want to simply check if the browser's useragent is in this list, that way I can serve AJAX capable browsers AJAX content and fall back for browsers without AJAX support.
example.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
),
)
);
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
You can echo a member of the array as long as it is not an array (or other complex type like an object). To see the entire array, do what feyd said. To see a member, simply reference it:
Code: Select all
<?php echo $useragents['noajax'][0]; ?>- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Using Firefox for testing, this always returns true?
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
),
);
if (in_array("Gecko", $useragents['noajax'])) {echo "Got Gecko";}
?>- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
The only thing you check for is "Gecko." Maybe this is what you meant:JAB Creations wrote:if (in_array("Gecko", $useragents['noajax'])) {echo "Got Gecko";}
Code: Select all
if (in_array($useragent, $useragents['noajax'])) {echo "Got Gecko";}- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Or regex it.
Code: Select all
preg_match('/' . $useragents['noajax'][$i] . '/', $useragent);- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact:
Always returns true as well?
Code: Select all
if (preg_match('/' . $useragents['noajax'][$i] . '/', $useragent)) {echo '1';}- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- JAB Creations
- DevNet Resident
- Posts: 2341
- Joined: Thu Jan 13, 2005 6:44 pm
- Location: Sarasota Florida
- Contact: