Run c program using php

how to run the c program using php exec() i saw a lot of google search results it is possible using the exec () function but i can't use it i tried so hard i wrote the program in c

 **myc.c** #include <stdio.h> int main(int argc, char *argv[]) { printf("Hello, world\n"); return 0; } **test.php** <?php exec('myc.c'); ?> 

can someone help me in this regard? I did this through the Wamp server by placing in the www folder

+8
source share
4 answers

You must compile your C program and then execute it with PHP. PHP will not run your C code, even if they have similar syntax.

The PHP exec function will execute a command similar to that executed in a shell or command line.

+8
source

Program C

 #include <stdio.h> int main(int argc, char **argv) { if(argv[1]) printf("First arg %d\n", argv[1]); if(argv[2]) printf("Second arg %d", argv[2]); return 0; } 

PHP code

 <?php exec("testone.exe 125 70", $out); print_r($out); ?> 

Combined output:

 <!-- Array ( [0] => First arg 27 [1] => Second arg 27 ) --> 
+8
source

If you want to run a program written in C, you need to run the program, not the source code.

Compile your C into an executable, then call the name of the executable from exec .

+7
source

The PHP ecosystem today offers some of the best solutions for this.

1. Write a PHP extension

You can use Zephir to easily create PHP extensions in a PHP-like language. They provide a mechanism called custom optimizers that clearly associates custom C libraries or custom C code with your extension.

Although this requires a bit more work, it will lead to performance close to C using native PHP calls and will update your PHP fu. You can also accidentally fall in love with Phalcon.

This Github comment provides a brief overview of why you should use the Zephir optimizer on top of the built-in CBLOCK and how to structure your directories / files.

2. Use FFI

Starting with PHP 7.4, the external function interface is supplied built-in. You can also install it as an extension for older versions of PHP.

Top? You do not need to write your own PHP extension in order to use C code. Back side? Currently, FFI's performance cannot match the real C code. You can think of it more like a crutch than a Batmobile.

If you care about high performance, try option 1.

0
source

Source: https://habr.com/ru/post/922115/


All Articles