How to Use C++ With PHP and JQuery to Make Neat Apps

Am learning C (this code also works with C++) and I already know PHP so as I soon as I saw what am about to show you, I thought of how it could be used.

C and C++’s Main Function

These two languages have a function called main that gets executed at run time. This function has at least two different prototypes, one that doesn’t take arguments and one that takes two.

// first form
int main()
{

}
// second form
int main(int argc, char *argsv[])
{

}

In the second form, the first parameter, argc is an integer that holds the number of parameters that was passed to main.

The second parameter, argsv[] , holds the values of these parameters. Since this is an array, the index key starts at zero, but the the actual values you pass start at one. The zero index holds the C++ program’s name.

How to Pass Arguments to Main at Runtime

To execute a program from the command line you just type the name of the program.

mycoolprogram.exe

If you want to pass parameters to main the same syntax is used, but this time the arguments follow the program’s name

mycoolprogram.exe  param1 param2 param3

How to Run C++ Programs With PHP

PHP has a function called exec() that let’s you run server commands and compiled programs. Here is exec()’s full prototype.

string exec ( string $command [, array &$output [, int &$return_var ]] )

As you can see the only required argument is $command, this would be your C++ program’s name. The second argument, if passed, will be used to store the output of your C or C++ program. This output variable will be an array of the lines written to the output using cout or printf.

Example Program: an AJAX Controller

Let’s make a web page for a controller with four buttons, UP, DOWN, LEFT and RIGHT.
Every time you click on a button a POST request will be executed via JQuery to a PHP program, the PHP program will run a C++ program with the values of the key your pressed, the output of the program will be sent back to JQuery and displayed in an alert box.

The User Interface in Pure HTML

Left Down Right Up

A 4-Key HTML Controller

The AJAX

Here is the full code you will need to have in your controller file.
If you want to learn more about AJAX I wrote a post on this subject called Learn AJAX.

<html>
<body>
<!-- user interface code begins -->
<button value="up">↑</button>
<button value="down">↓</button>
<button value="left">←</button>
<button value="right">→</button>
<!-- user interface code ends -->

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var phpScript='controller.php';
var keyPressed='';

	$("button").click(function(){
		
		keyPressed=$(this).val(); // get value of key pressed
		
		$.post(phpScript,{data:keyPressed},function(response){
			alert(response);
		});
	});
});
</script>
</body>
</html>



The PHP Program (controller.php)

<?php
$data=$_POST['data'];
exec("controller.exe {$data}",$output);

foreach($output as $line)
{
	echo $line;
}
?>

If you are using Linux don’t forget to change the .exe extension.

The C++ Program (controller.cpp)

#include <iostream>

using namespace std;

int main(int argc, char *argsv[])
{
    cout<<"You clicked "<<argsv[1]<<endl;

}

The Result

Result when you click the left arrow

How This Could Be (Is) Used

The first thing that came to my mind was that you could use PHP to control hardware programmed in C++ through a website, you could even build cool user interfaces with Photoshop and/or flash, I think that would be neat.