Evil eval() is back for a third edition – this time we’ll take a look at it from a more user-friendly perspective: debugging in Visual Studio.
eval()
is a commonly used construct in dynamic languages such as PHP, providing a simple way to allow running dynamically generated code. But how can you debug it if there is no corresponding source file?
PeachPie – the alternative runtime for PHP code under the modern .NET platform – provides a simple, yet efficient way of handling eval’ed code. By using PeachPie to compile and then run your PHP program or website, you can take advantage of neat debugging capabilities, such as debugging eval’ed code or functions created using create_function()
.

Visual Studio or Visual Studio Code
Let’s start with a project in Microsoft Visual Studio for Windows. Make sure you have the PeachPie for Visual Studio extension that, among other features, provides a simple wizard for the creation of a new project.

File | New | Project (Ctrl+Shift+N)
Bring up the New Project dialog and choose one of the provided PeachPie project templates as depicted above. Then edit the code by putting an eval()
construct in it.

Shows Solution Explorer (right) and code editor (left) with
eval
and a breakpoint.For additional information on how to create a project and get started with PeachPie, see the quick guide at peachpie.io/getstarted.
eval()
Notice the code contains a use of the eval()
construct, similar to the code below:
<?php $code = ' echo "Hello from eval!"; echo "This code is being dynamically evaluated"; '; eval($code);
Put a breakpoint (F9
) on the line with eval($code);
and run the program (F5
). The project gets compiled and executed. In case of an error in the code, the compilation fails, which will prevent you from running the program.

When paused on the line with eval()
, you can step into (F11
) the code. Visual Studio will show the source code, allowing for further actions – stepping through, inspecting variables etc.

In case the eval’ed code is more complex (e.g. it contains classes or functions) and you need further inspection, feel free to put another breakpoint in this editor window.

Continue running and hit the break-point (right).
create_function()
PHP also has an older, deprecated construct called create_function()
. It creates dynamic code that declares a function with a specified body and parameters, and gets you a random identifier, which can be used to call the dynamically created function. Below is a sample code that creates a function, prints its random identifier and then calls it with a parameter.

create_function()
and pausing just before calling the dynamically created function.When paused just before the function call, you can step into (F11
) the code and the debugger will show the content of the dynamic function.

Conclusion
And that’s it! Not only can PeachPie compile dynamic code, such as eval()
or the already deprecated create_function()
, it even allows you to debug this code in Visual Studio. Let us know what you think.