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." exists\n";
} else {
echo $fun." doesn't exist\n";
}
}
//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.
CodePudding user response:
you can write the functions yourself.
<?php
//str_starts_with
if(!function_exists('str_starts_with')) {
echo 'str_starts_with doesn\'t exist<br/>';
function str_starts_with($haystack,$needle,$trim_haystack=false) {
//str_starts_with(string $haystack, string $needle): bool
if($trim_haystack) {
$haystack = trim($haystack);
}
$strlen_needle = mb_strlen($needle);
if(mb_substr($haystack,0,$strlen_needle)==$needle) {
return true;
}
return false;
}
}
//str_ends_with
if(!function_exists('str_ends_with')) {
echo 'str_ends_with doesn\'t exist<br/>';
//str_ends_with(string $haystack, string $needle): bool
function str_ends_with($haystack,$needle,$trim_haystack=false) {
//str_starts_with(string $haystack, string $needle): bool
if($trim_haystack) {
$haystack = trim($haystack);
}
$strlen_needle = mb_strlen($needle);
if(mb_substr($haystack,-$strlen_needle,$strlen_needle)==$needle) {
return true;
}
return false;
}
}
// Tests:
if(str_starts_with('hello world','hello')) {echo 'start with hello';}
echo '<br/>';
if(str_ends_with('hello world','world')) {echo 'end with world';};
CodePudding user response:
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:
function str_starts_with($val, $str) {
if(mb_substr($val, 0, mb_strlen($str)) == $str) {
return true;
} else {
return false;
}
}
function str_ends_with($val, $str) {
if(mb_substr($val, -mb_strlen($str), mb_strlen($str)) == $str) {
return true;
} else {
return false;
}
}
To test it:
$string = 'stackoverflow';
$start = 'stack';
$end = 'flow';
if(str_starts_with($string, $start)) {
echo 'yes'; //echoes yes
} else {
echo 'no';
}
if(str_ends_with($string, $end)) {
echo 'yes'; //echoes yes
} else {
echo 'no';
}