5

I am having big c program.I want execute that function in php and get value

For example

C program

int add( int, int);         

void main()
{
  int i=1;
  printf("i starts out life as %d.", i);

  i = add(5, 10);           

  printf(" And becomes %d after function is executed.\n", i);
}


int add( int a, int b)          
{
  int c;
  c = a + b;
  return c;
}

my web form has value a and b. so when i submit form i want to call the c function add and get the output.

I know there is a function to execute external programs in php like

exec()
shell_exec()

But i am not familiar with this functions.so please give me sample of code. Should i place the c program file(in notepad) in server root folder.

Please guide me ! Thanks in advance

4
  • 2
    Things don't work like that. You need to structure your C application to parse input and then process it and return results. You can't just call C code from PHP. Commented Jul 15, 2011 at 7:55
  • @Alin:How can i do that i am good with c . can give me piece of code and instruction Commented Jul 15, 2011 at 7:57
  • @gwori: Simlpest solutions are to send the parameters as command line arguments or send them via stdin. Other approaches would be to write the C program as a socket daemon and run the PHP code as a client or implement a PHP extension in C. But the latter 2 methods are a lot more complex Commented Jul 15, 2011 at 8:10
  • A small correction. As @symcbean pointed out, you can call C code from PHP if you put it inside an extension. But I doubt this should be done in your particular situation. Commented Jul 15, 2011 at 8:18

2 Answers 2

6

I guess the simplest way would be to call your C from PHP, passing the parameters as arguments. On the C side:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[])
{
    int i = add(atoi(argv[1]), atoi(argv[2]));
    printf("%d\n", i);
    return 0;
}

(obviously, you should add error checking). On the PHP side:

$a = ...;
$b = ...;
$c = exec("/path/to/sum $a $b");

assuming your C program is called sum.


Edit: Just adding a comment about the various approaches that have been suggested to you so far:

  • Starting your C program with exec(), as in my answer above, is really the simplest solution. However, it costs you the creation of a new process every time you call your C code, which can be expensive if you do it a lot.

  • A PHP extension spares the process creation and should be more efficient, especially if you are making many calls to your C code and your C code is fast to compute the result.

  • A daemon is more interesting if your C program is slow to startup (long initialization) but can then process queries fast.

Sign up to request clarification or add additional context in comments.

Comments

0

There's a way to do that - but it's not trivial by any means. You need to write a php extension in c/c++, install it on the server where the php will be executed and update you php.ini - and then you'll have access to the c/c++ functions directly from php. Have a look here: http://devzone.zend.com/article/1021 about how to write extensions.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.