PHP is not a loner: why combine languages?
\n\nHave 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\nThe 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\nIn 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\nThe simplest way: exec and system calls
\n\nLet'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.
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
Example: calling Python from PHP
\nLet'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\nNow 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\nImportant: always use escapeshellarg() for arguments — this protects against command injection. And don't forget about file access permissions.
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\nAdvanced level: FFI (Foreign Function Interface)
\n\nStarting 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\nWhy 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\nExample: calling a C function from PHP
\nSuppose 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\nCompile it: gcc -shared -o libmath.so -fPIC math.c
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\nFFI 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\nBy the way, this is an excellent way to integrate PHP with C++ — use extern "C" to export functions.
\n\n