在PHP中找出一个数字/变量是奇数还是偶数的最简单最基本的方法是什么? 这和mod有关吗?

我试过一些剧本,但是…谷歌目前没有发送。


当前回答

检查偶数或奇数不使用条件和循环语句。

这对我很有用!

$(document).ready(function(){ $("#btn_even_odd").click(function(){ var arr = ['Even','Odd']; var num_even_odd = $("#num_even_odd").val(); $("#ans_even_odd").html(arr[num_even_odd % 2]); }); }); <!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <title>Check Even Or Odd Number Without Use Condition And Loop Statement.</title> </head> <body> <h4>Check Even Or Odd Number Without Use Condition And Loop Statement.</h4> <table> <tr> <th>Enter A Number :</th> <td><input type="text" name="num_even_odd" id="num_even_odd" placeholder="Enter Only Number"></td> </tr> <tr> <th>Your Answer Is :</th> <td id="ans_even_odd" style="font-size:15px;color:gray;font-weight:900;"></td> </tr> <tr> <td><input type="button" name="btn_even_odd" id="btn_even_odd" value="submit"></td> </tr> </table> </body> </html>

其他回答

检查偶数或奇数不使用条件和循环语句。

这对我很有用!

$(document).ready(function(){ $("#btn_even_odd").click(function(){ var arr = ['Even','Odd']; var num_even_odd = $("#num_even_odd").val(); $("#ans_even_odd").html(arr[num_even_odd % 2]); }); }); <!DOCTYPE html> <html> <head> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <title>Check Even Or Odd Number Without Use Condition And Loop Statement.</title> </head> <body> <h4>Check Even Or Odd Number Without Use Condition And Loop Statement.</h4> <table> <tr> <th>Enter A Number :</th> <td><input type="text" name="num_even_odd" id="num_even_odd" placeholder="Enter Only Number"></td> </tr> <tr> <th>Your Answer Is :</th> <td id="ans_even_odd" style="font-size:15px;color:gray;font-weight:900;"></td> </tr> <tr> <td><input type="button" name="btn_even_odd" id="btn_even_odd" value="submit"></td> </tr> </table> </body> </html>

你认为mod是个不错的开始,这是对的。下面是一个表达式,如果$number为偶数则返回true,如果为奇数则返回false:

$number % 2 == 0

适用于所有integerPHP值,参见算术操作符sphp。

例子:

$number = 20;
if ($number % 2 == 0) {
  print "It's even";
}

输出:

它甚至

<?php
// Recursive function to check whether
// the number is Even or Odd 
function check($number){
    if($number == 0)
        return 1;
    else if($number == 1)
        return 0;
    else if($number<0)
        return check(-$number);
    else
        return check($number-2);        
}
  
// Check the number odd or even
$number = 35;
if(check($number))
    echo "Even";
else
    echo "Odd";
?>

因此,输出将是Odd

我做了一些测试,发现在mod, is_int和&-操作符之间,mod是最快的,紧随其后的是&-操作符。 Is_int几乎比mod慢4倍。

I used the following code for testing purposes: $number = 13; $before = microtime(true); for ($i=0; $i<100000; $i++) { $test = ($number%2?true:false); } $after = microtime(true); echo $after-$before." seconds mod<br>"; $before = microtime(true); for ($i=0; $i<100000; $i++) { $test = (!is_int($number/2)?true:false); } $after = microtime(true); echo $after-$before." seconds is_int<br>"; $before = microtime(true); for ($i=0; $i<100000; $i++) { $test = ($number&1?true:false); } $after = microtime(true); echo $after-$before." seconds & operator<br>"; The results I got were pretty consistent. Here's a sample: 0.041879177093506 seconds mod 0.15969395637512 seconds is_int 0.044223070144653 seconds & operator

$number %2 = 1如果是奇数…所以不用用not even…

$number = 27;

if ($number % 2 == 1) {
  print "It's odd";
}