php function not getting include - php

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;
}

Related

How to call a global dynamic variable in php

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.

PHP scope issue in anonymous function

so I have these functions
function a(){
int c = 1;
b(function(){echo $c;});
}
function b($code){
$code();
}
but somehow $c becomes undefined in the anonymous function
I know it's bacause that the anonymous function is it's own scope, but is there someway to make this work?
Yes: you can use "use" statement.
function a()
{
$c = 1;
b(function() use ($c) {
echo $c;
});
}
function b($code){
$code();
}
http://php.net/manual/en/language.variables.scope.php
When you put $c inside a function it's considered to be a local scope variable.

PHP define global variables for functions?

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");
}

Access local variable in function from outside function (PHP)

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

PHP variable defined outside callback function is unaccessible inside the function

I'm trying to use $variable inside my callback function. I pass it to another function like this: functionName("egTraders_ItemDataBound"), inside that function I assign it to a variable and the call it like this: $theAssignedFunctionVariable($this, $rowToAdd);
And the function egTraders_ItemDataBound gets called properly but the variable $variable
is undefined. What can I do?
<?php
$variable = "var";
function egTraders_ItemDataBound($sender, $param1) {
echo $variable;
}
?>
If You are running PHP 5.3+ You can achive this by simply creating anonymous functioin with use keyword ( documentation ) :
$bar = 'bar';
$f = function() use ($bar)
{
var_dump($bar);
};
function bar( $fName )
{
$fName();
}
bar($f);
You could pass it in as a param or you could use it as a global in the function. I do not recommend the latter. You should stay away from globals.
Edit for example
$variable = "var";
function egTraders_ItemDataBound($sender, $param1) {
global $variable;
echo $variable;
}
egTraders_ItemDataBound(NULL, NULL);
you need to declare the variable as global because it is out of scope
$variable = "var";
function egTraders_ItemDataBound($sender, $param1) {
global $variable;
echo $variable;
}
The variable is declared outside of the scope of the function. You should revisit your design. I strongly recommend against using global variables as that is poor practice.

Categories