<?PHP
$bannedIPs = array('127.0.0.1','72.189.218.85');
function ipban()
{
if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs))
{
echo 'test';
}
}
ipban();
?>
The output of this script is:
Warning: in_array() [function.in-array]: Wrong datatype for second
argument in C:\webserver\htdocs\test\array.php on line 93
Can someone tell me why? I don't get it
And yes $_SERVER['REMOTE_ADDR'] is displaying 127.0.0.1
UPDATE
As suggested I tried this now but still get the same error
function ipban() {
$bannedIPs = array('127.0.0.1','72.189.218.85');
if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) {
echo 'test';
}
}
ipban();
You have run into a little problem with your variable scoping.
Any variables outside a function in PHP is not accessible inside. There are multiple ways to overcome this.
You could either declare $bannedIPs inside your function as such:
function ipban() {
$bannedIPs = array('127.0.0.1','72.189.218.85');
if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) {
echo 'test';
}
}
Tell your function to access $bannedIPs outside the function using the global keyword:
$bannedIPs = array('127.0.0.1','72.189.218.85');
function ipban() {
global $bannedIPs;
if (in_array($_SERVER['REMOTE_ADDR'], $bannedIPs)) {
echo 'test';
}
}
Or, use the $GLOBALS super global:
$bannedIPs = array('127.0.0.1','72.189.218.85');
function ipban() {
if (in_array($_SERVER['REMOTE_ADDR'], $GLOBALS['bannedIPs'])) {
echo 'test';
}
}
I recommend you read the manual page on Variable scope:
PHP: Variable Scope
If it's still not working, you have another problem in your code. In which case, you might want to consider using a var_dump() to check what datatype is $bannedIPs before down voting us all.
function ipban() {
global $bannedIPs;
var_dump($bannedIPs);
}
Your variable $bannedIPs is out-of-scope inside the function. Read up on variables scope: http://php.net/variables.scope
$var = 'xyz';
function abc() {
// $var does not exist here
$foo = 'abc';
}
// $var exists here
// $foo does not exist here
RE: Update:
Moving the variable inside the function works, your code snippet executes fine. There's got to be something else in your code.
What happens if you move $bannedIPs inside the function declaration? It's possible PHP thinks it's out of scope.
You need to global $bannedIPs;
This works for me:
function ipban() {
$bannedIPs = array('127.0.0.1','72.189.218.85');
$ip = '127.0.0.1';
if (in_array($ip, $bannedIPs)) {
echo 'test';
}
}
ipban();
So, you might want to see if that works, substitute in the IP address, and then finally replace it with the SERVER variable.
Related
I try to find the solution since many hours, but I can't solve it (I'm not a programmer ;)).
On a function, I have set a dynamic array which I want to use in another function.
To do this, I thought to use $GLOBALS[] array
I have no problem to call the variable out of the function one, but when I try to use it in function 2, it doesn't works.
Here is my code :
function one($name,$a,$b,$c)
{
// $GLOBALS[${$name}] = array($a,$b,$c);
global $$name;
$$name = array($a,$b,$c);
}
function two($name)
{
$name="D1";
echo ${$name}[1];
}
one("D1",10,20,30);
one("D2",100,200,300);
two("D1"); // doesn't works
$name="D1";
echo ${$name}[1]; // works, gives 20
$name="D2";
echo ${$name}[1]; // works, gives 200
It doesn't works and I do not understand why.
Thanks for your help.
Etienne
how about doing something like this:
function one() {
$dynamicArray = generateDynamicArray();
return $dynamicArray;
}
function two() {
$one = one(); // if it's in a class use: $this->one();
foreach($one in $key=>$value) {
// your code for each item in the array we got form function one()
}
}
instead of defining it globally.
<?php
function one($name,$a,$b,$c)
{
global $$name;
$$name = array($a,$b,$c);
}
function two($name)
{
global $$name;
echo ${$name}[0];
}
one("D1",10,20,30);
two("D1");
The scope of your function is different to the global scope.
I have file a.php that looks like
function func_1() {
inlude_once(b.php);
$somevar = 'def';
func_2($somevar);
}
and b.php that looks like
$some_global_var = 'abc';
function func_2($var) {
global $some_global_var;
echo $some_global_var.$var;
}
And for some reason I get only def as a result, why func_2 don't see $some_global_var ?
Because you forgot the scope of func_1. So when you include your define this is how your code appears to PHP
function func_1() {
$some_global_var = 'abc'; // <- this is inside the scope of the parent function!
function func_2($var) {
global $some_global_var;
echo $some_global_var.$var;
}
$somevar = 'def';
func_2($somevar);
}
You're doing it inside func_1. So the variable was never really available in the global scope. If you defined $some_global_var = 'abc'; outside, then it's in the global scope.
What you should do is inject this as an argument instead. Globals are a bad practice
function func_1() {
$some_global_var = 'abc';
function func_2($var, $var2) {
echo $var2 . $var;
}
$somevar = 'def';
func_2($somevar, $some_global_var);
}
put global in front of it.
per the PHP docs
Using global keyword outside a function is not an error. It can be used if the file is included from inside a function.
you may have an issue with an include file getting in the way
I have this in index.php
require('include\inc\config.inc');
When I created a function which require some variable from config.inc
When I run, it says the variable is missing
I need to redeclare the config.inc else the function will never work.
Is there a way to work around?
Now I have this
function test(){
require('include\inc\config.inc');
echo $tmp;
}
instead of
require('include\inc\config.inc');
function test(){
echo $tmp;
}
You should read about the variable scope. The $tmp variable is global and is not visible inside any function, unless declared with global
require('include\inc\config.inc');
function test(){
global $tmp;
echo $tmp;
}
Your variable is out of scope in the test() function as previously mentioned and using globals is not considered good practice. You need to pass the variable $tmp into the function test() like this:-
function test($tmp)
{
echo $tmp;
}
Then call the function like this:-
require_once 'include/inc/configure.inc'
test($tmp);
I'm not fluent in PHP, but can't you write:
require('include\inc\config.inc');
function test() use ($tmp) {
echo $tmp;
}
Is there a way to achieve the following in PHP, or is it simply not allowed? (See commented line below)
function outside() {
$variable = 'some value';
inside();
}
function inside() {
// is it possible to access $variable here without passing it as an argument?
}
note that using the global keyword is not advisable, as you have no control (you never know where else in your app the variable is used and altered). but if you are using classes, it'll make things a lot easier!
class myClass {
var $myVar = 'some value';
function inside() {
$this->myVar = 'anothervalue';
$this->outside(); // echoes 'anothervalue'
}
function outside() {
echo $this->myVar; // anothervalue
}
}
Its not possible. If $variable is a global variable you could have access it by global keyword. But this is in a function. So you can not access it.
It can be achieved by setting a global variable by$GLOBALS array though. But again, you are utilizing the global context.
function outside() {
$GLOBALS['variable'] = 'some value';
inside();
}
function inside() {
global $variable;
echo $variable;
}
No, you cannot access the local variable of a function from another function, without passing it as an argument.
You can use global variables for this, but then the variable wouldn't remain local.
It's not possible. You can do it by using global. if you just only do not want to define the parameters but could give it inside the function you can use:
function outside() {
$variable = 'some value';
inside(1,2,3);
}
function inside() {
$arg_list = func_get_args();
for ($i = 0; $i < $numargs; $i++) {
echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
}
}
for that see the php manual funct_get_args()
You cannot access the local variable in function. Variable have to set as global
function outside() {
global $variable;
$variable = 'some value';
inside();
}
function inside() {
global $variable;
echo $variable;
}
This works
I have:
include ('functions.php');
check_blocked();
echo $blocked;
in functions.php, check_blocked(); exists. Inside check_blocked I got:
global $blocked;
$blocked = '1234';
I want to echo $blocked variable, that are inside check_blocked().
It doesnt work, no output..
This is an example of my original problem, so please dont say that I could just have the echo inside the function, as I cannot have in my original code.
Your code should look like
$blocked = 0;
include('functions.php');
check_blocked();
echo $blocked;
With functions.php looking like
function check_blocked(){
global $blocked;
$blocked = 1234;
}
You must define blocked outside of the scope of the function before you global blocked into the function.
Why not just returning the value?
function check_blocked() {
$blocked = '1234';
return $blocked;
}
then
include ('functions.php');
echo check_blocked();
Avoid globales wherever possible.
You can change check_blocked() to return $blocked or you can try use:
include ('functions.php');
check_blocked();
global $blocked;
echo $blocked;
instead, use a return value. globals are bad.
include ('functions.php');
echo check_blocked();
function check_blocked() {
return '1234';
}
Unless the variable is declared outside of the check_blocked() function, you can't.
$foo = null;
function check_blocked() {
global $foo;
$foo = "Hello";
// do some stuff
}
global $foo;
echo $foo; // prints "Hello"
If its defined inside the function like:
function check_blocked() {
global $foo;
$foo = "Hello";
// other work
}
global $foo;
echo $foo; // Nothing
then the variable is scoped locally, and is discarded as soon as the function completed.