I was wondering if there was a way to call existing C code from php?
2 Answers
If your C code is compiled to an executable, that executable can be executed from PHP -- but I'm guessing this is not quite what you meant ?
If you want to excute C code from PHP : that is not possible... unless you compile your C code to a PHP extension.
A PHP extension is like a .dll
, on windows : it's a library, that can export functions that can be called from PHP.
If you want to learn more about writing PHP extensions, those links will probably interest you :
- Extension Writing Part I: Introduction to PHP and Zend
- Extension Writing Part II: Parameters, Arrays, and ZVALs
- Extension Writing Part II: Parameters, Arrays, and ZVALs [continued]
- Extension Writing Part III: Resources
- And, just for reference, as i'ts about C++ and not C : Wrapping C++ Classes in a PHP Extension
And, if you are really interested by the subject, and ready to spend some money on it, you could buy the book Extending and Embedding PHP (some pages are available as preview on Google Books too) ; It's considered as the book to read when interested on this subject (In fact, I've bought it some time ago, and, in my opinion, it is indeed an interesting read)
BTW, the author of that book is also the author of the first four articles I linked to ;-)

- 20,399
- 5
- 40
- 46

- 395,085
- 80
- 655
- 663
This article talks about PHP-C++ bindings. May be of help. In particular, checkout the BEGIN_EXTERN_C/END_EXTERN_C macros.

- 108,024
- 16
- 131
- 187