PHP code example of jamesgerat / exprlib
1. Go to this page and download the library: Download jamesgerat/exprlib 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/ */
jamesgerat / exprlib example snippets
// simple math
exprlib\Parser::build('2+1')->evaluate(); // 3
exprlib\Parser::build('2/1')->evaluate(); // 2
exprlib\Parser::build('2/(3.6*8.5)')->evaluate(); // 0.06536
exprlib\Parser::build('2+(6/2)+(8*3)')->evaluate(); // 29
exprlib\Parser::build('2+3+6+6/2+3')->evaluate(); // 17
exprlib\Parser::build('0.001 + 0.02')->evaluate(); // 0.021
exprlib\Parser::build("100 - 80 - 90 + 100")->evaluate();//30
// functions
exprlib\Parser::build('COS(0)')->evaluate(); // 1
exprlib\Parser::build('cos(90)')->evaluate(); // 0
exprlib\Parser::build('cos(180)')->evaluate(); // -1
exprlib\Parser::build('cos(360)')->evaluate(); // 1
exprlib\Parser::build('sin(0)')->evaluate(); // 0
exprlib\Parser::build('sin(90)')->evaluate(); // 1
exprlib\Parser::build('sin(180)')->evaluate(); // 0
exprlib\Parser::build('sqrt(9)')->evaluate(); // 3
exprlib\Parser::build('sqrt(4)')->evaluate(); // 2
exprlib\Parser::build('sqrt(3)')->evaluate(); // 1.73205
exprlib\Parser::build('tan(180)')->evaluate(); // 0
exprlib\Parser::build('log(10)')->evaluate(); // '1'
exprlib\Parser::build('log(10,10)')->evaluate(); // '1'
exprlib\Parser::build('ln(10)')->evaluate(); // '2.30259'
exprlib\Parser::build('log(0.7)')->evaluate(); // '-0.1549'
exprlib\Parser::build('ln(0.7)')->evaluate(); // '-0.35667'
exprlib\Parser::build('pow(10, 2)')->evaluate(); // 100
exprlib\Parser::build('pow(10, 3)')->evaluate(); // 1000
exprlib\Parser::build('pow(10, 0)')->evaluate(); // 1
exprlib\Parser::build('exp(12)')->evaluate(); // 162754.79142
exprlib\Parser::build('exp(5.7)')->evaluate(); // 298.8674
exprlib\Parser::build('sum(10, 20, 30)')->evaluate(); // 60
exprlib\Parser::build('avg(10, 20, 30)')->evaluate(); // 20
exprlib\Parser::build('log(0)')->evaluate(); // -INF
exprlib\Parser::build('log(0)*-1')->evaluate(); // INF
exprlib\Parser::build(sprintf('acos(%s)', rad2deg(8)))->evaluate(); // NAN
exprlib\Parser::build('round(2.555,0)')->evaluate(); // 3
exprlib\Parser::build('round(2.555,1)')->evaluate(); // 2.6
exprlib\Parser::build('round(222.555,-1)')->evaluate(); // 220
exprlib\Parser::build('round(225.555,-1)')->evaluate(); // 230
// min-max
exprlib\Parser::build('max(10,20,30)')->evaluate(); // 30
exprlib\Parser::build('min(10,20,30)')->evaluate(); // 10
// if-elsing
exprlib\Parser::build('if(1=1, 1, 0)')->evaluate(); // 1
exprlib\Parser::build('if(1<2, 1, 0)')->evaluate(); // 0
exprlib\Parser::build('if(1>2, 1, 0)')->evaluate(); // 0
// var placing
exprlib\Parser::build('[a]+1')->setVars(['a' => 3])->evaluate(); // 4
exprlib\Parser::build('[a]-[b]')->setVars(['a' => 2, 'b' => 5])->evaluate(); // -3
// var placing with if-elsing
exprlib\Parser::build('if([a]=5, 1, 0)')->setVars(['a' => 5])->evaluate(); // 1
exprlib\Parser::build('if([a]>[b], 1, 0)')->setVars(['a' => 3, 'b' => 2])->evaluate(); // 1