How to use the Excel Formula to PHP
- Paste an Excel or Google Sheets formula, or pick one from Examples.
- Choose the target language — the page starts with PHP, and you can switch at any time.
- Rename the detected cell references to meaningful variable names, for example
B2→price. - Choose a full function or the bare expression, then copy or download the code. Helper functions are added automatically when a formula needs them.
Turning spreadsheet formulas into PHP
Quotes, invoices, shipping costs and commission rules are often specified in Excel and then implemented in a PHP application — Laravel, Symfony, WordPress or WooCommerce. This converter reads the formula with Excel's operator precedence and writes PHP 8 code you can drop into a service class or helper file.
=IFERROR(VLOOKUP(A2, Products!A2:C500, 3, FALSE), "Not found")
function calculate($a2, $products_a2_c500)
{
return if_error(fn() => vlookup($a2, $products_a2_c500, 3, false), 'Not found');
}
How Excel functions map to PHP
| Excel | PHP |
|---|---|
IF(c, a, b) | c ? a : b |
IFS / SWITCH | parenthesised ternaries |
AND, OR, NOT | &&, ||, ! |
= and <> | == and != |
a & b | $a . $b |
a ^ b | $a ** $b |
SUM(A2:A10) | array_sum($a2_a10) |
ROUND(x, 2) | round($x, 2) |
MOD(a, b) | excel_mod() helper |
SUMIFS / COUNTIF… | sum_ifs(), count_if() helpers |
VLOOKUP / XLOOKUP | vlookup(), xlookup() helpers |
IFERROR(x, y) | if_error(fn() => x, y) |
LEFT / MID / LEN | mb_substr(), mb_strlen() |
TODAY() / NOW() | new DateTime() |
PHP-specific details
- Nested ternaries. Since PHP 8, unparenthesised nested ternaries are a fatal error. Every nested
IFis wrapped in parentheses automatically. - Concatenation precedence. In PHP 8,
+and-bind tighter than., so"Total: " . $a + $badds first. The converter keeps Excel's meaning either way. - Multibyte text.
LEFT,MID,LEN,UPPERandLOWERuse themb_*functions, so accented characters and emoji count as one character, like in Excel. - FIND and SEARCH.
strpos()returnsfalseor a 0-based offset; thefind_pos()helper returns Excel's 1-based position (0 when not found). - MOD. PHP's
%andfmod()keep the sign of the dividend;excel_mod()matches Excel for negative numbers. - Errors.
IFERRORcatchesDivisionByZeroErrorand lookup failures throughif_error(fn() => …), using arrow functions (PHP 7.4+). - Dates.
TODAY(),DATE()andYEAR()useDateTimeobjects.
Using the output
Helpers are plain functions placed above your function. In a framework, move them into a helper file or make them static methods on a class. Ranges are PHP arrays: a single column is a list, and a multi-column range is a list of rows.
Supported functions: IF, IFS, SWITCH, AND, OR, NOT, XOR, IFERROR, IFNA, ISBLANK, ISNUMBER, ISTEXT, SUM, AVERAGE, MIN, MAX, COUNT, COUNTA, COUNTBLANK, PRODUCT, MEDIAN, ROUND, ROUNDUP, ROUNDDOWN, INT, TRUNC, ABS, SIGN, MOD, POWER, SQRT, EXP, LN, LOG, LOG10, PI, CEILING, FLOOR, RAND, RANDBETWEEN, SUMIF, SUMIFS, COUNTIF, COUNTIFS, AVERAGEIF, AVERAGEIFS, SUMPRODUCT, VLOOKUP, XLOOKUP, MATCH, INDEX, CONCAT, CONCATENATE, TEXTJOIN, LEFT, RIGHT, MID, LEN, UPPER, LOWER, PROPER, TRIM, SUBSTITUTE, REPLACE, FIND, SEARCH, REPT, VALUE, EXACT, TEXT (number formats), TODAY, NOW, DATE, YEAR, MONTH, DAY. To bring the sheet's data along, convert it with Excel to PHP array.
Tools that pair well with the Excel Formula to PHP
- Excel Formula to JavaScript — Paste a spreadsheet formula and get readable JavaScript: a function with named parameters, correct operator precedence and small helper functions for things JavaScript lacks, such as SUMIFS and VLOOKUP.
- Excel Formula to Python — Paste an Excel or Google Sheets formula and get a Python function with clean variable names, the right imports and helpers for SUMIFS, VLOOKUP and Excel-style rounding.
- Excel Formula to SQL — Paste an Excel formula and get the equivalent SQL expression or SELECT query — IF and IFS as CASE WHEN, SUMIFS and COUNTIFS as conditional aggregates, and the right string and date functions for your database.
- CSV & Excel to SQL — Paste CSV or Excel cells, or open an .xlsx file, and get ready-to-run CREATE TABLE and INSERT statements with correctly escaped values for your database.
Frequently asked questions
Which PHP version does the output need?
** operator needs PHP 5.6.Why are the string functions mb_substr and mb_strlen?
How do I pass a range to the function?
A2:A10 becomes a flat array of values; A2:C500 becomes an array of rows, the format vlookup() expects.