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.
Related
I want to create an array which is in my function.php code definded so I do not have to transfer the array through my hole code. But it won't work...
This is an example of my function.php:
<?php
define("titleArray", array());
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
But it doesn't work... How can I fix that so that every function has access to the array?
Greetings
I suggest that you store your array globally using the $GLOBALS, and instead of using echo to print an array, use the print_r method instead.
<?php
$GLOBALS["titleArray"] = array('world');
function foo(){
print_r($GLOBALS["titleArray"]);
}
function boo(){
array_push($GLOBALS["titleArray"], "Hello");
print_r($GLOBALS["titleArray"]);
}
foo();
boo();
just remove $this
<?php
define("titleArray", array());
function foo(){
echo titleArray;
}
function boo(){
array_push(titleArray, "Hello");
}
note that array values are allowed after php 7.0 version
Generally we use define to make constant value which never updates, use class instead
<?php
class abc {
$titleArray = [];
function foo(){
echo $this->titleArray;
}
function boo(){
array_push($this->titleArray, "Hello");
}
}
The define() function defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set.
There are two ways to do it:
use global in every function
declare $GLOBALS[] once outside the function
$titleArray = [];
// $GLOBALS['titleArray'] = [];
function foo(){
global $titleArray;
print_r($titleArray);
// print_r($GLOBALS['titleArray']);
}
function boo(){
global $titleArray;
array_push($this->titleArray, "Hello");
// array_push($GLOBALS['titleArray'], "Hello");
}
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;
}
Having some trouble with this..
<?php
EG204_ExoSkel();
function EG204_ExoSkel() {
$to_be_end = 'Red';
everything_loop();
}
function everything_loop() {
echo $to_be_end;
}
?>
The code above will not echo Red, so I must be trying to use functions backwards. Might that be possible?
All that is in the Everything function is to be apart of different foreach loops.
Try this one (send it as an argument)
<?php
EG204_ExoSkel();
function EG204_ExoSkel() {
$to_be_end = 'Red';
everything_loop($to_be_end);
}
function everything_loop($argument) {
echo $argument;
}
?>
http://sandbox.phpcode.eu/g/3c1b6.php
You have scoping issues. The declaration of $to_be_end is only available in the EG204_ExoSkel function.
If you want to use it outside that function you should use global to make it globally available. And also add global in the other function (also to made use of the global variable). Resulting in this:
EG204_ExoSkel();
function EG204_ExoSkel() {
global $to_be_end;
$to_be_end = 'Red';
everything_loop();
}
function everything_loop() {
global $to_be_end;
echo $to_be_end;
}
Note: using global is considered bad practice and tends to make a mess of your code (and even introduce hard to find bugs). A better solution (if possible in your real code) is to pass on the variable to the other function(s).
Try referencing $to_be_end as a global!
WARNING Global are bad practice Avoid
<?php
EG204_ExoSkel();
function EG204_ExoSkel() {
global $to_be_end;
$to_be_end = 'Red';
everything_loop();
}
function everything_loop() {
global $to_be_end;
echo $to_be_end;
}
?>
How to pass a $_GET variable into function?
$_GET['TEST']='some word';
public function example() {
//pass $_GET['TEST'] into here
}
When I try to access $_GET['TEST'] in my function, it is empty.
The $_GET array is one of PHPs superglobals so you can use it as-is within the function:
public function example() {
print $_GET['TEST'];
}
In general, you pass a variable (argument) like so:
public function example($arg1) {
print $arg1;
}
example($myNonGlobalVar);
If this is a function and not an object method then you pass the parameter like so
function example($test) {
echo $test;
}
and then you call that function like so
$_GET['test'] = 'test';
example($_GET['test']);
output being
test
However if this is an object you could do this
class Test {
public function example($test) {
echo $test;
}
}
and you would then call it like so
$_GET['test'] = 'test';
$testObj = new Test;
$testObj->example($_GET['test']);
and the output should be
test
I hope this helps you out.
First of all - you should not set anything to superglobals ($_GET, $_POST, etc).
So we convert it to:
$test = 'some word';
And if you want to pass it to the function just do something like:
function example($value) {
echo $value;
}
And call this function with:
example($test);
function example ($value) {
$value; // available here
}
example($_GET['TEST']);
function example($parameter)
{
do something with $parameter;
}
$variable = 'some word';
example($variable);
Simply declare the value for the variable by
declare the function by
function employee($name,$email) {
// function statements
}
$name = $_GET["name"];
$email = $_GET["email"];
calling the function by
employee($name,$email);
<?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.