← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 2 of 24

PHP Math Functions

Math functions एक scientific calculator के extra buttons जैसे हैं: rounding, square roots, biggest, smallest, और random numbers। PHP के पास पहले से हैं ताकि आपको खुद hard sums न करने पड़ें।
Syntax
php
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
<?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);
?>

Minimum और Maximum Values

min() और max() या तो अलग-अलग arguments की एक list accept करते हैं या एक single array, मिलने वाली सबसे छोटी या सबसे बड़ी value return करते हुए -- किसी value को एक allowed range में clamp करने के लिए handy।

उदाहरण: Minimum and Maximum Values

php
<?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]);
?>

Random Number Generation

rand() एक quiz को shuffle करने जैसी non-critical randomness के लिए ठीक है, लेकिन random_int() एक cryptographically secure source इस्तेमाल करता है और वहाँ prefer किया जाना चाहिए जहाँ randomness security को affect करे, जैसे एक token generate करना।

उदाहरण: Random Number Generation

php
<?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);
?>

Absolute और Power Calculations

abs() किसी number का sign हटाता है, sqrt() एक square root compute करता है, और pow() एक base को दिए गए exponent तक raise करता है -- साथ में उन calculations को cover करते हुए जो geometry और finance code में लगातार दिखते हैं।

उदाहरण: Absolute and Power Calculations

php
<?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);
?>

Mathematical Conversions

base_convert() किसी number को arbitrary numeric bases (जैसे hex से binary) के बीच convert करता है, जो color codes, bitmasks, या दूसरे data के साथ काम करते समय उपयोगी है जो naturally एक non-decimal base में express होता है।

उदाहरण: Mathematical Conversions

php
<?php
// Print `base_convert("FF", 16, 2)` to the output
echo base_convert("FF", 16, 2);
?>
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. यह उम्मीद करना कि round() एक int return करेगा, जबकि यह एक float return करता है।
  2. security tokens के लिए random_int() के बजाय rand() या mt_rand() इस्तेमाल करना।
  3. floats के साथ % इस्तेमाल करना, जो उन्हें पहले integers में convert कर देता है; floats के लिए fmod() इस्तेमाल करें।

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.