1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help with Fatal error: Uncaught TypeError: number_format() expects parameter 1

Discussion in 'PHP' started by chrisj, Feb 19, 2019.

  1. #1
    I am using a php web script that all works successfully. But when I added this:

    declare( strict_types=1);
    error_reporting(-1);
    ini_set('display_errors' , 'true');
    PHP:
    I now see this:

    Fatal error: Uncaught TypeError: number_format() expects parameter 1 to be float, string given in /home/public_html/sources/videos/content.php:136 Stack trace: #0 /home/public_html/sources/videos/content.php(136): number_format('2.00') #1 /home/public_html/index.php(57): include('/home/.....') #2 {main} thrown in /home/public_html/sources/videos/content.php on line 136
    PHP:
    regarding the last code line here:

    $html_videos = '';
    if (!empty($videos)) {
        foreach ($videos as $key => $video) {
        $video = PT_GetVideoByID($video, 0, 0, 0);
            $html_videos .= PT_LoadPage('videos/list', array(
                'ID' => $video->id,
                'VID_ID' => $video->id,
                'TITLE' => $video->title,
                'VIEWS' => $video->views,
                'VIEWS_NUM' => number_format($video->views),
                'USER_DATA' => $video->owner,
                'THUMBNAIL' => $video->thumbnail,
                'URL' => $video->url,
                'TIME' => $video->time_ago,
                'DURATION' => $video->duration,
                'PRICE' => number_format($video->video_play_price<$config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price)
            ));
        }
    }
    Code (markup):
    Any help with resolving this error is appreciated.
    SEMrush
     
    chrisj, Feb 19, 2019 IP
    SEMrush
  2. Anveto

    Anveto Well-Known Member

    Messages:
    681
    Likes Received:
    36
    Best Answers:
    17
    Trophy Points:
    145
    #2
    You enabled strict types which affects type coercion (https://stackoverflow.com/questions/48723637/what-does-strict-types-do-in-php/53786924#53786924)

    number_format() takes an int but you seem to be passing a bool

    
    number_format($video->video_play_price < $config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price)
    
    Code (markup):
    It checks if '$video->video_play_price' is less than the result of the if statement '$config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price' and returns true or false which worked before because it would be converted to 0 or 1 and then parsed by number_format().
     
    Anveto, Feb 19, 2019 IP
  3. Anveto

    Anveto Well-Known Member

    Messages:
    681
    Likes Received:
    36
    Best Answers:
    17
    Trophy Points:
    145
    #3
    Try changing it to
    
    'PRICE' => number_format($config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price)
    
    Code (markup):
    It might work, hard to know without seeing the rest of the code.
     
    Anveto, Feb 19, 2019 IP
  4. chrisj

    chrisj Well-Known Member

    Messages:
    592
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #4
    Thanks for your messages. Greatly appreciated.
    However, after replacing with you suggested line of code, I see the exact same error.
    Any additional guidance will be welcomed
     
    chrisj, Feb 19, 2019 IP
  5. Anveto

    Anveto Well-Known Member

    Messages:
    681
    Likes Received:
    36
    Best Answers:
    17
    Trophy Points:
    145
    #5
    It would be hard to know, to see what the values are you can dump them
    
    var_dump($config['video_play_price']);
    var_dump($video->video_play_price);
    // And maybe die?
    die();
    
    Code (markup):
    This will give you an idea of what the values are and if they are integers or not.
     
    Anveto, Feb 20, 2019 IP