Get the App
SLTechnology News&Howtos  ›  Development  › 

How to judge whether it is odd or even in php

Shulou Source: shulou.com Published: 2022-05-31 16:31:22 09月30日 Update

Most people do not understand the knowledge points of this article "how to judge odd or even in php", so the editor summarizes the following contents, detailed contents, clear steps, and certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to judge odd or even in php" article.

In php, you can use the remainder operator "%" and the equality operator "= =" to determine whether a number is odd or even, just use the "%" operator to get the remainder after dividing the specified number by 2, and use the "=" operator to determine whether the remainder is 0; the syntax is "$num%2==0", if true is returned, it is even, and vice versa.

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The essence of judging whether a number is odd or even is to judge whether the number can be divisible by 2:

If it is divisible by 2, it is even.

If it is not divisible by 2, it is odd.

So how to implement the divisible function in PHP?

In PHP, the remainder operator "%" can be used to determine whether to divide or not.

If a number is divided by 2 and there is no remainder (0), then it is divisible by 2, which is an even number.

If a number is divided by 2 and there is a remainder (not 0), then it is not divisible by 2, which is odd.

Well, at this point, you should know how to tell whether it is odd or even: using the "%" operator. Syntax:

$num%2==0

Here is an example to learn more about:

The above is about the content of this article on "how to judge whether it is odd or even in php". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more related knowledge, please pay attention to the industry information channel.

Tags: Even odd is or operator operation content number remainder that is articles knowledge articles grammar value functions most tutorials more essence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info NVidia Docker vpn