diff options
author | Fatih Kızmaz <barka_21@hotmail.com> | 2022-05-19 05:03:44 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-05-19 05:03:44 +0300 |
commit | 3e6700d1576c6582169a5d50c95da106ee3017cf (patch) | |
tree | 66acb56fd05278a6f7ef328a1cc85c2ecfa9a8d2 /README.md | |
parent | f71b77a62eb27184b5653d6293250e7fda2fdfef (diff) |
Added ability to escape quotes in strings. (#110)
* Added ability to escape quotes in strings.
* Removed type checking for customfunc arguments. It was a bad idea to check types, because php automatically tries to convert a parameter to required type and throws if it failures. On the other hand, we can check types also in callables if required.
* Update phpdoc
* Fix some typos + improve min, max, avg funcs.
* Update readme + improvements.
* Fix a typo in sample.
* Fix unshown backslash in readme.
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 17 |
1 files changed, 15 insertions, 2 deletions
@@ -7,7 +7,7 @@ * Paratheses () and arrays [] are fully supported * Logical operators (==, !=, <, <, >=, <=, &&, ||) * Built in support for most PHP math functions -* Support for variable number of function parameters +* Support for variable number of function parameters and optional function parameters * Conditional If logic * Support for user defined operators * Support for user defined functions @@ -87,9 +87,17 @@ Add custom function to executor: ```php $executor->addFunction('abs', function($arg) {return abs($arg);}); ``` -Function default parameters (optional parameters) are also supported. +Optional parameters: ```php $executor->addFunction('round', function($num, int $precision = 0) {return round($num, $precision);}); +$executor->calculate('round(17.119)'); // 17 +$executor->calculate('round(17.119, 2)'); // 17.12 +``` +Variable number of parameters: +```php +$executor->addFunction('avarage', function(...$args) {return array_sum($args) / count($args);}); +$executor->calculate('avarage(1,3)'); // 2 +$executor->calculate('avarage(1, 3, 4, 8)'); // 4 ``` ## Operators: @@ -211,6 +219,11 @@ Expressions can contain double or single quoted strings that are evaluated the s ```php echo $executor->execute("1 + '2.5' * '.5' + myFunction('category')"); ``` +To use reverse solidus character (\) in strings, or to use single quote character (') in a single quoted string, or to use double quote character (") in a double quoted string, you must prepend reverse solidus character (\). + +```php +echo $executor->execute("countArticleSentences('My Best Article\'s Title')"); +``` ## Extending MathExecutor You can add operators, functions and variables with the public methods in MathExecutor, but if you need to do more serious modifications to base behaviors, the easiest way to extend MathExecutor is to redefine the following methods in your derived class: |