PHP VS HHVM

What is HHMV?

HHMV is an open-source virtual machine designed for executing PHP and HACK programs. HHMV uses JIT compiler for better performance. It supports Hack, PHP 5 and major features of PHP 7.

Facebook started HHMV tool in 2008, for converting PHP code into C++ so it can be compiled and run on the web server with use of less server resource. Facebook developed the Hip Hop Virtual Machine (HHVM).

Comparison between PHP and HHVM

No doubt HHVM is better than PHP 5. its execution time is far better than PHP 5 But while we compare it with PHP 7 it fails. due to PHP 7 uses the standard PHP interpreter, free software that is available for anyone to use, to directly interpret and execute PHP code on the server. This generates HTML code, which is then sent to the client. The client then displays the desired content to the web user, while the Hip Hop Virtual Machine first converts PHP code into Hip Hop bytecode. This code is then translated into machine code and executed. Some optimization takes place during this translation, ironing out inefficiencies in PHP code with the aim of delivering faster performance.

Benchmark Testing

For Benchmark Testing i have written the following very simple example to compute the exicution time of php script, running both using PHP and HHMV.

<php
	$time_start = microtime(true);

	$arr = array();
	for($i= 0; $i < 10000000; $i++){
		array_push($arr, $i);
	}

	$time_end = microtime(true);
	$execution_time = ($time_end - $time_start);

	echo "\nTotal Execution Time:\t ".$execution_time." Secs\n";
?>
		
Results

While execute the above code I found these results.

#Running with PHP 7
root@vishal:/var/www/html/hhvm# php index.php 
Total Execution Time:	 0.792 Secs
	
#Running with HHMV
root@vishal:/var/www/html/hhvm# hhvm index.php 
Total Execution Time:	 1.345 Secs
	
#Running with PHP 5
root@4a3d82b619f4:/var/www/html/hhvm# php index.php 
Total Execution Time:	 9.657 Secs
	

Create Shortcode in WordPress

Shortcode

The Shortcode is the simple custom functions in WordPress. You can use shortcodes in posts and pages. Here I am continuing with the last post on creating plugins.

Write down the following code in functions.php file inside your custom plugin folder.

function printHelloWord(){
    return "<p>".Hello World!."</p>";
}

add_shortcode("hello-world", "printHelloWord");

Here we have created a shortcode named “hello-world”. its function defenition is in printHelloWord function.

on WordPress Pages and WordPress Posts, you can use this shortcode like below. It will simply echo “Hello World!”.

[hello-world]

if you want to interact with a database you can use following reference code.

Use one of three defined constants. Default is OBJECT
1: - OBJECT - the result will be output as an object.
2: - ARRAY_A - the result will be output as an associative array.
3: - ARRAY_N - the result will be output as a numerically indexed array.

add_shortcode(getOptionValue, function($params){
    global $wpdb;
    $id = $params["id"];
    $res = $wpdb->get_results('select * FROM wp_options WHERE option_id = '$id limit 1;', ARRAY_A );
    return "<p>".$res["option_name"]."</p>";
});

Usage is like

[getOptionValue id=1]

Here we are passing params to our shortcode you can also pass more params as per your need. There are multiple options available for output type of query like here ARRAY_A.