Python and PHP compatibility in Web-Dvelopment, is it possible?

Question:

I’m working with legacy PHP project where all back-bones was written in PHP. I like scriptting features, useful and fun things in Python.

Question : Is it possible somehow implement Python scripts in PHP Back-end?

Want to hear experts of web-development sector.

Asked By: Ada

||

Answers:

PHP and Python are quite different languages in many fundamental respects. For starters, all variables in PHP are preceded with a dollar sign ($). Secondly, Python uses indentation to denote blocks of code whereas PHP uses curly braces like {} to denote blocks of code. PHP has different function names than Python, and different libraries, and different coding conventions. The languages are entirely different. You therefore cannot write Python code and run it with the PHP executable or write PHP code and run it with a Python executable.

You can invoke a Python script from within a PHP using the Command Line Interface (CLI) using PHP functions like exec or passthru. For example:

$output=null;
$retval=null;
exec('python3 --version', $output, $retval);
echo "Returned with status $retval and output:n";
print_r($output);

You can also invoke a PHP script from Python as described in this other post here in SO:

import subprocess

# if the script don't need output.
subprocess.call("php /path/to/your/script.php")

# if you want output
proc = subprocess.Popen("php /path/to/your/script.php", shell=True, stdout=subprocess.PIPE)
script_response = proc.stdout.read()
)
Answered By: S. Imp
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.