Free cookie consent management tool by TermsFeed Generator PHP Interacting with Python | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP Interacting with Python

PHP Interacting with Python

Last Updated on Mar 22, 2023

Introduction

Both PHP and Python are very powerful languages. Sometimes you might want to run a python program from your PHP code. It might be a machine learning algorithm, a crawler or anything else.

It’s very easy to do that.

Exec

Do you remember when we talked about running system commands?

We can use the same command and run a python program

Here my python file looks like this:

print("Hello My Friend!")

I can now use the exec function run the command and store the output and then echo that in php like this:

exec('python ./sayHello.py', $output);
// output is: Array ( [0] => Hello amir ! )
echo $output[0];
// Hello My Friend!

It’s always a good idea to escape all the shell commands. To do that you can use escapeshellcmd Function.

This escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands.

Security

So let’s make our code more secure:

$command = escapeshellcmd('python ./sayHello.py);
exec($command, $output);
echo $output[0];
// Hello My Friend!

Arguments

Sometimes your python code might accept some command line arguments. We can also pass those arguments from our PHP code.

My updated python code looks like this:

import sys
if len(sys.argv) > 1:
   name = sys.argv[1]
   print("Hello",name.capitalize(),'!')
else:
   print("Hello My Friend!")

Now if I pass the name it says hello and the name but if I don’t pass any name it will say Hello My Friend

Now let’s pass the name with PHP

$command = escapeshellcmd('python ./sayHello.py amir');
exec($command, $output);
echo $output[0];
// Hello Amir !

And if I don’t pass the name:

$command = escapeshellcmd('python ./sayHello.py');
exec($command, $output);
echo $output[0];
// Hello My Friend!

https://youtu.be/BtP-oa6FtXI

Conclusion

Now you know about running a python code from PHP.

I recommend you to open a PHP files and try to run a simple python code from PHP. then try to pass arguments and parse the response.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • run a python code from php
  • how to make system commands more secure
  • exec and escapeshellcmd function

Category: programming

Tags: #php #python #tips and tricks

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

PHP Operators
Mar 21, 2023 programming

PHP Operators

Today we are going to learn about Operators in PHP including arithmetic, comparison and logic operators. You will be using all of these operators during your career a lot. ...

5 Min Read Read More
Top Open Source Licenses: Understanding Your Options
May 07, 2024 programming

Top Open Source Licenses: Understanding Your Options

Open source licenses dictate how software can be used, modified, and distributed. Understanding the different types of licenses is essential for developer. ...

9 Min Read Read More
Creating a Simple Jupyter Notebook in PHP
Aug 28, 2024 programming

Creating a Simple Jupyter Notebook in PHP

This tutorial will guide you through the steps to create a simple PHP-based notebook interface. The notebook allows you to write and run PHP code in a web browser, maintaining the state between code executions. ...

28 Min Read Read More
TypeScript Beginner to Advanced 2025: The Complete Guide
Jul 18, 2025 programming

TypeScript Beginner to Advanced 2025: The Complete Guide

TypeScript is no longer optional for serious JavaScript developers, it’s essential. Whether you're building modern front-end applications with React, scaling backend services with Node.js, or contributing to large open-source projects, TypeScript is the industry standard in 2025. ...

23 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...