Thursday, September 25, 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
32319 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6680 POSTS0 COMMENTS
Nicole Veronica
11852 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6794 POSTS0 COMMENTS
Ted Musemwa
7070 POSTS0 COMMENTS
Thapelo Manthata
6752 POSTS0 COMMENTS
Umr Jansen
6761 POSTS0 COMMENTS