tech74
tech74

Reputation: 1375

Is it possible to call C code from php

is it possible to call C function from 3rd party C library from a PHP5 script. If so any link or how to do it please.

Upvotes: 3

Views: 3919

Answers (4)

timdev
timdev

Reputation: 62874

PHP is written in C, so if you have some C code that you want to be able to call in PHP, the best solution is to write a PHP extension, which will allow you to expose the functionality via some new php functions.

It's been a long time since I did this, but PHP has some tools to set up the skeleton of an extension. A good place to start is probably the official documentation

Upvotes: 2

camperdave
camperdave

Reputation: 1421

There is a way to do it, but it's probably not a very good idea. One could write a small native program around the library (an executable for your system, that is). Then, you could use the php system() function to call that program. Then, read the output you need (if any) off the standard output?

(there may be a better way, I'm not super-familliar with PHP, but at least this should work.)

Upvotes: 1

Mark Biek
Mark Biek

Reputation: 150739

You can also write PHP extensions in C. Put very simply, an extension lets you write a function (or functions) in C, then call those functions from PHP.

This Zend article is a good introduction.

Upvotes: 4

Crozin
Crozin

Reputation: 44376

If you want to execute C source-code, than it's impossible. But you can compile that code and run it from within PHP using system().

Upvotes: 0

Related Questions