PHP Numbers
In this page:
$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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
?>
Login to try C/C++/Java/PHP code in the editor
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
// Print "5" + "3" to the output
echo "5" + "3";
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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");
?>
Login to try C/C++/Java/PHP code in the editor
- floats को
==से compare करना, जैसे0.1 + 0.2 == 0.3, जो binary precision की वजह सेfalseहै; इसके बजाय एक छोटी tolerance से compare करें। - integer overflow को ignore करना:
PHP_INT_MAXमें add करना चुपचाप result को एक float बना देता है, ताकि integer-only code अलग तरह से behave करे। - यह मान लेना कि
intdiv()और/same हैं, जबकि10 / 4float2.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 होती हैं।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: