Php 如果0,如何使if语句忽略var?

Php 如果0,如何使if语句忽略var?,php,if-statement,ignore,zero,Php,If Statement,Ignore,Zero,我想检查一个变量是否大于x,除非它是0 比如说 <?php $max_n=10;//user setting maximum number of loops, infinite? choose 0. //the problem is that 0 is the smallest number, so the loop stops immediately for ($x = 0; $x <= $max_n; $x++) { $total_n=$x; } //if tot

我想检查一个变量是否大于x,除非它是0

比如说

<?php
$max_n=10;//user setting maximum number of loops, infinite? choose 0.


//the problem is that 0 is the smallest number, so the loop stops immediately

for ($x = 0; $x <= $max_n; $x++) {
    $total_n=$x;
}

//if total number exceeds max amount of numbers, do something
if(1==1 || $total_n > $max_n )
{
    die('Total number is greater than max numbers!');
}

显然,无限循环是个坏主意,但这不是重点

如何使if语句忽略max_n if max_n=0

您可以使用该语句根据某些条件跳到下一条记录

for ($x = 0; $x <= $max_n; $x++) {
  if($max_n===0){
    continue;
  }
    $total_n=$x;
}

对于($x=0;$x这对我来说很有帮助:

<?php
$max_n=10;//user setting maximum number of loops, infinite? choose 0.


//the problem is that 0 is the smallest number, so the loop stops immediately

for ($x = 0; $x <= $max_n; $x++) {
    $total_n=$x;
}

//if total number exceeds max amount of numbers, do something
if(1==1 || ( $total_n > 0 && $total_n > $max_n ) )
{
    die('Total number is greater than max numbers!');
}
?>

删除
1==1 | |
(这会使if语句始终为true),它将按预期工作。
<?php
$max_n=10;//user setting maximum number of loops, infinite? choose 0.


//the problem is that 0 is the smallest number, so the loop stops immediately

for ($x = 0; $x <= $max_n; $x++) {
    $total_n=$x;
}

//if total number exceeds max amount of numbers, do something
if(1==1 || ( $total_n > 0 && $total_n > $max_n ) )
{
    die('Total number is greater than max numbers!');
}
?>