Open In App

PHP Infix to Postfix Converter

Last Updated : 27 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Converting infix expressions to postfix notation (also known as Reverse Polish Notation) is a common task in computer science, especially in the context of parsing and evaluating expressions. In infix notation, the operator is placed between the operands, whereas in postfix notation, the operator follows its operands. In this article, we will explore how to convert an infix expression to a postfix expression in PHP.

Infix to Postfix Converter using Stack

A common approach to convert an infix expression to a postfix expression is to use a stack data structure. The algorithm works as follows:

  • Initialize an empty stack for operators and an empty string for the postfix expression.
  • Iterate through each element of the infix expression.
  • If the element is an operand, append it to the postfix expression.
  • If the element is an operator, pop operators from the stack and append them to the postfix expression while the operators have higher or equal precedence than the current operator. Then push the current operator onto the stack.
  • If the element is an opening parenthesis ‘(‘, push it onto the stack.
  • If the element is a closing parenthesis ‘)’, pop operators from the stack and append them to the postfix expression until an opening parenthesis ‘(‘ is encountered.
  • After processing all elements, pop any remaining operators from the stack and append them to the postfix expression.

PHP




<?php
 
function getPrecedence($operator) {
    switch ($operator) {
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        case '^':
            return 3;
        default:
            return 0;
    }
}
 
function infixToPostfix($infix) {
    $stack = [];
    $postfix = '';
 
    for ($i = 0; $i < strlen($infix); $i++) {
        $char = $infix[$i];
 
        if (ctype_alnum($char)) {
            $postfix .= $char;
        } elseif ($char == '(') {
            array_push($stack, $char);
        } elseif ($char == ')') {
            while (end($stack) != '(') {
                $postfix .= array_pop($stack);
            }
            array_pop($stack);
        } else {
            while (!empty($stack)
                && getPrecedence(end($stack))
                >= getPrecedence($char)) {
                $postfix .= array_pop($stack);
            }
            array_push($stack, $char);
        }
    }
 
    while (!empty($stack)) {
        $postfix .= array_pop($stack);
    }
 
    return $postfix;
}
 
// Driver code
$infix = "a+b*(c^d-e)^(f+g*h)-i";
$postfix = infixToPostfix($infix);
 
echo "Postfix expression: $postfix";
 
?>


Output

Postfix expression: abcd^e-fgh*+^*+i-

Explanation:

  • The getPrecedence() function returns the precedence level of an operator, which is used to determine the order of operations.
  • The infixToPostfix() function iterates through each character of the infix expression, handling operands, operators, and parentheses accordingly.
  • Operands are directly appended to the postfix expression.
  • Operators are pushed onto the stack after popping higher or equal precedence operators.
  • Parentheses are used to manage the order of operations, with opening parentheses being pushed onto the stack and closing parentheses causing operators to be popped until the corresponding opening parenthesis is encountered.
  • After processing all characters, any remaining operators on the stack are popped and appended to the postfix expression.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads