# php-parser

> Parse PHP code from JS and returns its AST

Latest version **3.7.0** (published 2026-06-10) · BSD-3-Clause license · 0 weekly downloads

## Install

```sh
npm install php-parser
pnpm add php-parser
yarn add php-parser
bun add php-parser
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

## Facts

| | |
|---|---|
| Version | 3.7.0 |
| Published | 2026-06-10 |
| First published | 2014-12-24 |
| Weekly downloads | 0 |
| License | BSD-3-Clause |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 825.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 565 |
| Author | Ioan CHIRIAC |
| Maintainers | ichiriac, czosel |
| Keywords | php, php5, php7, php8, parser, lexer, tokenizer, ast |

## Links

- npm: https://www.npmjs.com/package/php-parser
- Repository: https://github.com/glayzzle/php-parser
- Homepage: https://glayzzle.com/
- Issues: https://github.com/glayzzle/php-parser/issues
- npm.io page: https://npm.io/package/php-parser

## Alternatives

- [babylon](https://npm.io/package/babylon.md) — 5.1M weekly downloads
- [csscolorparser](https://npm.io/package/csscolorparser.md) — 3.7M weekly downloads
- [expr-eval-fork](https://npm.io/package/expr-eval-fork.md) — 1.5M weekly downloads
- [@leeoniya/ufuzzy](https://npm.io/package/@leeoniya/ufuzzy.md) — 247.7K weekly downloads
- [xml-parser](https://npm.io/package/xml-parser.md) — 78.4K weekly downloads

## Recent versions

- 3.7.0 (latest) — 2026-06-10
- 3.1.0-beta.11 (beta) — 2022-07-02
- 3.6.0 — 2026-05-17
- 3.5.1 — 2026-03-29
- 3.5.0 — 2026-03-22
- 3.4.0 — 2026-02-21
- 3.3.0 — 2026-02-18
- 3.2.5 — 2025-07-10
- 3.2.4 — 2025-06-27
- 3.2.3 — 2025-04-17
- 3.2.2 — 2024-12-31
- 3.2.1 — 2024-11-10
- 3.2.0 — 2024-11-06
- 3.1.5 — 2023-06-07
- 3.1.4 — 2023-03-21
- … 71 more at https://npm.io/package/php-parser/versions

## README

<h1 align="center">php-parser</h1>
<p align="center">
<a title="npm version" href="https://www.npmjs.com/package/php-parser"><img src="https://badge.fury.io/js/php-parser.svg"></a>
<a title="npm downloads" href="https://www.npmjs.com/package/php-parser"><img src="https://img.shields.io/npm/dm/php-parser.svg?style=flat"></a>
<a title="Gitter" href="https://gitter.im/glayzzle/Lobby"><img src="https://img.shields.io/badge/GITTER-join%20chat-green.svg"></a>
</p>
<p align="center">This JavaScript library parses PHP code and converts it to an AST.</p>

## Installation

This library is distributed with [npm](https://www.npmjs.com/package/php-parser) :

```sh
npm install php-parser --save
```

## Usage

```js
// initialize the php parser factory class
const fs = require("fs");
const path = require("path");
const engine = require("php-parser");

// initialize a new parser instance
const parser = new engine({
  // some options :
  parser: {
    extractDoc: true,
    php7: true,
  },
  ast: {
    withPositions: true,
  },
});

// Retrieve the AST from the specified source
const eval = parser.parseEval('echo "Hello World";');

// Retrieve an array of tokens (same as php function token_get_all)
const tokens = parser.tokenGetAll('<?php echo "Hello World";');

// Load a static file (Note: this file should exist on your computer)
const phpFile = fs.readFileSync("./example.php");

// Log out results
console.log("Eval parse:", eval);
console.log("Tokens parse:", tokens);
console.log("File parse:", parser.parseCode(phpFile));
```

## Sample AST output

```js
{
  'kind': 'program',
  'children': [
    {
      'kind': 'echo',
      'arguments': [
        {
          'kind': 'string',
          'isDoubleQuote': true,
          'value': 'Hello World'
        }
      ]
    }
  ]
}
```

- Try it online (demo) : http://glayzzle.com/php-parser/
- Or from AST Explorer : https://astexplorer.net/

## API Overview

The main API exposes a class with the following methods :

- **parseEval**(String|Buffer) : parse a PHP code in eval style mode (without php open tags)
- **parseCode**(String|Buffer, String filename) : parse a PHP code by using php open tags.
- **tokenGetAll**(String|Buffer) : retrieves a list of all tokens from the specified input.

You can also [pass options](https://github.com/glayzzle/php-parser/wiki/Options) that change the behavior of the parser/lexer.

## Documentation

- [AST nodes definition](https://php-parser.glayzzle.com/api/ast.js)
- [Sandbox](https://php-parser.glayzzle.com/demo)
- [List of options](https://php-parser.glayzzle.com/guides/options)

## Related projects

- [prettier/plugin-php](https://github.com/prettier/plugin-php) : Prettier PHP Plugin
- [babel-preset-php](https://gitlab.com/kornelski/babel-preset-php) : Babel preset for converting PHP syntax to JavaScript. It can run subset of PHP in the browser or in Node.js
- [wp-pot](https://github.com/rasmusbe/wp-pot) : Generate pot file for WordPress plugins and themes
- [crane](https://github.com/HvyIndustries/crane) : PHP Intellisense/code-completion for VS Code
- [php-unparser](https://github.com/chris-l/php-unparser) : Produce code that uses the style format recommended by PSR-1 and PSR-2.
- [php-writer](https://github.com/glayzzle/php-writer) : Update PHP scripts from their AST
- [ts-php-inspections](https://github.com/DaGhostman/ts-php-inspections) : Provide PHP code inspections written in typescript
- [php-reflection](https://github.com/glayzzle/php-reflection) : Reflection API for PHP files
- [vscode-phpunit](https://github.com/recca0120/vscode-phpunit) : vscode phpunit extension
- [lua2php](https://www.npmjs.com/package/lua2php) : a Lua to PHP transpiler

> You can add here your own project by opening an issue request.

## License

This library is released under BSD-3 license clause.

---
_Source: https://npm.io/package/php-parser · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
