user7116461
user7116461

Reputation:

Is there anyway to use C++/C or even python with php

suppose I have a simple PHP script where the user can enter 2 numbers $a and $b

my C++ code will add these two number a + b and return the value and I will output the result in a PHP script.

lifecycle is kinda like

php (input) -> c++ (calculation) -> php output

looking for something except

$ans =  exec('run cpp here with params')
and echo $ans 

How can I do this?

Upvotes: 0

Views: 44

Answers (1)

fvu
fvu

Reputation: 32973

If you have calculations that can benefit from the performance or features of C, it's not that hard to cast them into a php extension.

Once written, the extension will be loadable like any other extension - many php features are actually extensions, just have a look at php.ini. This also means that your C functions will be callable like regular php functions, making your code easier to understand and more robust than when you'd call the code via opaque mechanisms like system or exec.

I'd also suggest to have a look at one of the simpler standard extensions that has a functionality related to what you're after, and use that code as a template.

I have done such a conversion a couple of years ago, with the API of an in-house interprocess messaging system, it took me less than a day to get working and it was rock-solid.


If you really want to embed Python, it's already available

Upvotes: 1

Related Questions