How to make a variable inside a function be available outside?

Light93

I can't seem to get the array on $sql_result_array be available out of the function.

function function_name($query, $table_name){
    global $connection;
$sql_query = " SELECT * FROM {$table_name} WHERE name = '{$query}' ";
$sql_result = mysql_query($sql_query, $connection);
$sql_result_array = mysql_fetch_array($sql_result);
}

function_name($value1, $value2);
print_r($sql_result_array); // (outputs) Undefined variable: sql_result_array

Any suggestion on how to get that array available outside the function?

dave

You can either do

print_r(function_name($value1, $value2));

or

$sql_result_array = function_name($value1, $value2)
print_r($sql_result_array);

The general idea here, is that any variable you declare inside of a function is ONLY available inside of that function. So if you have

function a() {
    $i = 5;
    return $i;
}

OUTSIDE of that function, $i does not exist. Or if it DOES exist, setting $i to 5 INSIDE that function, won't change the value of $i OUTSIDE of that function. This is good because otherwise, you would have to use unique variable names EVERYWHERE, otherwise you would never know if a value got changed somewhere else.

We return $i so that is we want to use that value outside of the function, we can. But we have to either assign it

$other_variable = a();

or use it directly

do_something_else(a())

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a counted value inside FoldRows() function available outside the function in a CouchDB list?

From Dev

outside variable not available inside enhanced for

From Dev

JS Variable not available outside function

From Dev

How to make a return variable not undefined outside the function

From Dev

How to make a variable accessible outside a function?

From Dev

Global variable inside anonymous self-executing js function still available outside?

From Dev

How to make a variable which is inside a function global?

From Dev

Variable not available outside of a function in a react component

From Dev

make variable available in other function

From Dev

How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

From Dev

Global variable assigned inside while loop not available outside the loop

From Dev

Calling a JavaScript variable inside a function from outside

From Dev

static variable scope inside and outside the function

From Dev

Permanently changing an outside variable inside a function

From Dev

Make a variable standard inside function

From Dev

How to pass variable from inside click function to set variable outside to zero

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

array variable inside function affects the array variable outside the function in c

From Dev

How to use a variable created inside a function, outside of the function? (Android Studio, Java)

From Dev

How do I make a variable created inside a function become global?

From Dev

How do I make a variable created inside a function become global?

From Dev

How to set a member variable from outside of class and make use of that varible inside class in C++ ?

From Dev

How can I call a function that set sets an outside variable inside a custom component in React Native?

From Dev

return variable name from outside of function, as string inside python function

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

Will a variable declared with cdef outside a function have the same type inside the function?

From Dev

AngularJS: variable $scope inside a function appears undefined outside the function

From Dev

How to vectorize a function not 'inside' but 'outside' in R?

From Dev

How to make the result from a for loop available outside of that loop?

Related Related

  1. 1

    How to make a counted value inside FoldRows() function available outside the function in a CouchDB list?

  2. 2

    outside variable not available inside enhanced for

  3. 3

    JS Variable not available outside function

  4. 4

    How to make a return variable not undefined outside the function

  5. 5

    How to make a variable accessible outside a function?

  6. 6

    Global variable inside anonymous self-executing js function still available outside?

  7. 7

    How to make a variable which is inside a function global?

  8. 8

    Variable not available outside of a function in a react component

  9. 9

    make variable available in other function

  10. 10

    How to Manipulate Variable Value inside a Function When The Variable was Declared Outside the Function?

  11. 11

    Global variable assigned inside while loop not available outside the loop

  12. 12

    Calling a JavaScript variable inside a function from outside

  13. 13

    static variable scope inside and outside the function

  14. 14

    Permanently changing an outside variable inside a function

  15. 15

    Make a variable standard inside function

  16. 16

    How to pass variable from inside click function to set variable outside to zero

  17. 17

    array variable inside function affects the array variable outside the function in c

  18. 18

    array variable inside function affects the array variable outside the function in c

  19. 19

    How to use a variable created inside a function, outside of the function? (Android Studio, Java)

  20. 20

    How do I make a variable created inside a function become global?

  21. 21

    How do I make a variable created inside a function become global?

  22. 22

    How to set a member variable from outside of class and make use of that varible inside class in C++ ?

  23. 23

    How can I call a function that set sets an outside variable inside a custom component in React Native?

  24. 24

    return variable name from outside of function, as string inside python function

  25. 25

    AngularJS: variable $scope inside a function appears undefined outside the function

  26. 26

    Will a variable declared with cdef outside a function have the same type inside the function?

  27. 27

    AngularJS: variable $scope inside a function appears undefined outside the function

  28. 28

    How to vectorize a function not 'inside' but 'outside' in R?

  29. 29

    How to make the result from a for loop available outside of that loop?

HotTag

Archive