Page 1 of 2

Use array variable from an includes?

Posted: Tue May 29, 2007 6:48 pm
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.

Re: Use array variable from an includes?

Posted: Tue May 29, 2007 6:56 pm
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. :)

Posted: Tue May 29, 2007 11:23 pm
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
  ),
  )
 );
?>

Posted: Tue May 29, 2007 11:38 pm
by feyd
"array" is all echo can say. It is an array.

var_export(), var_dump() and/or print_r() the value.

Posted: Wed May 30, 2007 12:56 am
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]; ?>

Posted: Thu May 31, 2007 4:06 pm
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";}
?>

Posted: Thu May 31, 2007 4:10 pm
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";}

Posted: Thu May 31, 2007 4:38 pm
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".

Posted: Thu May 31, 2007 4:39 pm
by feyd
You will need to search each element yourself if you want to look for "contains."

Posted: Thu May 31, 2007 4:46 pm
by superdezign
Or regex it.

Code: Select all

preg_match('/' . $useragents['noajax'][$i] . '/', $useragent);

Posted: Thu May 31, 2007 6:21 pm
by JAB Creations
Always returns true as well?

Code: Select all

if (preg_match('/' . $useragents['noajax'][$i] . '/', $useragent)) {echo '1';}

Posted: Thu May 31, 2007 6:47 pm
by superdezign
Are you... doing anything with [$i]? Or did you just copy and paste?

I was simply giving a suggestion through pseudo-code.

Posted: Thu May 31, 2007 7:09 pm
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?

Posted: Thu May 31, 2007 7:21 pm
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.

Posted: Thu May 31, 2007 7:22 pm
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.