How to better separate PHP files from HTML code

Source: Internet
Author: User
Tags eval php file php code string format

As we all know, php is a server-side embedded html-based scripting language. However, for a website based on embedded html, the code will soon become huge and uncontrollable. How can we separate php code from html and make a lib (Template) similar to dw so that the page can be modified more easily and the code can be easily maintained?

Later, I read a lot of articles, saying that phplib can be implemented. After reading a few pages, I felt dizzy and lost my desire to read it. However, the problem still has to be solved. By chance, we were able to download the source code of the vbb Forum. After a rough look, we found that, except for the PHP file, we seldom saw the html code. I thought this was not the style I wanted. The only difference was that I knew that it put the html code in the database and called it through the php file. After a series of processing, use the eval function to bring the expected variables into the dynamic pages required for generation. In this way, I did not look at the vbb source code and transferred it to the eval function. The php Chinese manual introduces eval functions as follows:

Function: eval ()

Miscellaneous Library

Eval

Enter the value in the string.

Syntax: void eval (string code_str );

Return value: None

Correspondence type: data processing

Description

This function can be used to substitute variable values in a string for processing data in a database. The code_str parameter is the string to be processed. It is worth noting that the string to be processed must conform to the PHP string format and contain a semicolon at the end. The strings processed by using this function will be continued to the end of the PHP program.

Example
Reference content is as follows:

<? Php
$ String = 'cup ';
$ Name = 'coffee ';
$ Str = '$ name is included in $ string. <br> ';
Echo $ str;
Eval ("$ str =" $ str ";");
Echo $ str;
?>


In this example, the return value is

$ Name is installed in $ string.

The cup contains coffee.

The example test has no problems. However, when I test the following code, an error occurs:
Reference content is as follows:

<?
$ Aa = 'My name is yyy! ';
$ Str = '<input type = "text" name = "textfield" value = "$ aa"> ';
Eval ("$ str =" $ str ";");
Echo $ str;
?>


After thinking about it, I asked for help. In a series of solutions proposed by netizens, I finally ran successfully in this way:

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.