VOOZH about

URL: https://www.geeksforgeeks.org/php/php-token_get_all-function/

⇱ PHP token_get_all() Function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

PHP token_get_all() Function

Last Updated : 25 Aug, 2023

The token_get_all() function is an inbuilt function in PHP that is used to tokenize a given PHP source code string into an array of tokens. This function is particularly useful for analyzing, parsing, or manipulating PHP code programmatically.

Syntax:

token_get_all(string $code, int $flags = 0)

Parameters: This function accepts two parameters that are described below.

  • $code: This is the string that is going to tokenize.
  • $flags: This is an optional parameter that modifies the tokenization behavior of the string.

Return Values: The token_get_all() returns array contains elements where each element represents a token.

Program 1: The following program demonstrates the token_get_all() function.


Output
Token: T_OPEN_TAG - Content: <?php 
Token: T_ECHO - Content: echo
Token: T_WHITESPACE - Content: 
Token: T_CONSTANT_ENCAPSED_STRING - Content: "Hello, world!"
Token: ;
Token: T_WHITESPACE - Content: ...

Program 2: The following program demonstrates the token_get_all() function.


Output
Token: T_OPEN_TAG - Content: <?php

Token: T_WHITESPACE - Content: 
Token: T_VARIABLE - Content: $message
Token: T_WHITESPACE - Content: 
Token: =
Token: T_WHITESPACE - Content: 
Token: T_CONSTAN...

Reference: https://www.php.net/manual/en/function.token-get-all.php

Comment
Article Tags:
Article Tags: