{"id":37,"date":"2025-07-18T05:00:43","date_gmt":"2025-07-18T05:00:43","guid":{"rendered":"https:\/\/medium.com\/p\/4d3ec46d0753"},"modified":"2025-07-31T08:34:46","modified_gmt":"2025-07-31T08:34:46","slug":"simple-arithmetic-operations-using-php","status":"publish","type":"post","link":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/","title":{"rendered":"Simple Arithmetic Operations using PHP"},"content":{"rendered":"<p>Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division. These code samples are just for practice purpose, it may have issues or bugs.<\/p>\n<pre>&lt;?php\r\n\r\nfunction arithmetic_operations(int $op, float $x, float $y) : string {\r\n\r\n switch($op) {\r\n case 1 :\r\n \/**\r\n * This is addition operation\r\n *\/\r\n $xy = $x + $y;\r\n echo \"Addition of two numbers {$x} + {$y} = {$xy}\" . PHP_EOL;\r\n break;\r\n\r\n case 2 :\r\n \/**\r\n * This is subtraction operation\r\n *\/\r\n $xy = $x - $y;\r\n echo \"Subtraction of two numbers {$x} - {$y} = {$xy}\" . PHP_EOL ;\r\n break;\r\n\r\n case 3 :\r\n \/**\r\n * This is multiplication operation\r\n *\/\r\n $xy = $x * $y;\r\n echo \"Multiplication of two numbers {$x} * {$y} = {$xy}\" . PHP_EOL;\r\n break;\r\n\r\n case 4 :\r\n \/**\r\n * This is division operation\r\n *\/\r\n if ($y == 0) {\r\n echo \"Divisor must not be zero, please re-run the process\" . PHP_EOL;\r\n }\r\n else {\r\n $xy = $x \/ $y;\r\n echo \"Division of two numbers {$x} \/ {$y} = {$xy}\" . PHP_EOL;\r\n }\r\n break;\r\n\r\n default:\r\n echo \"Something went wrong...\" . PHP_EOL;\r\n\r\n }\r\n\r\n return \"\";\r\n}\r\n\r\nfunction select_opertion() {\r\n $op = (int) readline(\"Enter a number for the operation: \");\r\n\r\n if (!in_array($op, [1, 2, 3, 4])) {\r\n echo(\"Re-select a valid operation\"). PHP_EOL;\r\n $op = select_opertion();\r\n }\r\n return $op;\r\n}\r\n\r\n$flow = true;\r\n\r\ndo {\r\n\r\n $x = (float) readline(\"Enter first number: \");\r\n $y = (float) readline(\"Enter second number: \");\r\n echo(\"1. Addition | 2. Subtraction | 3. Multiplication | 4. Division\") . PHP_EOL;\r\n $op = select_opertion();\r\n print($op);\r\n\r\n arithmetic_operations($op, $x, $y);\r\n\r\n $con = readline(\"Do you want to continue: \");\r\n if ($con == \"n\") $flow = false;\r\n\r\n} while ($flow);<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\" alt=\"\" width=\"1\" height=\"1\" \/><\/p>\n<p>Here we achieve these operations using switch case and functions in php. This code is expected to run on command line.<\/p>\n<p>New methods in this article:<\/p>\n<ol>\n<li><a href=\"https:\/\/www.php.net\/manual\/en\/function.readline.php\" target=\"_blank\" rel=\"noopener\">readline<\/a> &#8211; click on this and you will go to the documentation of this method.<\/li>\n<\/ol>\n<hr \/>\n<table>\n<thead>\n<tr>\n<th>Related Posts<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><a href=\"https:\/\/kishor10d.com\/blogs\/simple-arithmeti\u2026ython-match-case\/\" rel=\"noopener\">Simple Arithmetic Operations using PHP<\/a><\/td>\n<\/tr>\n<tr>\n<td><a href=\"https:\/\/kishor10d.com\/blogs\/php-curl-vs-guzzle-which-http-client-should-you-use\/\" rel=\"noopener\">PHP cURL vs Guzzle: Which HTTP Client Should You Use?<\/a><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division. These code samples are just for practice purpose, it&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[5],"tags":[44,45,47,48,46,43],"class_list":["post-37","post","type-post","status-publish","format-standard","hentry","category-php","tag-arithmetic-operations","tag-arithmetic-operations-using-php","tag-math-operations","tag-math-operations-using-php","tag-mathematical-operations","tag-php"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Simple Arithmetic Operations using PHP - Practical Kishor<\/title>\n<meta name=\"description\" content=\"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Simple Arithmetic Operations using PHP - Practical Kishor\" \/>\n<meta property=\"og:description\" content=\"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/\" \/>\n<meta property=\"og:site_name\" content=\"Practical Kishor\" \/>\n<meta property=\"article:published_time\" content=\"2025-07-18T05:00:43+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-07-31T08:34:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/\"},\"author\":{\"name\":\"admin\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#\\\/schema\\\/person\\\/682d05c56912dd235403d4d49862750a\"},\"headline\":\"Simple Arithmetic Operations using PHP\",\"datePublished\":\"2025-07-18T05:00:43+00:00\",\"dateModified\":\"2025-07-31T08:34:46+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/\"},\"wordCount\":93,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#\\\/schema\\\/person\\\/682d05c56912dd235403d4d49862750a\"},\"image\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/medium.com\\\/_\\\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\",\"keywords\":[\"arithmetic operations\",\"arithmetic operations using php\",\"math operations\",\"math operations using php\",\"mathematical operations\",\"php\"],\"articleSection\":[\"PHP\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/\",\"url\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/\",\"name\":\"Simple Arithmetic Operations using PHP - Practical Kishor\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/medium.com\\\/_\\\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\",\"datePublished\":\"2025-07-18T05:00:43+00:00\",\"dateModified\":\"2025-07-31T08:34:46+00:00\",\"description\":\"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#primaryimage\",\"url\":\"https:\\\/\\\/medium.com\\\/_\\\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\",\"contentUrl\":\"https:\\\/\\\/medium.com\\\/_\\\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/simple-arithmetic-operations-using-php\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Simple Arithmetic Operations using PHP\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#website\",\"url\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/\",\"name\":\"Practical Kishor\",\"description\":\"The quick brown fox jumps over the lazy dog\",\"publisher\":{\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#\\\/schema\\\/person\\\/682d05c56912dd235403d4d49862750a\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/#\\\/schema\\\/person\\\/682d05c56912dd235403d4d49862750a\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\",\"caption\":\"admin\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g\"},\"sameAs\":[\"https:\\\/\\\/kishor10d.com\\\/blogs\"],\"url\":\"https:\\\/\\\/kishor10d.com\\\/blogs\\\/author\\\/kishor10d\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Simple Arithmetic Operations using PHP - Practical Kishor","description":"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/","og_locale":"en_US","og_type":"article","og_title":"Simple Arithmetic Operations using PHP - Practical Kishor","og_description":"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.","og_url":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/","og_site_name":"Practical Kishor","article_published_time":"2025-07-18T05:00:43+00:00","article_modified_time":"2025-07-31T08:34:46+00:00","og_image":[{"url":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753","type":"","width":"","height":""}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"Written by":"admin","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#article","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/"},"author":{"name":"admin","@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"headline":"Simple Arithmetic Operations using PHP","datePublished":"2025-07-18T05:00:43+00:00","dateModified":"2025-07-31T08:34:46+00:00","mainEntityOfPage":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/"},"wordCount":93,"commentCount":0,"publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"image":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753","keywords":["arithmetic operations","arithmetic operations using php","math operations","math operations using php","mathematical operations","php"],"articleSection":["PHP"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/","url":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/","name":"Simple Arithmetic Operations using PHP - Practical Kishor","isPartOf":{"@id":"https:\/\/kishor10d.com\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#primaryimage"},"image":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#primaryimage"},"thumbnailUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753","datePublished":"2025-07-18T05:00:43+00:00","dateModified":"2025-07-31T08:34:46+00:00","description":"Here is the code for simple arithmetic operations using PHP like Addition, Subtraction, Multiplication and Division.","breadcrumb":{"@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#primaryimage","url":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753","contentUrl":"https:\/\/medium.com\/_\/stat?event=post.clientViewed&amp;referrerSource=full_rss&amp;postId=4d3ec46d0753"},{"@type":"BreadcrumbList","@id":"https:\/\/kishor10d.com\/blogs\/simple-arithmetic-operations-using-php\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/kishor10d.com\/blogs\/"},{"@type":"ListItem","position":2,"name":"Simple Arithmetic Operations using PHP"}]},{"@type":"WebSite","@id":"https:\/\/kishor10d.com\/blogs\/#website","url":"https:\/\/kishor10d.com\/blogs\/","name":"Practical Kishor","description":"The quick brown fox jumps over the lazy dog","publisher":{"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/kishor10d.com\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/kishor10d.com\/blogs\/#\/schema\/person\/682d05c56912dd235403d4d49862750a","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g","caption":"admin"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/56b0564d13413fec0713653cf76e91ba4d9c5fbbf6dbe0a8adbd3941c88bc1e4?s=96&d=mm&r=g"},"sameAs":["https:\/\/kishor10d.com\/blogs"],"url":"https:\/\/kishor10d.com\/blogs\/author\/kishor10d\/"}]}},"_links":{"self":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/37","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/comments?post=37"}],"version-history":[{"count":8,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":79,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/posts\/37\/revisions\/79"}],"wp:attachment":[{"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kishor10d.com\/blogs\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}