Use array variable from an includes?

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

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

Use array variable from an includes?

Post by JAB Creations »

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Re: Use array variable from an includes?

Post by feyd »

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'].
What have you tried?
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.
It doesn't. :)
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'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

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
  ),
  )
 );
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

"array" is all echo can say. It is an array.

var_export(), var_dump() and/or print_r() the value.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

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

Post by JAB Creations »

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";}
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

JAB Creations wrote:if (in_array("Gecko", $useragents['noajax'])) {echo "Got Gecko";}
The only thing you check for is "Gecko." Maybe this is what you meant:

Code: Select all

if (in_array($useragent, $useragents['noajax'])) {echo "Got Gecko";}
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

superdezign, this works almost perfectly however it only does exact matches and I want to do partial. For example it would only trigger if the entire useragent is simply "Gecko" and not if the useragent contains "Gecko".
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You will need to search each element yourself if you want to look for "contains."
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Or regex it.

Code: Select all

preg_match('/' . $useragents['noajax'][$i] . '/', $useragent);
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Always returns true as well?

Code: Select all

if (preg_match('/' . $useragents['noajax'][$i] . '/', $useragent)) {echo '1';}
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Are you... doing anything with [$i]? Or did you just copy and paste?

I was simply giving a suggestion through pseudo-code.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

JAB Creations wrote:superdezign, this works almost perfectly however it only does exact matches and I want to do partial. For example it would only trigger if the entire useragent is simply "Gecko" and not if the useragent contains "Gecko".
strstr() or strpos() maybe?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Everah wrote:
JAB Creations wrote:superdezign, this works almost perfectly however it only does exact matches and I want to do partial. For example it would only trigger if the entire useragent is simply "Gecko" and not if the useragent contains "Gecko".
strstr() or strpos() maybe?
Right.
PHP Manual wrote:Tip: Do not use preg_match() if you only want to check if one string is contained in another string. Use strpos() or strstr() instead as they will be faster.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

What is [$i]? I'm not sure what I'm supposed to do with it? When something works, I replicate it. Once it's replicated I have repetition and once I have repetition I'm able to learn.
Post Reply