


PHP Algorithm Exercise 12: Get the absolute value between n and 51 and return the value as required
In the previous article "PHP Algorithm Exercise 11: Check whether two given integers are within the specified range", I introduced how to check whether two given integers are within the specified range. Friends in need can learn about it~
This article will continue to bring you a series of PHP algorithm exercises. [Recommended: PHP algorithm exercise series summary (continuously updated~) ]
First of all, let me describe the problem to be solved in this article: "How to write a PHP program to obtain n and 51 The absolute value between. If n is greater than 51, three times the absolute value is returned."
Related introduction: The absolute value refers to the distance from the corresponding point of a number on the number axis to the origin, represented by "| |". |b-a| or |a-b| represents the distance between the point representing a and the point representing b on the number axis.
Let’s go directly to the code:
<?php function test($n) { $x = 51; if ($n > $x) { return ($n - $x)*3; } return $x - $n; } echo test(53)."<br>"; echo test(30)."<br>"; echo test(51)."<br>";
The execution results are as follows:
6 21 0
It’s very simple to implement!
Here we mainly need to master the use of if conditional statements, and then implement the execution process according to the requirements. For example, in this example, if n is greater than 51, it is required to return three times the absolute value, that is, the "($n - $x)*3;" formula must be executed; otherwise, the absolute value of the two numbers is directly returned.
Related introduction:
In PHP, we can use the following conditional statements:
if statement: If the specified condition is true, Then execute the code;
if...else statement: If the condition is true, the code is executed; if the condition is false, the other end of the code is executed;
if...elseif....else statement: execute different code blocks based on two or more conditions;
switch statement: select among multiple code blocks One to execute.
Finally, I would like to recommend the latest and most comprehensive "PHP Video Tutorial"~ Come and learn!
The above is the detailed content of PHP Algorithm Exercise 12: Get the absolute value between n and 51 and return the value as required. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.
