3

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);
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117
Kelderic
  • 6,502
  • 8
  • 46
  • 85
  • Just, out of curiosity, where is `$Result` declared/initialized? – Brandon White Sep 24 '15 at 20:59
  • `C_TEXT($result)` is the declaration. – Kelderic Sep 24 '15 at 21:00
  • Yes, that is why I need a function that would work like `eval`, so that I can put it in there. – Kelderic Sep 24 '15 at 21:03
  • To answer the question part `eval("\$evaulatedCode = $unEvaluatedCode");` – AbraCadaver Sep 24 '15 at 21:06
  • Never heard about that. If you ask me, one of the things you don't need for web development is that proprietary 4D stuff. – hek2mgl Sep 24 '15 at 21:06
  • Abra, would the `$evaluatedCode` be returned? – Kelderic Sep 24 '15 at 21:07
  • hek, the main reason I'm asking is so that on the web pages, I can just write PHP instead of having to write in the proprietary 4D stuff. – Kelderic Sep 24 '15 at 21:08
  • No `$evaluatedCode` will be assigned the return of the evaluated code and be available in global scope. I was answering the stated question, I have no knowledge of 4D. – AbraCadaver Sep 24 '15 at 21:09
  • @AndyM Got your point. Believe me, I've worked >10 years in web dev but I've never heard of 4D before. I guess you are doing it on customer's request. (I pity you in that case!) Otherwise I would simply not use that 4D stuff. (Note that I've removed the 4d tag, because it relates to 4d in the sense of 4 dimensions. Check the tag description.) – hek2mgl Sep 24 '15 at 21:53
  • If you want to capture the *output* of eval, then use `ob_start` etc. and `eval("?>$script – mario Sep 24 '15 at 22:09

1 Answers1

2

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';") 
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Doesn't work. `eval` was my first thought too, as I explained in the question. But `eval` isn't a function. It doesn't return a value. I think as I review this more that it isn't possible to do what I want. – Kelderic Sep 24 '15 at 21:19
  • Actually, now that I recheck the documentation, I think your `return` statement should have worked. The manual says: _eval() returns NULL unless return is called in the evaluated code, in which case the value passed to return is returned._ – Barmar Sep 24 '15 at 22:42
  • That's odd then that it doesn't work. I'm wondering if 4D just isn't treating the eval statement as an official "function". – Kelderic Sep 25 '15 at 12:13
  • I suppose anything is possible if it has its own PHP interpreter instead of using the real thing. Did you fix the semicolon as I showed? You had it inside the single quotes. – Barmar Sep 25 '15 at 15:33