Open In App

PHP token_name() Function

Last Updated : 25 Aug, 2023
Suggest changes
Share
Like Article
Like
Report

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


Article Tags :

Explore