← Back to PHP Course | Chapter 1: Introduction & Basics | Lesson 13 of 13

PHP Numbers

PHP numbers दो flavors में आते हैं: 7 जैसे whole numbers, और 7.5 जैसे decimal point वाले numbers। यह whole apples गिनने बनाम एक cup आटा नापने जैसा है।
Syntax
php
$integer = 10;
$float = 10.5;

var_dump(PHP_INT_MAX);   // largest integer
var_dump(is_int($value));
var_dump(is_float($value));

Integer और Float Types

PHP में दो core numeric types हैं: whole numbers के लिए int और decimal point या exponential notation वाले numbers के लिए float (जिसे double भी कहते हैं)।

PHP dynamically typed है, इसलिए एक variable एक line में int और अगली में float hold कर सकता है -- type आपके assign किए literal से infer होता है, पहले से declare नहीं होता।

उदाहरण: Integer and Float Types

php
<?php
// Declare `$a`, set to `10`
$a = 10;
// Declare `$b`, set to `10.5`
$b = 10.5;
// Print a detailed dump (with types) of `$a`
var_dump($a);
// Print a detailed dump (with types) of `$b`
var_dump($b);
?>

Integer Limits और Overflow

हर platform की एक maximum integer size होती है, जो PHP में PHP_INT_MAX constant की तरह expose होती है (आमतौर पर 64-bit systems पर 9223372036854775807)।

उस limit से आगे 1 add करना wrap around या error नहीं करता -- PHP चुपचाप result को एक float में promote कर देता है, जो बहुत बड़े numbers के लिए precision खो सकता है, इसलिए उस boundary के पास exact integer arithmetic पर भरोसा करना risky है।

उदाहरण: Integer Limits and Overflow

php
<?php
// Print `PHP_INT_MAX . "\n"` to the output
echo PHP_INT_MAX . "\n";
// Print a detailed dump (with types) of `PHP_INT_MAX + 1`
var_dump(PHP_INT_MAX + 1);
?>

Floating-Point Precision

Floats binary में store होते हैं, और कई decimal fractions -- जैसे 0.1 -- binary में exactly represent नहीं हो सकते, वही issue जो IEEE 754 floats पर बनी हर language में है।

यही वजह है कि PHP में 0.1 + 0.2 == 0.3 false evaluate हो सकता है; exact equality के लिए floats compare करना subtle bugs का एक common source है, और compare करने से पहले एक fixed precision तक round करना usual fix है।

उदाहरण: Floating-Point Precision

php
<?php
// Print a detailed dump (with types) of `0.1 + 0.2 == 0.3`
var_dump(0.1 + 0.2 == 0.3);
// Print `round(0.1 + 0.2, 10) == round(0.3, 10) ? "Equal after rounding" : "Still unequal"` to the output
echo round(0.1 + 0.2, 10) == round(0.3, 10) ? "Equal after rounding" : "Still unequal";
?>

Numeric Strings

PHP "42" या "3.14" जैसी strings को numeric strings मानता है और arithmetic के लिए इन्हें automatically convert कर देगा -- "5" + "3" 8 evaluate होता है, कोई string concatenation नहीं।

PHP 8 ने यह rules कड़े कर दिए कि valid numeric string क्या count होती है, और arithmetic में इस्तेमाल की गई non-numeric strings अब पुराने versions की तरह चुपचाप 0 बनने के बजाय एक TypeError throw करती हैं।

उदाहरण: Numeric Strings

php
<?php
// Print "5" + "3" to the output
echo "5" + "3";
?>

Number Formatting और Conversion

number_format(), round(), intval(), और floatval() जैसे functions display या storage के लिए formats और precision levels के बीच convert करते हैं।

number_format() खासतौर पर users को prices या बड़े counts दिखाते समय thousands separators और एक fixed decimal count add करने के लिए उपयोगी है, कुछ ऐसा जो एक float का raw echo सही से नहीं करेगा।

उदाहरण: Number Formatting and Conversion

php
<?php
// Print `number_format(1234567.891, 2) . "\n"` to the output
echo number_format(1234567.891, 2) . "\n";
// Print `round(4.567, 1) . "\n"` to the output
echo round(4.567, 1) . "\n";
// Print `intval("42abc")` to the output
echo intval("42abc");
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. floats को == से compare करना, जैसे 0.1 + 0.2 == 0.3, जो binary precision की वजह से false है; इसके बजाय एक छोटी tolerance से compare करें।
  2. integer overflow को ignore करना: PHP_INT_MAX में add करना चुपचाप result को एक float बना देता है, ताकि integer-only code अलग तरह से behave करे।
  3. यह मान लेना कि intdiv() और / same हैं, जबकि 10 / 4 float 2.5 देता है और सिर्फ intdiv(10, 4) 2 देता है।
चैप्टर सारांश
  • PHP को इसके history, features, setup, और basic syntax व structure वाले पहले program के साथ introduce किया गया है।
  • Comments, keywords, और identifiers PHP code के basic rules हैं।
  • Data types, variables, constants, type casting, और numbers control करते हैं कि values कैसे store होती हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.