I have a php file that works well when I run it on browser but when I schedule it to run via cpanel cronjob, strangely it doesn’t work. I spoke at length with the hosting company(namecheap) but they insisted the cron environment was ok, and the problem might be from my code. So I decided to debug my code thoroughly to find out which functions were preventing the file from running via cronjob. This is the code I used for the debugging:
$funcs = ['str_starts_with', 'str_ends_with', 'writegames', 'is_array'];
foreach($funcs as $fun) {
if(function_exists($fun)){
echo $fun." existsn";
} else {
echo $fun." doesn't existn";
}
}
//NB: writegames is a custom function I wrote, so it exists
Here is what I found out: ALL the functions echo ‘EXISTS’ when I run this code on browser, but when I schedule it via cpanel cronjob, ‘str_starts_with’ and ‘str_end_with’ echo ‘DOESNT EXIST’, how is this even possible? I have PHP 8.0 enabled on my cpanel, I was wondering if there’s some other thing which I was supposed to set which I didn’t set or should I move to PHP 8.1? At this point, I’m very confused. Thanks.
2
Answers
Just like @LawrenceCherone and @rickdenhaan pointed out in the comments. My webserver was running on PHP 8 while my cron was running on PHP 7 because the cpanel was configured to PHP 7.
I wrote to the Hosting agency to configure my cpanel to PHP 8 which they did within seconds and everything started working fine again.
Alternatively, assuming I didn't choose to configure my cpanel to PHP 8, this is the code I would have used, for PHP 7 up to PHP 4:
To test it:
you can write the functions yourself.
how to and test video