PHP Math Functions
In this page:
round($number, decimals);
ceil($number);
floor($number);
min($a, $b);
max($array);
random_int(min, max);
Basic Rounding Functions
round(), ceil(), और floor() सभी एक float को एक whole number में बदलते हैं लेकिन direction पर disagree करते हैं: round() सबसे नज़दीकी integer चुनता है, ceil() हमेशा ऊपर round करता है, और floor() हमेशा नीचे round करता है, जो billing calculations जैसी चीज़ों के लिए मायने रखता है।
उदाहरण: Basic Rounding Functions
<?php
// Print `round(4.5) . "\n"` to the output
echo round(4.5) . "\n";
// Print `ceil(4.1) . "\n"` to the output
echo ceil(4.1) . "\n";
// Print `floor(4.9)` to the output
echo floor(4.9);
?>
Login to try C/C++/Java/PHP code in the editor
Minimum और Maximum Values
min() और max() या तो अलग-अलग arguments की एक list accept करते हैं या एक single array, मिलने वाली सबसे छोटी या सबसे बड़ी value return करते हुए -- किसी value को एक allowed range में clamp करने के लिए handy।
उदाहरण: Minimum and Maximum Values
<?php
// Print `min(3, 1, 4) . "\n"` to the output
echo min(3, 1, 4) . "\n";
// Print `max([3, 1, 4])` to the output
echo max([3, 1, 4]);
?>
Login to try C/C++/Java/PHP code in the editor
Random Number Generation
rand() एक quiz को shuffle करने जैसी non-critical randomness के लिए ठीक है, लेकिन random_int() एक cryptographically secure source इस्तेमाल करता है और वहाँ prefer किया जाना चाहिए जहाँ randomness security को affect करे, जैसे एक token generate करना।
उदाहरण: Random Number Generation
<?php
// Print `rand(1, 10) . "\n"` to the output
echo rand(1, 10) . "\n";
// Print `random_int(1, 10)` to the output
echo random_int(1, 10);
?>
Login to try C/C++/Java/PHP code in the editor
Absolute और Power Calculations
abs() किसी number का sign हटाता है, sqrt() एक square root compute करता है, और pow() एक base को दिए गए exponent तक raise करता है -- साथ में उन calculations को cover करते हुए जो geometry और finance code में लगातार दिखते हैं।
उदाहरण: Absolute and Power Calculations
<?php
// Print `abs(-5) . "\n"` to the output
echo abs(-5) . "\n";
// Print `sqrt(16) . "\n"` to the output
echo sqrt(16) . "\n";
// Print `pow(2, 3)` to the output
echo pow(2, 3);
?>
Login to try C/C++/Java/PHP code in the editor
Mathematical Conversions
base_convert() किसी number को arbitrary numeric bases (जैसे hex से binary) के बीच convert करता है, जो color codes, bitmasks, या दूसरे data के साथ काम करते समय उपयोगी है जो naturally एक non-decimal base में express होता है।
उदाहरण: Mathematical Conversions
<?php
// Print `base_convert("FF", 16, 2)` to the output
echo base_convert("FF", 16, 2);
?>
Login to try C/C++/Java/PHP code in the editor
- यह उम्मीद करना कि
round()एक int return करेगा, जबकि यह एक float return करता है। - security tokens के लिए
random_int()के बजायrand()याmt_rand()इस्तेमाल करना। - floats के साथ
%इस्तेमाल करना, जो उन्हें पहले integers में convert कर देता है; floats के लिएfmod()इस्तेमाल करें।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser