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.
Related
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 mytheme-child/functions.php where I declared and assigned global variable:
global $mycustvar;
$mycustvar = "abc"
Now when I print_r() same variable in **mytheme-child/woocommerce/single-product/product-image.php** then not able to get output as abc. It should be right? As it's a global variable?.
Kindly correct me if I misunderstood somewhere.
For example, in functions.php:
function test() {
global $hello;
$hello = 'hello world';
}
add_action('after_theme_setup', 'test');
In single.php, this will not work:
echo $hello;
Because $hello is undefined. This however will work:
global $hello;
echo $hello;
Give it a try:
// function.php
function my_custom_data() {
global $mycustvar;
$mycustvar = 'abc';
}
add_action( 'after_theme_setup', 'my_custom_data' );
then call any where just as
global $mycustvar;
echo $mycustvar;
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 a function that includes another file like so
// some function
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
}
// some_file.php
<?php echo $someData; ?>
How would I get this to work where the include file can use the variables from the calling function? I will be using some output buffering.
As long as $someData is defined in SomeFunction(), some_file.php will have access to $someData.
If you need access to variables outside of SomeFunction(), pass them as arguments to SomeFunction().
That's already available :)
See include()
The best would be to not do use globals at all, but pass the variable as parameter:
function SomeFunction()
{
$someData = 'SomeData';
include_once('some_file.php');
some_foo($someData);
}
Otherwise you risk transforming your code base in spaghetty code, at least on the long term.
Seems kinda unorganized to include files in functions...what about...
function SomeFunction()
{
$someData = 'SomeData';
return $someData;
}
$data = SomeFunction();
<?php include('file.php') ?> // file.php can now use $data
You don't have to do anything. Usage of include() (and it's siblings) is analogous to copy-pasting the code from the included file into the including file at the spot where include() is called.
Simple example
test.php
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
include 'test2.php';
}
test();
test2.php
<?php
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
Again, this is analogous to the combined
<?php
$foo = 'bar';
function test()
{
$bar = 'baz';
echo '<pre>', print_r( get_defined_vars(), 1 ), '</pre>';
}
test();
<?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.