How to execute a php script from another php script by using the shell?

php, shell, shell-exec

Solution

If you need to write a php file's output into a variable use the ob_start and ob_get_contents functions. See below:

<?php
    ob_start();
    include('myfile.php');
    $myStr = ob_get_contents();
    ob_end_clean();
    echo '>>>>' . $myStr . '<<<<';
?>

So if your 'myfile.php' contains this:

<?php
    echo 'test';
?>

Then your output will be:

>>>>test<<<<

Problem

I have a php file called `sample.php` with the following content: ``` <?php echo "Hello World!"; ?> ``` And what I want to do, is to run this php script using a second php script. I think `shell_exec` could help me, but I don't know its syntax. By the way, I want to execute this files with `cpanel`. So I have to execute the shell. Is there any way to do this?

Original source