PHP Integration with Other Languages: Combining Technologies in One Project

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

PHP is not a loner: why combine languages?

\n\n

Have you ever written in PHP and felt that its capabilities were lacking for a certain task? Perhaps you need to quickly process a large dataset, perform complex mathematical calculations, or run a neural network? You're itching to do it, but the standard PHP libraries don't provide the necessary tool. Sound familiar?

\n\n

The good news: PHP doesn't have to work in a vacuum. It gets along perfectly with other languages. Moreover, a well-planned integration of PHP with other languages allows you to create hybrid systems where each tool does what it does best. PHP handles the web layer and business logic, Python handles data analysis, C++ handles performance, and JavaScript handles real-time operations.

\n\n

In this article, we'll explore four working ways to make PHP friends with other technologies. You'll see real code examples, understand when each approach is justified, and be able to immediately apply the knowledge in your project. Let's go!

\n\n

The simplest way: exec and system calls

\n\n

Let's start with a basic but very effective method. The exec() function in PHP allows you to run any system commands. This means you can execute scripts in Python, Perl, Node.js, or any other interpreted language.

\n\n

This approach is ideal when:

\n
    \n
  • The task is performed infrequently (e.g., generating a report once an hour);
  • \n
  • The result is not needed instantly in the user's response;
  • \n
  • You don't want to complicate the architecture with microservices.
  • \n
\n\n

Example: calling Python from PHP

\n

Let's say we have a Python script that calculates a complex mathematical function:

\n\n
# calculate.py\nimport sys\nimport math\n\nx = float(sys.argv[1])\nresult = math.sin(x) ** 2 + math.cos(x) ** 2\nprint(result)\n
\n\n

Now let's call it from PHP:

\n\n
<?php\n$x = 1.5708; // approximately π/2\n\n$output = [];\n$return_var = 0;\nexec("python3 calculate.py " . escapeshellarg($x), $output, $return_var);\n\nif ($return_var === 0) {\n    echo "Result: " . implode("\\n", $output);\n} else {\n    echo "Script execution error";\n}\n?>\n
\n\n

Important: always use escapeshellarg() for arguments — this protects against command injection. And don't forget about file access permissions.

\n\n

The downside of this method is that a new process is created for each call. This is not suitable for high-load systems. But for background tasks, it's perfect.

\n\n

Advanced level: FFI (Foreign Function Interface)

\n\n

Starting with PHP 7.4, the language introduced FFI — a mechanism for directly calling C functions from PHP. This is not just integration; it's telepathy between languages. You can load any dynamic library (.so, .dll) and call its functions as if they were native.

\n\n

Why is this needed? For example, to use high-performance C/C++ libraries for image processing, cryptography, or working with databases. FFI provides native code speed without rewriting everything in C.

\n\n

Example: calling a C function from PHP

\n

Suppose we have a simple C library that adds two numbers:

\n\n
// math.c\n#include <stdint.h>\n\nint32_t add(int32_t a, int32_t b) {\n    return a + b;\n}\n
\n\n

Compile it: gcc -shared -o libmath.so -fPIC math.c

\n\n

Now use FFI in PHP:

\n\n
<?php\n$ffi = FFI::cdef(\n    "int32_t add(int32_t a, int32_t b);",\n    "/path/to/libmath.so"\n);\n\n$result = $ffi->add(10, 25);\necho "Sum: " . $result; // Outputs: Sum: 35\n?>\n
\n\n

FFI is a powerful tool but requires caution. An error in C code can crash the entire PHP process. Use it only for trusted libraries and always test on a staging environment.

\n\n

By the way, this is an excellent way to integrate PHP with C++ — use extern "C" to export functions.

\n\n

Async

Blogs

Book Recommendations