# Overview

php-parser

&#x20;[![](https://api.travis-ci.org/glayzzle/php-parser.svg?branch=2.2.0)](https://travis-ci.org/glayzzle/php-parser) [![Coverage Status](https://coveralls.io/repos/github/glayzzle/php-parser/badge.svg?branch=master\&v=20170115)](https://coveralls.io/github/glayzzle/php-parser?branch=master) [![](https://badge.fury.io/js/php-parser.svg)](https://www.npmjs.com/package/php-parser) [![](https://img.shields.io/npm/dm/php-parser.svg?style=flat)](https://www.npmjs.com/package/php-parser) [![](https://img.shields.io/badge/GITTER-join%20chat-green.svg)](https://gitter.im/glayzzle/Lobby) [![](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser?ref=badge_shield)

This javascript library parses PHP code and convert it to AST.

## Installation

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

```bash
npm install php-parser --save
```

## Usage

```javascript
// initialize the php parser factory class
var fs = require('fs');
var path = require('path');
var engine = require('php-parser');

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

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

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

// Load a static file (Note: this file should exist on your computer)
var 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

```javascript
{
  '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://github.com/glayzzle/php-parser/blob/master/docs/AST.md)
* [List of options](https://github.com/glayzzle/php-parser/wiki/Options)
* [Main API](https://github.com/glayzzle/php-parser/tree/master/docs)
* [Lexer API](https://github.com/glayzzle/php-parser/blob/master/docs/lexer.md)
* [Parser API](https://github.com/glayzzle/php-parser/blob/master/docs/parser.md)

## 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

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

## License

This library is released under BSD-3 license clause.

[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fglayzzle%2Fphp-parser?ref=badge_large)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://php-parser.glayzzle.com/readme.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
