Thursday, September 4, 2025
HomeLanguagesPHP token_name() Function

PHP token_name() Function

The token_name() function is an inbuilt function in PHP that is used to retrieve the textual representation of a given token identifier. In PHP, when you write code, it gets parsed into a series of tokens, which are the basic units of code that the interpreter understands. These tokens include keywords, operators, constants, variables, and other elements of the code.

Syntax:

string token_name(int $token)

Parameters: This function accepts only one parameter which is described below.

  • $token: An integer representing a token constant.

Return Value: The token_name() function returns the symbolic name of the given token.

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

PHP




<?php
  
// Example token constant
$token = T_IF;
  
$tokenName = token_name($token);
  
echo "Token name for $token: $tokenName";
  
?>


Output

Token name for 322: T_IF

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

PHP




<?php
  
// Example token constants
$tokens = [T_IF, T_ECHO, T_FOREACH]; 
  
foreach ($tokens as $token) {
    $tokenName = token_name($token);
    echo "Token name for $token: $tokenName\n";
}
  
?>


Output

Token name for 322: T_IF
Token name for 324: T_ECHO
Token name for 330: T_FOREACH

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

RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6632 POSTS0 COMMENTS
Nicole Veronica
11800 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11859 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS