Store function result to variable for later use in PHP?

Luis Rivera

Considering the time I've been using PHP I feel shame for asking this, but it seems I'm way too tired to solve it by myself at this moment.

I have a php variable function that gets an CSV file and stores the data into an array.

$csvdata = function(){
    // Get CSV data to array
    return $array;
}

This array is used by other functions in different situations.

The thing is that I though this functions will run just once, but I just noticed it runs every time I use the var $csvdata.

What I want is to create a self evoked function that runs only once and store the data into an array that I can use as many times as needed without PHP re-doing the process of getting the csv and storing the data every time I use $csvdata.

Its really weird because I feel like if my brain is playing a joke on me... (Brain: "It's obvious... I know the answer but I wont tell you")

Sorry pals

trincot

You can call newly created functions on the fly with call_user_func:

$csvdata = call_user_func(function(){
    // Get CSV data to array
    return $array;
});

Then just reference $csvdata as a variable (without parenthesis).

Explanation

The documentation calls the argument to call_user_func a callback, and that is what it is: you pass a function reference to it, and it is the implementation of call_user_func that calls your function for you ("calling you back"). The function reference can be an inline, anonymous function (like above).

But it can also be a variable representing a function:

$func = function(){
    // Get CSV data to array
    return $array;
};
$csvdata = call_user_func($func);

Or, it can be a string, which holds the name of the function:

function myFunc(){
    // Get CSV data to array
    return $array;
};
$csvdata = call_user_func('myFunc');

All of these do the same: your function gets called. You can even tell call_user_func which arguments to pass to your function:

function myFunc($arg){
    echo $arg;
    // Get CSV data to array
    return $array;
};
$csvdata = call_user_func('myFunc', 'hello');

The above will echo 'hello'.

I provide these variants just for information, as your interest is in the inline function argument. Also then you can pass arguments:

$csvdata = call_user_func(function($arg1, $arg2){
    echo $arg1 . "," . $arg2;
    // Get CSV data to array
    return $array;
}, 'hello', 'there');

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PHP: Store Result of JavaScript Function into a PHP Variable

From Dev

Store function result into variable

From Dev

Store function result into variable

From Dev

Store value as a variable for later use CodedUI

From Dev

Store type in variable to use like a type later

From Dev

Store "$@" to a variable for use later, respecting quoted args

From Dev

How to store query name in a variable and use it later?

From Dev

Store mysql query rows in variable for later use

From Dev

Store into variable result of function VBA

From Dev

How to store a value generated in a while loop to a variable for later use in JQuery

From Dev

Titanium / How to temporarily store a photo to use in a later function

From Dev

How can I store the result of a function into a variable?

From Dev

How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

From Dev

Assign function and it's conditions to a variable for use later in script - R

From Dev

Jquery/PHP: Result of jQuery function into PHP variable

From Dev

Use PHP variable in javascript later and from different files

From Dev

Store value in a directive for later use

From Dev

How to store a value for later use?

From Dev

store username to use in a later form

From Dev

How to store a function in a variable in Lisp and use it

From Dev

PHP static variable as a result of another function

From Dev

PHP - Use variable in mysqli string result

From Dev

How can I store a result of a function in a variable in Python?

From Dev

Why store the result of getElementById() to a variable instead of using the function everytime?

From Dev

How can I store a result of a function in a variable in Python?

From Dev

How to store result of a LINQ statement executed in foreach to a variable for future use?

From Dev

Store column result in variable

From Dev

Store the result of a command in a variable

From Dev

store into variable a sudo result

Related Related

  1. 1

    PHP: Store Result of JavaScript Function into a PHP Variable

  2. 2

    Store function result into variable

  3. 3

    Store function result into variable

  4. 4

    Store value as a variable for later use CodedUI

  5. 5

    Store type in variable to use like a type later

  6. 6

    Store "$@" to a variable for use later, respecting quoted args

  7. 7

    How to store query name in a variable and use it later?

  8. 8

    Store mysql query rows in variable for later use

  9. 9

    Store into variable result of function VBA

  10. 10

    How to store a value generated in a while loop to a variable for later use in JQuery

  11. 11

    Titanium / How to temporarily store a photo to use in a later function

  12. 12

    How can I store the result of a function into a variable?

  13. 13

    How to store the result of a SQL statement as a variable and use the result in an SSIS Expression?

  14. 14

    Assign function and it's conditions to a variable for use later in script - R

  15. 15

    Jquery/PHP: Result of jQuery function into PHP variable

  16. 16

    Use PHP variable in javascript later and from different files

  17. 17

    Store value in a directive for later use

  18. 18

    How to store a value for later use?

  19. 19

    store username to use in a later form

  20. 20

    How to store a function in a variable in Lisp and use it

  21. 21

    PHP static variable as a result of another function

  22. 22

    PHP - Use variable in mysqli string result

  23. 23

    How can I store a result of a function in a variable in Python?

  24. 24

    Why store the result of getElementById() to a variable instead of using the function everytime?

  25. 25

    How can I store a result of a function in a variable in Python?

  26. 26

    How to store result of a LINQ statement executed in foreach to a variable for future use?

  27. 27

    Store column result in variable

  28. 28

    Store the result of a command in a variable

  29. 29

    store into variable a sudo result

HotTag

Archive