PHP Framework-PHP itself tried to write the template engine, the last display method is actually require an HTML file, why output it?

Source: Internet
Author: User
Tags php framework
Does the PHP require function output when it encounters text?
This is the template class I wrote:
/** * User: Fire lizard making */namespace core;//template class template{private $data = []; Private $path = ';    Template path Public function __construct () {$this->path = config::get (' Project.template_path '); /** * Template Assignment * @param $key * @param $value */Public Function assign ($key, $value) {if (Is_ar        Ray ($key)) {$this->data = Array_merge ($this->data, $key);        } else {$this->data[$key] = $value; }}/** * Gets the path * @param $file */Private Function GetFilePath ($file) {$params = explode ('. '),        $file); The template path has been delimited $path = ROOT. Directory_separator.        $this->path;            foreach ($params as $key = + $param) {if ($key = = count ($params)-1) {$path. = $param; } else {$path. = $param.            Directory_separator; }} return $path.    '. html '; Public function display ($file) {if (Empty ($file){throw new \exception ("Template Can not is Empty");        } $realPath = $this->getfilepath ($file);            if (Is_file ($realPath)) {Extract ($this->data);        Require ($realPath); } else {throw new \exception ("Template:{$realPath}Not Found "); }    }}

Reply content:

Does the PHP require function output when it encounters text?
This is the template class I wrote:

/** * User: Fire lizard making */namespace core;//template class template{private $data = []; Private $path = ';    Template path Public function __construct () {$this->path = config::get (' Project.template_path '); /** * Template Assignment * @param $key * @param $value */Public Function assign ($key, $value) {if (Is_ar        Ray ($key)) {$this->data = Array_merge ($this->data, $key);        } else {$this->data[$key] = $value; }}/** * Gets the path * @param $file */Private Function GetFilePath ($file) {$params = explode ('. '),        $file); The template path has been delimited $path = ROOT. Directory_separator.        $this->path;            foreach ($params as $key = + $param) {if ($key = = count ($params)-1) {$path. = $param; } else {$path. = $param.            Directory_separator; }} return $path.    '. html '; Public function display ($file) {if (Empty ($file){throw new \exception ("Template Can not is Empty");        } $realPath = $this->getfilepath ($file);            if (Is_file ($realPath)) {Extract ($this->data);        Require ($realPath); } else {throw new \exception ("Template:{$realPath}Not Found "); }    }}

    1. requireNot a function

    2. requireThe function is to put the following string as the file name, regardless of the file extension is not .php , all think that the file is a PHP program, introduced into the current program run.

    3. If the PHP program is not?>包起来,就会直接输出。

require executes the referenced file as a PHP file, regardless of the suffix name of the file (no suffix is allowed), and some PHP Trojans use this to bypass firewalls. The
PHP code needs to be placed in the !--? php and . to execute.

Crab Demon.

When a file is included, the parser goes out of PHP mode at the beginning of the target file and enters HTML mode to recover at the end of the file. For this reason, any code in the destination file that needs to be executed as PHP code must be included in the valid PHP start and end tags.

include or requrie a file without PHP start and end tags !--? php?--> are parsed as HTML.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.