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

PHP Exec

Last Updated on Mar 22, 2023

Introduction

Did you know we can run system commands through PHP?

Well not only it’s possible but also it’s very easy.

You can easily run your commands with exec function

exec

exec(command,output,result code)

command: the command you want to run. Like mkdir test

output: if there is any output for your command it will be saved on this variable

resultCode: the code that will be returned from running your command.

So let’s see a couple of examples example

Let’s create a directory called test

<?php
exec('mkdir test', $output, $resultCode);
// output is an empty array because there is no returned value
// resultCode is 0

Let’s get the user.

<?php
exec('whoami', $output, $retval);
// resultCode is 0
// output is:
// Array ( [0] => root )

see how easy it is?

The possibilities are endless!

https://youtu.be/Qcw4sbV-KcQ

Conclusion

Now you know about exec in PHP.

I recommend you to open a PHP files and try to run different system commands with PHP.

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

Key takeaways

  • run system commands
  • exec function

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

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

Related Posts

PHP curl
Mar 22, 2023 programming

PHP curl

Today we are going to talk about curl in PHP. Sometimes we need to send requests to another url to get data, maybe another API. So how can we do that in PHP? curl is the answer. ...

6 Min Read Read More
PHP Splat Operator
Feb 15, 2023 programming

PHP Splat Operator

Today we are going to talk about splat operator in PHP. Splat operator is when we add three dots before the name of the variable and it's way more useful than you think. ...

7 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
Docker Beginner to Advanced 2025: The Complete Guide for Developers
Jul 18, 2025 programming

Docker Beginner to Advanced 2025: The Complete Guide for Developers

Containers have transformed the way we develop, deploy, and scale applications. Docker leads the charge, offering a developer-friendly platform to create consistent, portable environments from development to production. ...

27 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 ...