PHP: Checking if a Number is Positive, Negative or Zero

Updated: January 9, 2024 By: Guest Contributor Post a comment

Overview

Understanding how to check for positive, negative, or zero values in PHP is crucial for data validation and control flow in applications. This tutorial covers multiple methods tailored to PHP beginners and advanced developers alike.

Introduction to Number Checking

In PHP, the sign of a number determines how it interacts with other mathematical functions and comparisons. Positive numbers are greater than zero, negative numbers are less than zero, and zero is neutral. Knowing how to check for each of these cases is valuable when writing algorithms that pivot behavior based on numerical input.

To determine if a number is positive, negative, or zero, we need to compare it against zero. PHP provides several operators for this, the most common being the comparison operators (>, <, and ==).

Basic Example Using If Statements


function checkNumber($num) {
  if ($num > 0) {
    echo "The number ". $num. " is positive.";
  } elseif ($num < 0) {
    echo "The number ". $num. " is negative.";
  } else {
    echo "The number is zero.";
  }
}

checkNumber(15); // Output: The number 15 is positive.
checkNumber(-5); // Output: The number -5 is negative.
checkNumber(0);  // Output: The number is zero.

Using the Ternary Operator

The ternary operator is a condensed form of the if-else statement, which can be useful for brief operations.


function checkNumberTernary($num) {
  $result = $num > 0 ? "positive" : ($num < 0 ? "negative" : "zero");
  echo "The number is ". $result . ".";
}

checkNumberTernary(10); // Output: The number is positive.
checkNumberTernary(-10); // Output: The number is negative.
checkNumberTernary(0);   // Output: The number is zero.

Advanced: Using switch Statements (PHP 8+)

In PHP 8 and later, the match expression offers a clean and readable way to do comparisons. Although match is usually used with exact value matching, creative use of true conditions can simulate the behavior of switch statements for numbers.


function checkNumberMatch($num) {
  $result = match(true) {
    $num > 0 => "positive",
    $num < 0 => "negative",
    default => "zero",
  };
  echo "The number is ". $result . ".";
}

checkNumberMatch(20); // Output: The number is positive.
checkNumberMatch(-20); // Output: The number is negative.
checkNumberMatch(0);   // Output: The number is zero.

Error Handling for Non-Numeric Values

When working with user inputs or dynamic data, you may encounter values that are not numbers. It is important to validate that the values are numeric before performing comparisons.


function checkIfNumericAndDetermine($value) {
  if (!is_numeric($value)) {
    echo "This value is not a number.";
    return;
  }
  checkNumber($value);
}

checkIfNumericAndDetermine("PHP"); // Output: This value is not a number.

Simplifying with the Sign Function

For developers interested in a more mathematical approach to the problem, PHP does not have a built-in sign function, but one can be implemented to simplify the number checking process.


function sign($num) {
  return ($num > 0) - ($num < 0);
}

function checkNumWithSign($num) {
  switch (sign($num)) {
    case 1:
      echo "The number ". $num. " is positive.";
      break;
    case -1:
      echo "The number ". $num. " is negative.";
      break;
    case 0:
      echo "The number is zero.";
      break;
  }
}

checkNumWithSign(25); // Output: The number 25 is positive.
checkNumWithSign(-25); // Output: The number -25 is negative.
checkNumWithSign(0);   // Output: The number is zero.

Conclusion

In PHP, determining whether a number is positive, negative, or zero can be accomplished in many ways, from simple if-else statements to more complex match expressions or custom functions. The key is to choose the right approach that matches the context and complexity of your application, ensuring efficient and readable code.