How to get python output and print with php?

Member

by orpha , in category: PHP General , 6 months ago

How to get python output and print with php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by filiberto , 6 months ago

@orpha 

To get the output of a Python script and print it using PHP, you can use the exec() function to execute the Python script and capture its output.


Here is an example:

  1. Create a Python script, for example, script.py with the following code:
1
print("Hello, World!")


  1. In your PHP file, use the exec() function to execute the Python script and capture its output:
1
2
3
4
$output = exec("python script.py");

// Print the output
echo $output;


  1. When you run your PHP file, it will execute the Python script and capture its output. Then, the output will be printed using the echo statement.


Make sure that the PHP server has permission to execute the Python script and that Python is installed on your server.