Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run C++ script in PHP

Tags:

c++

json

php

I have a C++ program that needs to take data from a PHP script, process it, and return the data to my PHP script.

  1. How do you pass the values from PHP to C++?
  2. How do you run the C++ script? Do you have to compile it first some how?
  3. How do you get the values out of the C++ script?
like image 847
Don P Avatar asked Aug 03 '12 04:08

Don P


People also ask

How to run c code in PHP?

If you want to run a program written in C then you have to run the program and not the source code. Compile your C into an executable, then call the name of the executable from exec . Show activity on this post. You should compile your C program and then execute it with PHP.

How do I run a C script?

Step 1: Open turbo C IDE(Integrated Development Environment), click on File and then click on New. Step 2: Write the C program code. Step 3: Click on Compile or press Alt + F9 to compile the code. Step 4: Click on Run or press Ctrl + F9 to run the code.


2 Answers

1 . How do you pass the values from PHP to C++?

Ans: In php file, you could use exec function to execute your C++ binary file. Example:

exec("/path/to/your/binary $var1 $var2", $output);

2 . How do you run the C++ script? Do you have to compile it first some how?

Ans: Of course, you can't execute the C++ script directly, C++ is compiled language, you could just execute the binary file.

3 . How do you get the values out of the C++ script?

Ans: See 1, you will get the output form C++ by $output.

like image 64
xdazz Avatar answered Oct 18 '22 23:10

xdazz


1- you can use exec to call external application, use command line parameters

2- C++ is not scripting, its a compiled language, you have to compile it first

3- normal std out will be captured by php exec function

like image 2
Hawili Avatar answered Oct 19 '22 01:10

Hawili