Numbers in PHP

Contents
Integers
Floats
Common math functions
Random numbers

Integers

Integers are whole numbers within the platform range, usually 64 bit today. Underscores may split long literals for reading:

$views = 1_000_000; var_dump($views); // int(1000000) var_dump(PHP_INT_MAX); // int(9223372036854775807) on 64 bit

Floats

Floats carry a decimal point, but binary fractions cannot store every decimal exactly. Never compare money with double equals: compare the gap instead:

$sum = 0.1 + 0.2; var_dump($sum); // float(0.30000000000000004) echo round(3.14159, 2); // 3.14

Common math functions

Core helpers cover daily needs without any setup:

echo abs(-7); // 7 echo min(3, 1, 2); // 1 echo max(3, 1, 2); // 3 echo (int) 9.99; // 9

Random numbers

random_int deals cryptographically strong integers, which fits dice, shuffles, and tokens. A deeper recipe lives in Generate unique random numbers with PHP.

echo random_int(1, 6); // one of 1, 2, 3, 4, 5, 6 $token = bin2hex(random_bytes(8)); echo strlen($token); // int(16)

Next: Operators

Article author: Arthur Isaev

Related articles
Development with PHP
PHP course: zero to hero in 50 lessons
What is PHP and where it runs
Install PHP 8.5 and run your first command
Your first PHP script
Comments in PHP
Variables and constants
Types in PHP
Strings in PHP
Numbers in PHP
Operators
if and else
switch and match
Ternary, null coalescing and nullsafe
Loops
Arrays
Associative arrays
Functions
Variable scope
include and require
Namespaces
Composer and autoloading
Everyday string functions
Dates and time
JSON in PHP
Reading and writing files
Errors: read, reproduce, fix
Classes and objects
Constructors and promotion
Inheritance
Interfaces and traits
Enums
Magic methods
Attributes
Exceptions
Strict types
Closures and arrow functions
Generators with yield
Forms: GET and POST
HTTP: headers and status codes
Cookies
Sessions
Login with sessions
JSON REST endpoint
Database with PDO
Uploads and .htaccess
Lint like a pro
Clean functions
Debugging
Modern PHP tour: 5 to 8.4
PHP 8.5 for busy developers
Hero roadmap: the whole course on one page
Install PHP 8.5 on Windows 11
Install PHP 8.5 on Ubuntu
Install PHP 8.5 on Rocky Linux
Install PHP 8.5 on macOS
Install PHP 8.5 on FreeBSD
PHP 8.5 in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: