PHP code example of ralphschindler / calcdown

1. Go to this page and download the library: Download ralphschindler/calcdown 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/ */

    

ralphschindler / calcdown example snippets


use Calcdown\CalcdownParser;

$parser = new CalcdownParser();

// Simple calculation
$result = $parser->parseLine('2 + 2');
echo $result->result; // 4

// With units and conversions
$result = $parser->parseLine('$50 + 20%');
echo $result->result; // "60"
echo $result->resultUnits; // "USD"

// Multi-line calculations with variables
$block = <<<CALC
rate = $45 times 1
hours = 8
daily = rate * hours
weekly = daily * 5
CALC;

$results = $parser->parseBlock($block);
$weeklyRate = $results->finalLine();
echo $weeklyRate->result; // "1800"

2 + 2                    // 4
5 * (3 + 1)             // 20
10 / 2                  // 5
2 ^ 3                   // 8 (exponentiation)
10 % 3                  // 1 (modulo)

$100 + $50              // "150" (USD)
€200 - 10%              // 180 (EUR)
£50 in Euros            // 57.5 (EUR)
$7 * 4                  // "28" (USD)

100 cm in m             // 1 (m)
20 ml in teaspoons      // "4.05" (tsp)
50 cm in inches         // 50 (inches)

today + 7 days          // "2025-11-09" (date)
today + 30 days         // "2025-12-02" (date)

price = $8 times 3      // Assign $24 to price
tax = price * 8.5%      // Use price variable
total = price + tax     // "25.02" (USD approx)

100 + 20%               // 120
200 - 15%               // 170
20% of what is 50       // 250 (reverse percentage)

// Combining multiple features
base = $299 times 1
discount = base - 25%
tax = discount + 8.5%
final = tax in EUR      // Approx "207.06" (EUR)

// Construction calculations
length = 25 cm * 6
buffered = length + 5%
meters = buffered in m    // 1.58 (m)

2 + 2 # This is a comment
# Lines starting with # are ignored
price = $100 # inline comments work too

$result = $parser->parseLine('price = $50 + 20%');
$result->result;              // "60"
$result->resultUnits;         // "USD"
$result->assignedVariables;   // ['price' => "60"]

$block = "a = 10\nb = 5\nresult = a + b";
$results = $parser->parseBlock($block);
$final = $results->finalLine();
$final->result; // 15

$line->toArray(); // Returns associative array:
// [
//     'expression' => '2 + 2',
//     'result' => 4,
//     'units' => null,
//     'assigned_variables' => []  // Only if variables were assigned
// ]

$block->toArray(); // Returns array of line results
$block->finalLine(); // Returns last LineEvaluation or null
$block->lines; // Array of all LineEvaluation objects

$invoice = <<<CALC
subtotal = $1250 times 1
discount = subtotal - 10%
tax = discount + 8.5%
total = tax
CALC;

$result = $parser->parseBlock($invoice);

$timeline = <<<CALC
project_start = today
design_phase = project_start + 14 days
development_phase = design_phase + 30 days
testing_phase = development_phase + 10 days
launch_date = testing_phase + 7 days
CALC;

$result = $parser->parseBlock($timeline);
$launch = $result->finalLine();

$conversions = <<<CALC
recipe_ml = 250 ml
in_teaspoons = recipe_ml in teaspoons

width_cm = 150 cm  
width_m = width_cm in m
CALC;

$result = $parser->parseBlock($conversions);

$finance = <<<CALC
income = $5000 times 1
expenses = income - 30%
savings = income * 20%
remaining = expenses - savings
euros = remaining in EUR
CALC;

$result = $parser->parseBlock($finance);

// Invalid token returns error token
$parser->tokenize('2 & 3');  
// Returns error token with message

// Undefined variables default to 0
$result = $parser->parseLine('undefined_var + 5');
$result->result; // 5