I need to pass a value from PHP to C++. I think I can do with PHP's passthru()
function.
Then I want C++ to do something to that value and return the result to PHP.
This is the bit I can't work out, does anyone know how to pass data from C++ to PHP?
I'd rather not use an intermediate file as I am thinking this will slow things down.

- 5,753
- 72
- 57
- 129

- 2,896
- 7
- 44
- 54
4 Answers
You could have your c++ app send its output to stdout, then call it from PHP with backticks, e.g.
$output=`myapp $myinputparams`;

- 50,477
- 20
- 96
- 125

- 295,876
- 54
- 310
- 348
-
Thank you very much to all answer-ers, I expect I will need to graduate to the more flexible solutions in time but for now this has worked. Thanks. – Columbo Jun 09 '09 at 16:15
wow thanks a lot to Columbo & Paul Dixon.
Now I can run php to call c++ thn pass back value to php~ =)
Here I provide a sample cpp & php for it:
A simple program for adding up inputs:
a.cpp (a.exe):
#include<iostream>
#include<cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
int val[2];
for(int i = 1; i < argc; i++) { // retrieve the value from php
val[i-1] = atoi(argv[i]);
}
int total = val[0] + val[1]; // sum up
cout << total; // std::cout will output to php
return 0;
}
sample.php:
<?php
$a = 2;
$b = 3;
$c_output=`a.exe $a $b`; // pass in the two value to the c++ prog
echo "<pre>$c_output</pre>"; //received the sum
echo "Output: " . ($output + 1); //modify the value in php and output
?>
output:
5
Output: 6

- 307
- 1
- 5
- 14
This article about wrapping C++ classes in a PHP extension may help.
EDIT: all the solutions in other answers are far simpler, but less flexible. It all depends on what you need.

- 2,005
- 13
- 12
-
I would say if anything a socket connection is more flexible, you can send binary data anywhere to do anything :) Your solution may be better in another sense in that it integrates php and c++ code which may or may not be more what the OP is asking. – Doug T. Jun 09 '09 at 15:43
-
On the other hand, you still need to serialize/deserialize any data you want to send, which IMO qualifies as less flexible. The advantage of using a language's extension interface is that you get to handle the language's data structures directly--for sockets you "only" get strings of bytes. – Jacob B Jun 09 '09 at 16:20
-
Out of these three methods which in your opinios would be the quickest. To me it appears that the backticks method has to start and execute the C++ script whereas the sockets and (I think) wrapper methods rely on C++ programs that are memory resident and so can respond quicker. Does that seem reasonable or have I got it wrong? – Columbo Jun 09 '09 at 16:33
-
Yes, that's correct. Wrapper script is faster than sockets by a little tiny bit--there's some extra overhead due to serialization and memory allocation. Backticks have to not only start the C++ script but also, I believe, invoke the shell. – Jacob B Jun 09 '09 at 17:03
If you need to communicate to a running C++ program, a socket connection over the localhost might be the simplest, most platform-independent and most widely supported solution for communicating between the two processes. Sockets were traditionally built to communicate over network interfaces, but I have seen them used in many cases as a method of doing a pipe. They are fairly ubiquitous and supported in most modern languages.
Here's a C guide for socket programming. for your C++ program.

- 64,223
- 27
- 138
- 202