Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run PHP file in Command Line using PHP script

I have one simple PHP file which I am running from command line like

php my-file.php

and its working fine without any issue. I want run another file when some event happen like below

<?php
echo " hello this is sample";
exit();
// I want run another file here after exit command called 
next-file.php
echo "next-file is successfully running";
?>

Let me know if someone can give idea how can I achieve this? Thanks

like image 804
Mira Avatar asked Dec 18 '25 17:12

Mira


1 Answers

Use include_once 'next-file.php'; to import and run the code.

Then related to PHP Doc, here is the definition of exit():

Terminates execution of the script. Shutdown functions and object destructors will always be executed even if exit is called.

So add this code line into the shutdown function as below:

function shutdown()
{
  include_once 'next-file.php';
  echo "next-file is successfully running", PHP_EOL;
}

register_shutdown_function('shutdown');

Be sure to record this function before the exit().

like image 162
Jonathan Gagne Avatar answered Dec 20 '25 09:12

Jonathan Gagne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!