Formatting number with php

I have been using the HeadCount stack, which picks up the page hits from a text file stored in the main site folder. Using a Header stack, the following information is inserted:

<?php echo (file_get_contents("page-hits.txt")); ?>

Does anyone know how to format the number so that a comma is placed when the page hits exceed 1000, so that the number actually shows at “1,000” or “29,342”. etc?

I looked on w3schools for suggestions, but can’t figure out where to put the suggestions they have, such as $formattedNum = number_format($num, 0).

Thanks

Try:

<?php
    $hits = (int) file_get_contents("page-hits.txt"); // read and cast to number
    echo number_format($hits, 0, '.', ',');          // 0 decimals, . as decimal point, , as thousands separator
?>

Can’t take credit for this - ChatGPT is great for this stuff! :wink:

Hi, Stephen,

I don’t have an answer for you. Instead I got a question: who makes the HeadCount stock? I may need it…

https://stacks4stacks.com
I think you can still email him. The page still lists an email link.

1 Like

Thanks so much. One day I will have to spend some time learning more about ChatGPT. The code worked perfectly.

1 Like

Surprise! I’ve had that stack downloaded since 2017! Just never used… :stuck_out_tongue: