PHP code example of puritandesigns / poindexter

1. Go to this page and download the library: Download puritandesigns/poindexter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

puritandesigns / poindexter example snippets


use Poindexter\Parsing\Parser;

// Simple arithmetic
Parser::calculate('1 + 1')->getValue(); // returns 2
Parser::calculate('2 * 2')->getValue(); // returns 4

// Comparisons
// returns 1 (truthy)
Parser::calculate('1 < 2')->getValue();
Parser::calculate('2 = 2')->getValue();
// returns 0 (falsy)
Parser::calculate('2 <= 1')->getValue();
Parser::calculate('1 = 2')->getValue();

// Logical Operators (returns binary value)
// and
Parser::calculate('1 & 1')->getValue(); // returns 1
Parser::calculate('1 & 0')->getValue(); // returns 0
Parser::calculate('0 & 0')->getValue(); // returns 0
// or
Parser::calculate('1 | 1')->getValue(); // returns 1
Parser::calculate('1 | 0')->getValue(); // returns 1
Parser::calculate('0 | 0')->getValue(); // returns 0

// Variables
Parser::calculate('x + y', ['x' => 1, 'y' => 2])->getValue();
Parser::calculate('long_name + y', ['long_name' => 1, 'y' => 2])->getValue();

// Simple parentheses
Parser::calculate('(1 + x) * (1 + y)', ['x' => 1, 'y' => 2])->getValue();

use Poindexter\Interfaces\ResultInterface;
use Poindexter\Factors;

$factors = [
    new Factors\Number(5),
    new Factors\Add(),
    new Factors\Parenthesis([
        new Factors\Number(5),
        new Factors\Add(),
        new Factors\Parenthesis([
            new Factors\Number(5),
            new Factors\Add(),
            new Factors\Number(5)
        ])
    ]),
    new Factors\Divide(),
    new Factors\Number(5)
];

$calculator = new \Poindexter\Calculator($factors, ResultInterface::INTEGER);

$calculator->calculate()->getValue();