php4d-database

PHP Use 'eval' As a Function (Get Return Value)


Background

I'm using a 4D server. 4D has a built in PHP processor, but in an odd way. I have to start an inline 4D script in my webpage, then I have to call up PHP Execute, and give it the path of a .php file.

4D Script

C_TEXT($result)
C_BOOLEAN($isOK)
$isOK:=PHP Execute("C:\\php\\myPHPFile.php";"my_function";$result)
ALERT($Result)

myPHPFile.php

<?php
    function my_function() {
        return 'Hello World';
    }
?>

Goal

I want to be able to write this in my webiste:

<?php echo 'Hello World'; ?>

And have it processed. I almost have it figured out. If I first convert it via the server code (4D can do that) to this:

C_TEXT($result)
C_BOOLEAN($isOK)
$isOK:=PHP Execute("";"echo 'Hello World'";$result)
ALERT($Result) 

You are supposed to be able to run PHP native functions directly without a .php file. You'd think that would work. Turns out it doesn't.

So I tried this:

C_TEXT($result)
C_BOOLEAN($isOK)
$isOK:=PHP Execute("";"echo";$result;"Hello World") // I can send parameters
ALERT($Result) 

Still doesn't work. Turns out that I need to evaluate the code using a function.

C_TEXT($result)
C_BOOLEAN($isOK)
$isOK:=PHP Execute("";"eval";$result;"return 'Hello World;'") // I can send parameters
ALERT($Result) 

That SHOULD send "return 'Hello World';" to eval(). Well that STILL doesn't work, because I've found out that eval() isn't actually a function. So what I need to know, after all that background, is this:

Question

Is there a native function in PHP that would work like this:

$evaulatedCode = eval($unEvaluatedCode);

Solution

  • You were pretty close, you have the semicolon in the wrong place in your return statement. Try this:

    $isOK:=PHP Execute("";"eval";$result;"return 'Hello World';")