When I try to open the site, the HTTP 500 error appears, and the Laravel log shows: "A non well formed numeric value encountered"
PHP version: 7.4
Here's a value of $sec
before casting intval(($sec / 60) % 60)
.
Value is 60.316000
and type of this value: string
.
Here is the content of the index.blade.php file: https://pastebin.com/Cv8hsndU
Here is the content of the helpers.php file: https://pastebin.com/1TSY2rTC
Any idea how can I fix this, in file index.blade.php
where this function called (strings 29,50,86,122,150), code that's call it looks like this:
@if($recomment != NULL)
@foreach($recomment as $result)
<?php $video_categories = get_video_form_cat($result->ID) ?>
@if(count($video_categories)>0)
<h2>Recommended Category For You <a style="text-decoration: none" href="{{URL('categories/')}}/{{$result->ID}}.{{$result->post_name}}.html"><span>-{{$result->title_name}}</span></a></h2>
<div class="row content-image">
@foreach($video_categories as $result)
<?php $rating=GetRatingVideo($result->string_Id);?>
<div class="col-xs-6 col-sm-4 col-md-3 image-left">
<div class="col">
<div class="col_img">
<span class="hd">HD</span>
<a href="{{URL('watch')}}/{{$result->string_Id.'/'.$result->post_name}}.html">
<img src="{{$result->getImageUrl($result->poster)}}" alt="{{$result->title_name}}" height="177" />
</a>
<div class="position_text">
<p class="icon-like"></p>
<p class="percent">{{floor($rating['percent_like'])}}%</p>
<p class="time_minimute">{{sec2hms($result->duration)}}</p>
</div>
</div>
<h3><a href="{{URL('watch')}}/{{$result->string_Id.'/'.$result->post_name}}.html">{{str_limit($result->title_name,30)}}</a></h3>
</div>
</div>
@endforeach
</div>
@endif
@endforeach
@endif
Here's function that's causing this error (helpers.php):
function sec2hms ($sec, $padHours = false)
{
$hms = "";
$hours = intval(intval($sec) / 3600);
if($hours > 0){
$hms .= ($padHours)? str_pad($hours, 2, "0", STR_PAD_LEFT). ':': $hours. ':';
}
$minutes = intval(($sec / 60) % 60);
$hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
return $hms;
}
Here's a full error message from Laravel log:
Next ErrorException: A non well formed numeric value encountered (View: /var/www/clients/client0/web3/web/resources/views/home/index.blade.php) in /var/www/clients/client0/web3/web/app/Helper/helpers.php:35
Stack trace:
#0 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/Engines/PhpEngine.php(44): Illuminate\View\Engines\CompilerEngine->handleViewException()
#1 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/Engines/CompilerEngine.php(58): Illuminate\View\Engines\PhpEngine->evaluatePath()
#2 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/View.php(135): Illuminate\View\Engines\CompilerEngine->get()
#3 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/View.php(106): Illuminate\View\View->getContents()
#4 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/View.php(80): Illuminate\View\View->renderContents()
#5 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Http/Response.php(51): Illuminate\View\View->render()
#6 /var/www/clients/client0/web3/web/vendor/symfony/http-foundation/Response.php(198): Illuminate\Http\Response->setContent()
#7 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(1229): Symfony\Component\HttpFoundation\Response->__construct()
#8 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(112): Illuminate\Routing\Router->prepareResponse()
#9 [internal function]: Illuminate\Routing\ControllerDispatcher->Illuminate\Routing\{closure}()
#10 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func()
#11 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#12 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func()
#13 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(114): Illuminate\Pipeline\Pipeline->then()
#14 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(68): Illuminate\Routing\ControllerDispatcher->callWithinStack()
#15 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch()
#16 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Route.php(134): Illuminate\Routing\Route->runWithCustomDispatcher()
#17 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(708): Illuminate\Routing\Route->run()
#18 [internal function]: Illuminate\Routing\Router->Illuminate\Routing\{closure}()
#19 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func()
#20 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#21 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func()
#22 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(710): Illuminate\Pipeline\Pipeline->then()
#23 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(674): Illuminate\Routing\Router->runRouteWithinStack()
#24 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->dispatchToRoute()
#25 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(236): Illuminate\Routing\Router->dispatch()
#26 [internal function]: Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}()
#27 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(139): call_user_func()
#28 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(50): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#29 /var/www/clients/client0/web3/web/app/Http/Middleware/VerifyCsrfToken.php(31): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle()
#30 [internal function]: App\Http\Middleware\VerifyCsrfToken->handle()
#31 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#32 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#33 [internal function]: Illuminate\View\Middleware\ShareErrorsFromSession->handle()
#34 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#35 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(62): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#36 [internal function]: Illuminate\Session\Middleware\StartSession->handle()
#37 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#38 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#39 [internal function]: Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle()
#40 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#41 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#42 [internal function]: Illuminate\Cookie\Middleware\EncryptCookies->handle()
#43 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#44 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(44): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#45 [internal function]: Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle()
#46 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(124): call_user_func_array()
#47 [internal function]: Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}()
#48 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): call_user_func()
#49 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(122): Illuminate\Pipeline\Pipeline->then()
#50 /var/www/clients/client0/web3/web/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(87): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter()
#51 /var/www/clients/client0/web3/web/index.php(96): Illuminate\Foundation\Http\Kernel->handle()
#52 {main}
The error is specifically calling out line 35 of helpers.php:
$minutes = intval(($sec / 60) % 60);
What stands out to me here is the value is being converted to an integer however that's after the calculation has taken place. If you're trying to perform the calculation on a different value type, you're going to run into a problem.
Notice for instance that it didn't flag line 31:
$hours = intval(intval($sec) / 3600);
The difference is that $sec
is converted to an integer first.
I would move that conversion to the top of the function so that any calculation relying on the variable can be sure that it's an integer.
$sec = intval($sec);
...
$hours = intval($sec / 3600);
...
$minutes = intval(($sec / 60) % 60);
There are a number of other ways you may want to clean the function up but this should address the most immediate problem.