Numbers in PHP

Содержание
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

Автор статьи: Arthur Isaev

Похожие статьи
Программирование на 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

Поиск по сайту

Канал @aofeed Чат @aofeedchat

Контакты и сотрудничество:
Рекомендую наш хостинг beget.com
Пишите на info@aredel.com если Вы:
1. Хотите написать статью для нашего сайта или перевести статью на свой родной язык.
2. Хотите разместить на сайте рекламу, подходящую по тематике.
3. Реклама на моём сайте имеет максимальный уровень цензуры. Если Вы увидели рекламный блок недопустимый для просмотра детьми школьного возраста, вызывающий шок или вводящий в заблуждение - пожалуйста свяжитесь с нами по электронной почте
4. Нашли на сайте ошибку, неточности, баг и т.д. ... .......
5. Статьи можно расшарить в соцсетях, нажав на иконку сети: