php tell between function and string

125
June 07, 2018, at 04:40 AM

is their a way to create an array that stores both strings and functions that generate strings?

<?php
function foo()
{
    return "calling foo()";
}

function bar()
{
    return "callin bar()";
}

$data = array();
$data[] = "foo";
$data[] = "bar";
foreach ($data as $element) {
    if (is_callable($element)) echo $element()."\n";
    else echo $element."\n";
}

will output:

calling foo()
calling bar()

is there some way to implement the array so that I get the output:

calling foo()
bar

something like

$data[] = &foo;
$data[] = "bar";
foreach ($data as $element) {
    if (is_callable($element) && !is_string($element)) return $element();
    else return $element;
}
Answer 1

There are many possible solutions, depending on what is your specific use case (which I couldn't clearly understand from your question).

Examples:

having an array with strings only

<?php
function foo()   { return "string foo from function"; }
function bacon() { return "string bacon from function"; }
$data = ['foo', 'bar', 'chunky', 'bacon'];
foreach ($data as $str) {
    echo function_exists($str) ? $str() : $str;
    echo PHP_EOL;
}

output:

string foo from function
bar
chunky
string bacon from function

mixing functions and strings on array

<?php
$foo   = function() { return "string foo from function"; };
$bacon = function() { return "string bacon from function"; };
$data = [$foo, 'bar', 'chunky', $bacon];
foreach ($data as $element) {
    echo is_callable($element) ? $element() : $element;
    echo PHP_EOL;
}

output:

string foo from function
bar
chunky
string bacon from function

fallback to printing string if function is undefined

This one is a bit uglier, but just for completeness ...

<?php
function foo()   { return "string foo from function"; };
function bacon() { return "string bacon from function"; };
$data = ['foo', 'bar', 'chunky', 'bacon'];
foreach ($data as $element) {
    try {
        echo $element(), PHP_EOL;
    } catch (\Error $e) {
        echo 'fallback to string ', $element, PHP_EOL;
    };
}

outputs:

string foo from function
fallback to string bar
fallback to string chunky
string bacon from function
Answer 2

I'm not sure why you would need this, but the problem is that the way you are doing it they are always strings and if they are callable there is no way to differentiate. How about using the key or something similar, since the array is ordered by the order of creation:

$data["foo"] = "foo";
$data[] = "bar";
foreach ($data as $key => $element) {
    if (is_callable($element) && is_string($key)) {
        echo $element()."\n";
    } else {
       echo $element."\n";
    }
}

Another possibility (there are many):

$data[]['fnc'] = "foo";
$data[]['str'] = "bar";
foreach ($data as $element) {
    if (isset($element['fnc']) && is_callable($element['fnc'])) {
        $element = $element['fnc'];
        echo $element()."\n";
    } else {
        echo $element['str']."\n";
    }
}
READ ALSO
PhpWord table no border

PhpWord table no border

I've added a table to a phpword file but seem to be unable to change the border of the tableI read that borderSize is a cell specific setting but even there no change

996
replace keys in array from other array php

replace keys in array from other array php

I have 2 arrays and want to copy keys from first array to second array and keep values on second array:

160
Set a custom calculated price in Woocommerce Before Calculate Totals hook

Set a custom calculated price in Woocommerce Before Calculate Totals hook

So I setup woocommerce with a seat reservation sort of plugin but I'm running into pricing issuesI can set the price and it gets it fine but the cart item shows double the price

193
Async search with jquery, ajax and json

Async search with jquery, ajax and json

I'm working on a project for schoolin this project, I have to make an async search bar to search data in a table in CodeIgniter, and this has to be done with JSON

131