How can regular expressions be used to determine the type of operation (addition, subtraction, multiplication, division) in a mathematical expression in PHP?

Regular expressions can be used to determine the type of operation in a mathematical expression by searching for specific mathematical symbols such as + (addition), - (subtraction), * (multiplication), and / (division). By using regular expressions to match these symbols in the input expression, we can determine the type of operation being performed.

$expression = "5 + 3"; // Example mathematical expression

if (preg_match('/\+/', $expression)) {
    echo "Addition operation detected";
} elseif (preg_match('/-/', $expression)) {
    echo "Subtraction operation detected";
} elseif (preg_match('/\*/', $expression)) {
    echo "Multiplication operation detected";
} elseif (preg_match('/\//', $expression)) {
    echo "Division operation detected";
} else {
    echo "Operation not recognized";
}