hey guys,

im building a small prediction software, the GUI is in PHP and i want to do the data crushing in C. (sequence, matrix and multiple variables isolation)

i know the basics in C, but im no C expert.
Is it possible to pass from C to PHP other return type than int with main() ?
passing a table, even a int table would be nice. (no worry small table 6-12)

i didnt search, im not at that part yet.
but do you guys know good math lib for the task i mentionned up top(sequence, matrix and multiple variables)



4 Years
Discussion Span
Last Post by DarkMonarch

I assume you are using the system() function to call the C programs. This is the simplest approach, but as you can see, it is rather limited.

As it happens, PHP has another built-in function for this purpose, called exec(). It returns an array of strings which contains the standard output stream (STDOUT) of the C program; in this regard, it is somewhat similar to using a pipe (though pipes don't break up the stream into lines). All you need to do is have the C program printf() the desired values, and you can get the result when the C program returns.

If you need a binary value, however, you might want to use passthru() instead, which is similar to exec() but handles the output as a binary stream. This comparison of the three may help you understand them.

But this isn't the end of the story. There is also proc_open(), which allows bidirectional communication between the PHP process and the C process it invokes. This is a bit more complicated, but it may provide a more flexible solution.

Finally, there is a yet more flexible solution: SWIG. While it is not specific to PHP, it does support PHP as well as other languages such as Ruby and Python. As a general-purpose function interface, it is more complicated to use than the others, but if you need this sort of power, it is available.


Soku: yes i guess i could do it in java, i know java. i could of done the number crunching in PHP also i guess. nah! i just want to redo some C, its been a while and i want to push my learning, never did a cross language application. thx for the comment.

Schoil: thx man, ill try those functions and yes i was using system, i tried exec but when i was returning Test! string from C, it gave me something like 116 in PHP, maybe i wasnt using it right. I'm not familiar with pipes, but after reading the proc_open function i think i get the idea. Now what you re saying is:

if i do in PHP:

exec("prediction", $output, $return);
        echo "$return\n"; // the return int
        var_dump($output); // will be the printf output ?

and in C would look something like:

printf("%s", somestrg);
return value;

So far i was using a flat file for communication between PHP and C for none int values. Would be nice to scrap those files. hmm... i though i was ok in PHP, but looks like i still need to read somemore.

For now this helps alot, i think i get what you meant with the exec, will be of great use, and im gonna read more on the proc_open and the pipe concept.



This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.