• Hello World Snippet

    Hello World Snippet demonstrating the simpliest PHP application

    Jakub Míšek
    Friday, 24 March 2017
    PHP Peachpie Snippet hello world
  • PHP variables

    Small example snippet with PHP variables

    Ben Fistein
    Friday, 24 March 2017
    PHP Peachpie Snippet variables
  • Leibniz Pi

    This snippet contains the formula for calculating Pi in PHP.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet leibniz pi
  • Method calling microbenchmark

    Small test sample to microbenchmark method calling

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet microbenchmark method calling
  • Type-safe usage demonstration

    Peachpie provides an incredibly fast compile time check and creates typed interfaces for .NET languages, allowing for a type safe use.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • Property assignment test code

    This code was used to microbenchmark property assignment with Peachpie against standard PHP.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet property assignment benchmark
  • Include_once

    Demonstrating how include_once is compiled.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet include
  • Conditional declaration

    This snippet includes a conditional declaration of a function, which is quite unusual in modern programming languages. Such a piece of code has to be supported by the compiler and the runtime as well.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet conditional declaration
  • Include test

    Small snippet to demonstrate how 'include' is compiled to MSIL and decompiled to C#, see http://www.peachpie.io/2016/05/the-net-effect.html.‎

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet include
  • Static local variables

    Sample snippet demonstrating how Peachpie compiles static locals in this article: http://www.peachpie.io/2016/05/static-locals.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet static local variables
  • Static locals

    This snippet shows the two basic usages of static locals: counting something and checking to ensure that we do not enter into a function more than once

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet static locals
  • Foreach

    Foreach is a powerful construct in PHP, see this article: http://www.peachpie.io/2016/06/foreach.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet foreach
  • Static fields

    Every bunch of static fields is represented as a nested class named "_statics", whose instance is lazily bound to a web request, represented as a Context class in Peachpie. Then, by accessing a static field of a class, the compiler actually generates code that retrieves instances of "_statics" from Context and accesses its instance (non-static) fields. For more information on this, please refer to http://www.peachpie.io/2016/06/static-fields.html .

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet static fields
  • Sample Azure script

    This sample script was used to benchmark PHP on Microsoft Azure in this article: http://www.peachpie.io/2016/07/azure.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • Second pass - with strong type information

    Second pass from our standard benchmark with strong type information: http://www.peachpie.io/2016/07/standard-benchmark.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • Sample compilation optimization snippet

    This optimization avoids repetitious memory copying. Since call_user_func_array() takes an associative array that has to be internally copied into a simple array, we shall use call_user_func() instead. It takes .NET params as a flat array already.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • Replacing call_user_func() with an actual functions call

    Just to see the overhead of dynamic calls caused by constant lookups into hashtables, let us get rid of call_user_func() completely. We will replace it with an actual functions call.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • SimpleDog class snippet

    Here you can see a 'SimpleDog' class and 'goodboy' global function in PHP. Notice the well-known PHPDoc annotation '@param' that we use to mark the function parameter's type, which means that the compiled methods have a specific CLI type.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet class @param CLI type
  • "Hello World" mini snippet

    Tiny "Hello world" snippet

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet hello world
  • Syntactic sugar

    This code exemplifies syntactic sugar generated by Peachpie compiler.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet syntactic sugar
  • Referencing .dll from PHP

    The dependencies in a .NET dll become seamlessly available to the PHP code with Peachpie. The compiler takes care of this process and allows you to use the C# symbols in PHP. See this article: http://www.peachpie.io/2016/12/php-dotnet-csharp-interoperability.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet dll
  • Declaration injection at runtime

    Peachpie allows you to define PHP global functions as C# lambdas dynamically at runtime. You can use a non-existing global function in your PHP code, so the compiler postpones its direct call by using 'Call Sites'. Then, we provide the function definition in the runtime before the call is made so that the runtime uses your provided method.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet declaration injection
  • Include of a non-compiled content file

    The line 'require_once( $svg_icons )' allows one to upload a file onto the host's server if not otherwise handled. Peachpie actually helps in this case, as we do not allow the Include of a non-compiled content file. The compiler recognizes that WordPress performs a Require on a file that had originally not been compiled. Rather than running it, which can potentially be harmful, it merely inserts its content into the site.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet include
  • Class extending conditionally declared class

    As of Peachpie version 0.5.0, the compiler did not support classes extending a class that is declared conditionally, such as this code snippet.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet class extension conditional declaration
  • Syntax error snippet

    This snippet is used to demonstrate a syntax error

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet error syntax
  • Warning example snippet

    This snippet shows a green underlining for a warning/recommendation.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet warning
  • Tooltip example snippet

    This snippet serves as an example of how tooltips are displayed.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet tooltip
  • Standard benchmark - first pass

    First warmup pass from our standard benchmark: http://www.peachpie.io/2016/07/standard-benchmark.html.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet
  • Anonymous classes

    Support for anonymous classes was added in PHP 7. Anonymous classes are useful when simple, one-off objects need to be created.

    Ben Fistein
    Saturday, 25 March 2017
    PHP Peachpie Snippet anonymous class
  • Send Mail using mail function in PHP

    Send Mail using mail function in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet mail
  • Base64 Encode and Decode String in PHP

    Base64 Encode and Decode String in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet base64 encode decode string
  • Get Remote IP Address

    Get Remote IP Address in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet remote ip address
  • Get IP address if client is behind proxy server

    Get remote IP address in PHP if client is behind proxy server.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet IP address proxy
  • Seconds to strings

    This function will return the duration of the given time period in days, hours, minutes and seconds. e.g. secsToStr(1234567) would return “14 days, 6 hours, 56 minutes, 7 seconds”.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet seconds strings
  • Email validation

    Snippet to validate an email in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet email validation
  • Parsing XML easily

    Required Extension: SimpleXML

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet parse XML
  • Database connection

    Database connection in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet database connection
  • Generate authentication code

    This basic snippet will create a random authentication code, or just a random string.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet authentication code
  • Date format validation

    Validate a date in “YYYY-MM-DD” format.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet date format validation
  • HTTP redirection

    HTTP redirection in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet HTTP redirection
  • Directory listing

    Directory listing in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet directory listing
  • Browser detection script

    Browser detection PHP script.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet browser detection
  • File unzipping

    Unzip a file in PHP.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet file unzip
  • Random color

    Generates a random hex color (like FF00FF).

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet random hex color
  • Sanitize database inputs

    When inserting data in your database, you have to be careful about SQL injections and other attempts to insert malicious data into the database. The function below is an efficient way to sanitize a string before using it with your database.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet sanitize database
  • Email error logs to yourself

    Instead of publicly displaying possible errors on your website, you can use a custom error handler to email error logs to yourself with this snippet.

    Ben Fistein
    Sunday, 26 March 2017
    PHP Peachpie Snippet email error log
  • Send an SMS

    For sending SMS using any language, you’d need an SMS gateway. Most of the SMS providers these days provide with an API. For the below PHP snippet, MSG91 is used as an SMS gateway. As of March 2017, Peachpie doesn't support cURL yet.

    Ben Fistein
    Monday, 27 March 2017
    PHP Peachpie Snippet sms
  • Preventing SQL injection

    An SQL injection is a common technique used to hack into a website. Using the below code can help you prevent it.

    Ben Fistein
    Monday, 27 March 2017
    PHP Peachpie Snippet SQL injection
  • Find out a website's source code

    Use this function to be able to get the HTML code of any webpage.

    Ben Fistein
    Monday, 27 March 2017
    PHP Peachpie Snippet source code
  • Generating CSV

    The following code wraps logic to construct CSV output from an array of data. It has optional parameters to allow for header columns and whether to flush the CSV directly to the browser or return the output as a string. The elegance here is the usage of streams with fputcsv() as the function requires an open file handle to operate.

    Ben Fistein
    Tuesday, 28 March 2017
    PHP Peachpie Snippet CSV generate
  • Check if server is HTTPS

    This code allows you to check if your script is running on a server with HTTPS.

    Ben Fistein
    Tuesday, 28 March 2017
    PHP Peachpie Snippet check HTTPS server
  • Get percentage

    Use this snippet to calculate the percentage of a certain value in our project.

    Ben Fistein
    Tuesday, 28 March 2017
    PHP Peachpie Snippet percentage %
  • Array operator example

    This snippet is taken straight from the PHP manual, showing how array operators work.

    Ben Fistein
    Wednesday, 29 March 2017
    PHP Peachpie Snippet array operator
  • Create a tag cloud

    This snippet creates a tag cloud, including a sample usage at the end.

    Ben Fistein
    Thursday, 30 March 2017
    PHP Peachpie Snippet tag cloud
  • Evil Eval 1

    Show of use of eval() and dynamic declaration of a class. Note this is dangerous and should not be used in production. Snippets show a corresponding warninig message.


    Friday, 31 March 2017
    PHP Peachpie Snippet eval warnings
  • Eval test #1

    This short snippet was used to test eval().

    Ben Fistein
    Saturday, 08 April 2017
    PHP Peachpie Snippet eval test
  • Eval test #2

    This snippet was used to test the eval() function.

    Ben Fistein
    Saturday, 08 April 2017
    PHP Peachpie Snippet eval test
  • PHP Sessions

    Short snippet showing our implementation of PHP sessions.

    Ben Fistein
    Sunday, 23 April 2017
    PHP Peachpie Snippet session
  • PHP Generators

    Quick snippet showing the initial support for generators.

    Ben Fistein
    Sunday, 23 April 2017
    PHP Peachpie Snippet generator
  • PHP Generator snippet

    For use in 0.7.0 article, showing our initial support of generators.

    Ben Fistein
    Sunday, 23 April 2017
    PHP Peachpie Snippet generator
  • Diagnostics error message

    Short snippet demonstrating meaningful error messages for unsupported constructs.

    Ben Fistein
    Sunday, 23 April 2017
    PHP Peachpie Snippet diagnostics
  • Type analysis update

    Newest type analysis features for blog post.

    Ben Fistein
    Monday, 12 June 2017
    PHP Peachpie Snippet type analysis
  • Unreachable code analysis

    Latest unreachable code analysis for blog post

    Ben Fistein
    Monday, 12 June 2017
    PHP Peachpie Snippet unreachable
  • Optimized/not optimized for loop

    This snippet shows one foor loop with an optimized control variable and one without.

    Ben Fistein
    Wednesday, 21 June 2017
    PHP Peachpie Snippet for loop optimized
  • Argument unpacking

    Short snippet used for a blog post.

    Ben Fistein
    Sunday, 02 July 2017
    PHP Peachpie Snippet argument unpacking
  • call error

    Invoking a member function on a non-object detected in compile time.

    Jakub Míšek
    Wednesday, 26 July 2017
    PHP Peachpie Snippet error analysis
  • Iterator/Generator

    Used for a blog post

    Ben Fistein
    Wednesday, 09 August 2017
    PHP Peachpie Snippet iterator generator
  • Array access

    Used for a blog.

    Ben Fistein
    Wednesday, 09 August 2017
    PHP Peachpie Snippet array access
  • PHP array

    Short snippet to be used in our blog about array optimizations.

    Ben Fistein
    Tuesday, 31 October 2017
    PHP Peachpie Snippet array
  • Instanceof $this converts $this to string

    Test case with an unexpected result.

    Ben Fistein
    Saturday, 02 December 2017
    PHP Peachpie Snippet instanceof string
  • Yield from example

    Small snippet showing Peachpie's implementation of `yield from`.

    Ben Fistein
    Sunday, 14 January 2018
    PHP Peachpie Snippet generators yield
  • Traits

    Trait example

    Ben Fistein
    Sunday, 14 January 2018
    PHP Peachpie Snippet trait
  • Trait example

    Mini trait

    Ben Fistein
    Sunday, 14 January 2018
    PHP Peachpie Snippet
  • Peachpie version checking

    This snippet checks whether your code runs on Peachpie and if so, on which version, and then outputs a corresponding message.

    Ben Fistein
    Monday, 15 January 2018
    PHP Peachpie Snippet version
  • Razor Partial View in PHP

    Used for our blog post.

    ben@iolevel.com
    Friday, 03 August 2018
    PHP Peachpie Snippet razor mvc
  • Razor Partial View

    Used for our blog


    Tuesday, 07 August 2018
    PHP Peachpie Snippet razor partial view mvc
  • crypt example

    Example of crypt() function.

    Jakub Míšek
    Thursday, 23 March 2017
    PHP Peachpie Snippet crypt
  • sample User and method call

    calling method on new expression

    Jakub Míšek
    Thursday, 23 March 2017
    PHP Peachpie Snippet
  • dependency injection

    this is how we do dependency injection

    Jakub Míšek
    Friday, 24 March 2017
    PHP Peachpie Snippet