We have recently come across a phenomenal tool that illustrates the power of Roslyn. And we immediately knew we had to make it work with Peachpie.
SharpLab (formerly TryRoslyn)
Some time ago, we stumbled across a great project called SharpLab by Ashmind. It used to be called TryRoslyn, because, well, it let’s you try Roslyn, Microsoft’s compiler platform. The tool serves as both, a .NET code playground and learning platform that helps developers understand the mechanics of .NET. SharpLab shows exactly what all the .NET languages are compiled into, including the intermediate steps and the results. It lets you visualize the code just as the compiler sees it. Currently, SharpLab offers the classic .NET languages – C#, Visual Basic and F# – and lets you decompile/disassemble into four different targets: C#, Visual Basic, IL and JIT Asm (Native Asm Code).
Enter try.peachpie.io. Since Peachpie runs on Roslyn as well, there is no reason why PHP couldn’t be added to this great tool. There are many reasons for us to be excited about this new addition. First and foremost, it’s a perfect illustrator of what exactly happens to your PHP code when you compile it with Peachpie – and it shows everything in real time! We received many questions since we started the project about what exactly compiles to what, and try.peachpie.io should finally provide all the answers. Secondly, you can take advantage of this tool to quickly try out some short code snippets without having to set up projects in an IDE. It will show you exactly what the compiler outputs in mere milliseconds. Let’s take a look at a small snippet of code:
As you can see, the Peachpie-modified version only allows PHP as the input language, whereas the original SharpLab also offers all standard .NET languages (and soon PHP). In the output window, you will see MSIL by default, as this is the actual compilation output. However, you can also decompile the code to VB or C# or even display the syntax tree.
And finally, try.peachpie.io is a tool that allows you to really dig deep in your code and fine tune it. Since it offers a debug and release mode, you can perfectly identify optimization spots and unnecessary pieces of code.
Embedding Peachpie code
With SharpLab’s built-in mechanism to encode all the information with Base64, you can easily embed your code in your blogs and websites. Just as we embedded the snippet above, you can also build snippets into your blogs or websites using the simple
iframe mechanism. And so this:
Pretty neat, right? Just beware that when you’re embedding this code, the source URL protocols must match, i.e. if your site has an SSL certificate, the source URL should also be
https to match, and vice versa.
Let us know what you think and enjoy this new playground!