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 Conditionals
Mar 21, 2023 programming

PHP Conditionals

Today we are going to learn about conditionals in PHP. Conditionals are very useful in any programming language. So important that we can argue that the foundation for all the technologies around us are conditions. ...

7 Min Read Read More
PHP Array Search
Feb 14, 2023 programming

PHP Array Search

Today we are going to talk about array search function in PHP. array search is a very useful function that helps you find the key of a value you’re looking for. ...

3 Min Read Read More
PHP Array Chunk
Feb 14, 2023 programming

PHP Array Chunk

Today we are going to talk about array chunk function in PHP. Sometimes you have to deal with a very large array of data. we'll learn how to split the array in different chunks. ...

6 Min Read Read More
Laravel 12 Beginner to Advanced: Complete Guide
Jul 17, 2025 programming

Laravel 12 Beginner to Advanced: Complete Guide

Laravel is a name that resonates throughout the PHP community. With its elegant syntax, robust ecosystem, and developer-friendly tools, Laravel has become the go-to framework for modern web development. Laravel 12, released on February 24, 2025, continues this tradition, streamlining workflows and boosting performance with support for the latest PHP advancements. ...

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