{"id":16633,"date":"2020-07-25T12:38:30","date_gmt":"2020-07-25T12:38:30","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=16633"},"modified":"2022-04-28T09:15:21","modified_gmt":"2022-04-28T09:15:21","slug":"php-7-fundamental-tutorial-for-beginners-php-math-operations","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/php-7-fundamental-tutorial-for-beginners-php-math-operations\/","title":{"rendered":"PHP 7 Fundamental Tutorial for Beginners \u2013 PHP Math Operations"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Performing Math Operations<\/h2>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/www.devopsschool.com\/blog\/wp-content\/uploads\/2020\/07\/php-operators-1024x576.png\" alt=\"\" class=\"wp-image-16748\"\/><\/figure>\n\n\n\n<p>PHP has many built-in functions that help you do anything from simple addition or subtraction to advanced computation.<\/p>\n\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/1e465ae45275e044032d6e358220d825.js\"><\/script>\n\n\n\n<p>Each math operation has a certain precedence level; Multiplication and division are usually done before addition and subtraction. However, parentheses can change this precedence; Sentences enclosed within parentheses are always evaluated, regardless of prior level of operation, as in the following example.<\/p>\n\n\n\n<p>Example: <\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/0ec9decb02ce2683a15f59bf8e859ecb.js\"><\/script>\n\n\n\n<p>In the following section we&#8217;re going to look at some built-in PHP functions that are most frequently used in performing mathematical operations.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find the Absolute Value of a Number<\/h3>\n\n\n\n<p>The absolute value of an integer or float can be found with the abs () function, as shown in the following <strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/86f9f9fc3842a32f65d926d015db8e24.js\"><\/script>\n\n\n\n<p>As you can see if the given number is negative or not, the valued return is positive. But, if the number is positive, this function only returns the number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Round a Fractional Value Up or Down<\/h3>\n\n\n\n<p>The ceiling () function can be used to round a fractional value to the next highest integer value, while the floor () function can be used to round a fractional value to the next lowest integer value, As shown in the following <strong>Example:<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/dbaea4670e1156e0d4b691821f6fe2af.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Find the Square Root of a Number<\/h3>\n\n\n\n<p>You can use the sqrt () function to find the square root of a positive number. This function returns a special value NAN for negative numbers.<\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/79562cbca2322f3a64be0baa2e5cf4d2.js\"><\/script>\n\n\n\n<h3 class=\"wp-block-heading\">Generate a Random Number<\/h3>\n\n\n\n<p>The rand () function can be used to generate a random number. You can optionally specify a range by passing minimum, maximum arguments,<\/p>\n\n\n\n<p><strong>Example<\/strong><\/p>\n\n\n\n<script src=\"https:\/\/gist.github.com\/dharmu9898\/c211c0debdc7e28685e4875f0bac9e65.js\"><\/script>\n\n\n\n<p>If the rand () function is called without optional minimum, maximum arguments, it returns a pseudo dimensional number between 0 and getrandmax (). The getrandmax () function shows the largest possible random value, which is only 32767 on the Windows platform. Therefore, if you need a range larger than 32767, you can specify only minimum and maximum arguments.<\/p>\n\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_30973\"><iframe loading=\"lazy\"  id=\"_ytid_55139\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_30973\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>Performing Math Operations PHP has many built-in functions that help you do anything from simple addition or subtraction to advanced computation. Example: Each math operation has a certain precedence level;&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[5150],"tags":[480,6300,6297,6298,6299],"class_list":["post-16633","post","type-post","status-publish","format-standard","hentry","category-php","tag-absolute","tag-generate-a-random","tag-performing","tag-round-a-fractional-value-up-or-down","tag-square-root"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16633","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=16633"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16633\/revisions"}],"predecessor-version":[{"id":24645,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/16633\/revisions\/24645"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=16633"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=16633"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=16633"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}