Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4054156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T14:32:20+00:00 2026-05-20T14:32:20+00:00

The PHP function eval() appears to be a rather interesting function. Can someone explain

  • 0

The PHP function eval() appears to be a rather interesting function. Can someone explain why it works in this given situaton, on line: 14

function Parse($inFrontEnd)
{       
    // Now create an array holding translation tokens with some from above
    // Load translation table into buffer
    $tableLines = file(Utilities::GetRelativePath(TTABLE_DIR).TTABLE); // Array of lines from TTable.cfg
    // Explode by whitespace
    foreach($tableLines as $aLine)
    {
        $lineParts = EXPLODE(' ', $aLine);
        $word = "/".$lineParts[0]."/";
        $definition = $lineParts[1];
        // Add key (word) => value (definition) to array
        // Eval() to return value of the const
        Main::$translateChars[$word] = eval("return $definition;"); 
    }
    // Read data from template file
    $parseArray = file($inFrontEnd); // Load FrontEnd source code into array ready for parse
    /* Perform the translation of template by the translation table defined data */
    $parseArray = preg_replace(array_keys(Main::$translateChars), array_values(Main::$translateChars), $parseArray);
    return $parseArray;
}

So what I’m doing here is reading in a template from a template directory. The templatename.php file comprises of text tokens written constant-like, which are then translated by regular expressions replacing the tokens with the data the constants with their names hold, thus returning a fully validated page of a webpage, which is printed for the user to view. This allows pages to be very dynamic by allowing the reuse of these tokens over many webpages (templates).

My question is: I had trouble for a while with the line that uses eval(). What I’m trying to do there is fill an array with each key being the name of the constant read in from, what I’ve named, the translation table (TTable.cfg), which holds the name of each token and the constant associated with it:

TITLE TITLE
CSS_INCLUDE CSS_INCLUDE
SHOW_ALL_POSTS SHOW_ALL_POSTS
…

So with the protocol [TOKEN] [CONSTANT][CR][LF]

The keys within the array would be created fine, but the values would return null or break my code when I had the key be associated with: constant($definition);
It complained it couldn’t find the constants being declared. However, when I use eval as is on this line, each key associated with: eval(“return $definition;”);
it works as I want it – the values as their corresponding constant’s data.

I do apologise for the length of this post. I couldn’t find any other example for my question other than the case I found it in.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T14:32:21+00:00Added an answer on May 20, 2026 at 2:32 pm

    The value of the variable $definition is replaced into the string definition "return $definition;", as you’re using double quotes. Hence, what is passed to eval is essential something like this: "return FOO;", where FOO is the value of the variable $definition.

    Now, that code is evaluated using eval(), and the result is returned as the result of the evaluation, which is the value of the constant FOO (different in each iteration).

    Using the constant in this case makes more sense: It is faster, potentially securer, and more readable.

    if ( defined( $definition ) ) {
        $constval = constant( $definition );
    }
    else {
        $constval = $definition;
    }
    

    This will also give you some insight of why it works when using eval() and not just constant(); PHP replaces unknown constants with their respective names, thus eval works in your case. However, any other way would raise warnings and be a bad practice, as it doesn’t make clear what’s going on to the reader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get a PHP function go to a specific website when it
This example is from php.net: <?php function Test() { static $a = 0; echo
I'm using the PHP function imagettftext() to convert text into a GIF image. The
Is there a Php function to determine if a string consist of only ASCII
is there an alternative for mysql_insert_id() php function for PostgreSQL? Most of the frameworks
<?php function toconv(string) { $gogo = array(a => b,cd => e); $string = str_replace(
<?php function data_info($data) { if ($data) { while (!feof($data)) { $buffer = fgets($data); if
I'm looking for the name of the PHP function to build a query string
I have code with the following form: <?php function doSomething{ //Do stuff with MySQL
When using the php include function the include is succesfully executed, but it is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.