Get Variables from the Outside, Inside a Function in PHP

Giving my function access to outside variable

By default, when you are inside a function, you do not have access to the outer variables.


If you want your function to have access to an outer variable, you have to declare it as global, inside the function :

function someFuntion(){
global $myArr;
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal;
}

For more informations, see Variable scope.

But note that using global variables is not a good practice : with this, your function is not independant anymore.


A better idea would be to make your function return the result :

function someFuntion(){
$myArr = array(); // At first, you have an empty array
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
return $myArr;
}

And call the function like this :

$result = someFunction();


Your function could also take parameters, and even work on a parameter passed by reference :

function someFuntion(array & $myArr){
$myVal = //some processing here to determine value of $myVal
$myArr[] = $myVal; // Put that $myVal into the array
}

Then, call the function like this :

$myArr = array( ... );
someFunction($myArr); // The function will receive $myArr, and modify it

With this :

  • Your function received the external array as a parameter
  • And can modify it, as it's passed by reference.
  • And it's better practice than using a global variable : your function is a unit, independant of any external code.


For more informations about that, you should read the Functions section of the PHP manual, and,, especially, the following sub-sections :

  • Functions arguments
  • Returning values

Access variable from outside function (PHP)

Insert this inside the function:

global $ERROR;

So, the variable can be accessed inside the function scope (see global keyword).

function validateLogin($data) {
global $ERROR;
...
}

Alternatively you can access to all variables that are outside the function using $GLOBALS:

$GLOBALS['ERROR']

Get variables from the outside, inside a function in PHP

You'll need to use the global keyword inside your function.
http://php.net/manual/en/language.variables.scope.php

EDIT (embarrassed I overlooked this, thanks to the commenters)

...and store the result somewhere

$var = '1';
function() {
global $var;
$var += 1; //are you sure you want to both change the value of $var
return $var; //and return the value?
}

PHP function use variable from outside

Add second parameter

You need to pass additional parameter to your function:

function parts($site_url, $part) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
}

In case of closures

If you'd rather use closures then you can import variable to the current scope (the use keyword):

$parts = function($part) use ($site_url) { 
$structure = 'http://' . $site_url . 'content/';
echo $structure . $part . '.php';
};

global - a bad practice

This post is frequently read, so something needs to be clarified about global. Using it is considered a bad practice (refer to this and this).

For the completeness sake here is the solution using global:

function parts($part) { 
global $site_url;
$structure = 'http://' . $site_url . 'content/';
echo($structure . $part . '.php');
}

It works because you have to tell interpreter that you want to use a global variable, now it thinks it's a local variable (within your function).

Suggested reading:

  • Variable scope in PHP
  • Anonymous functions

Php get function variable outside not working

since the variables '$var','$var2' and '$var3' are local variable for the function variables so it has no existence unless the function is called or executed.

So there are two methods to fix your issue

  1. Method one make the variable global.

      <?php
    // Declaring the global veriable
    $var=0;
    $var2=0;
    $var3=0;

    function variables()
    { // method 2 to declare global
    GLOBAL $var = rand(1111,9999);
    GLOBAL $var2 = rand(11111,99999);
    GLOBAL $var3 = rand(111111,999999);
    }

but still, if u want to get the rand value from the function u need to call the function at least once

     // calling the function
variables();
echo variables([$var]);
echo variables([$var2]);
echo variables([$var3]);

?>

hope you got the solution if not just comment I will help u out with this for sure

How to call variable inside function while using require or include outside that function in php

The use of include/require is actually not relevant for your question. An include or a require will only "merge" the contents of other files to your code in order to become available.

As others have mentioned you need to use the global keyword. In many other programming languages the global variables are visible inside functions by default. But in PHP you need to explicitly define which variables that should be visible in each function.

Example:

<?php  
$a = "first";
$b = "second";
$c = "third";

function test() {
global $a, $b;
echo $a . " " . $b . " " . $c;
}

test(); // This will output only "first second" since variable $c is not declared as global/visible in the test-function
?>

However, if the variables should be treated as static constants I would recommend you to define them as constants. Constants have a "real" global scope and you do not need to use the global keyword.

<?php  
define("A", "first");
define("B", "second");
define("C", "third");

function test() {
echo A . " " . B . " " . C;
}

test(); // This will output "first second third". You don't need to use the global keyword since you refer to global constants
?>

Access local variable in function from outside function (PHP)

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

make variables available outside function in PHP?

I would strongly advise NOT using global.

What will probably be best is for you to return from the function:

function imageSize($name, $nr, $category){
$path = 'ad_images/'.$category.'/'.$name.'.jpg';
$path_thumb = 'ad_images/'.$category.'/thumbs/'.$name.'.jpg';
list($width, $height) = getimagesize($path);
list($thumb_width, $thumb_height) = getimagesize($path_thumb);
${'thumb_image_' . $nr . '_width'} = $thumb_width;
${'thumb_image_' . $nr . '_height'} = $thumb_height;
${'image_' . $nr . '_width'} = $width;
${'image_' . $nr . '_height'} = $height;

$myarr = array();
$myarr['thumb_image_' . $nr . '_width'] = $thumb_width;
$myarr['thumb_image_' . $nr . '_height'] = $thumb_height;
$myarr['image_image_' . $nr . '_width'] = $width;
$myarr['image_image_' . $nr . '_height'] = $height;
return $myarr;

}

$myImage = imageSize($name, $nr, $category);

then you access each var:

echo $myImage['thumb_image_1_width'];
echo $myImage['thumb_image_1_height'];
echo $myImage['image_1_weight'];
echo $myImage['image_1_height'];

etc.



Related Topics



Leave a reply



Submit