13

how to run c program using php exec() i have seen many results from google search it is possible by exec() function but i am unable to use it i tried in this way i wrote a 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 any one help me pls in this regard i executed this through wamp server by placing in www folder

2
  • c is a programming language so you need to compile before executing..where php is scripting language so you can execute directly... Commented Aug 4, 2012 at 12:42
  • 1
    You're trying to run you C source code. Good luck... Commented Aug 4, 2012 at 12:58

4 Answers 4

10

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.

Sign up to request clarification or add additional context in comments.

Comments

9

C Program

#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 ) -->

Comments

9

Today's PHP ecosystem provides a couple better solutions for this now.

1. Write a PHP extension

You can use Zephir to easily build PHP extensions in a PHP-like language. They provide a mechanism called custom optimizers that cleanly binds arbitrary C libraries or custom C code to your extension.

While it requires a little more work, it could (YMMV) result in near-C performance via native PHP calls, and upgrades your PHP-fu. You also might accidentally fall in love with Phalcon.

In my personal experience, pure Zephir actually harmed the performance of pure PHP for my task, so I would recommend it only if you want to leverage the power of existing C libs, or you have heavy workloads.

This Github comment provides a brief overview of why you should use the Zephir optimizer over an inline CBLOCK and how to structure your directories/files.

2. Use FFI

As of PHP 7.4, the Foreign Function Interface comes built-in. You can also install it as an extension for older versions of PHP.

The upside? You don't have to write your own PHP extension to use C code. The downside? Right now FFI performance doesn't really compare to actual C code. You can think of it more like a crutch than a batmobile.

If you care about high performance, try option 1.

Comments

8

You should compile your C program and then execute it with PHP. PHP will not run your C code, even though they have similar syntaxes.

The PHP exec function will execute a command similar to how it is done in a shell or command prompt.

2 Comments

You could exec gcc -o myc myc.c and then exec ./myc but then you will be at serious risk of ending up on the daily wtf :P
pls explain the process detail

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.