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.
     
    chrisj, Feb 19, 2019 IP
  2. Anveto

    Anveto Well-Known Member

    Messages:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #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:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #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:
    606
    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:
    697
    Likes Received:
    40
    Best Answers:
    19
    Trophy Points:
    195
    #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
  6. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    One of your 2 number_format uses is returning a "string" instead of a "number".
    String could be empty.

    Either
    'VIEWS_NUM' => number_format($video->views),
    or
    'PRICE' => number_format($video->video_play_price<$config['video_play_price'] ? $config['video_play_price'] : $video->video_play_price)

    Your $video is a class variable, probably a result of a query sent before to the database...
    You should read this, getting float and int values from string variables:
    https://www.php.net/manual/en/function.floatval.php


    You can ignore the error by placing the whole code inside a try/catch block, that will catch the error, and you can choose to do nothing about it.
    However, not good practice by ignoring error if you catch it.


    By the way, you are in for a lot of work with this strict mode.
    Good thing is that script will be much stronger, like a hardcore JAVA/c++ script.
    Also, I don't think your coding skill is enough to do that kind of programming... Sorry to say...
     
    JEET, Mar 29, 2019 IP