Home Backend Development PHP Tutorial Ideas on how PHP generates HTML

Ideas on how PHP generates HTML

May 15, 2018 pm 05:16 PM
html php Ideas

This article mainly introduces the idea of ​​how PHP generates HTML. Interested friends can refer to it. I hope it will be helpful to everyone.

Currently, the news release systems of many websites on the Internet use dynamic server technology to generate static HTML. The benefits of this are: first, it can reduce the burden on their servers, and second, it generates HTML static pages. , so its website has a greater chance of being searched by search engines. The author's website once used PHP, a dynamic technology, to build a news release system. The principle is to apply PHP's technology to generate HTML static pages. The relevant platform is Windows XP Sp2 php4.32 mysql. Therefore, here, I want to briefly talk about it. Let’s look at the idea of ​​​​this approach. This article is suitable for friends who have some basic knowledge of PHP MYSQL database operations, SQL statements and web design. If you are a friend who wants to learn from scratch, then please lay a solid foundation first! There is no need to look down here. If you meet the above conditions, congratulations, please read on. However, before actually building it, you need to make the following preparations.

1. With the function of local debugging of PHP

Under the WINDOWS XP operating system, the author recommends that you download a PHP MYSQL APHCHE server package from the Internet, such as Huajun Software Park, and search there It can be downloaded in one click. After downloading, you can install it by default. This way you will have the ability to test PHP locally, saving you a lot of trouble with manual configuration. How about, keep it simple, OK, this is just the first step.

2. Conceive the functions of the news release system

News releases on the homepage are often updated through the background. The updates in the background are nothing more than basic functions such as adding, editing, and deleting data. realized. Here, you can use web design software to build the backend interface you want. Of course, PHP is used to implement its functions. In this step, it is recommended that you first think about the functions that the news release system should have. Here, how to use PHP to add, edit, and delete data will not be repeated, because the focus is on how to generate static technology on this basis.



#3. The technical principle of PHP generating HTML.

Ha ha. Fei has said so much, and finally it’s time to talk. In fact, this principle is not complicated. Generally speaking, it should be an application of replacing data syntax in PHP. OK, let’s talk about a simple example and analyze it step by step! I believe you are smart and can understand it clearly. Just watch every step carefully. Here, I just guide you on how to do it. You can practice it in detail!

(1) Create a new database in MYSQL and name it database (can be customized). Create a new table and name it news (because it is a news release, just give it a name that is easy to remember. You can customize it. Definition), and then create these field names:
id (auto-increment, this is the key, type: INT)
title (as the name suggests, news title, the type can be TEXT)
content (news content, type TEXT is optional)
path (HTML file path, type is TEXT)

(2) Create conn.php
This is the PHP file to connect to the database. You can put the statement to connect the data separately. In this file, multiple files that need to connect to the database in the future can directly reference this file.

(3) Design the add.form form for adding news. The simple source code is as follows:

 <form method=”post” action=”add.php”> //提交至 add.php 
  新闻标题:<input type=”text” name=”title” size=”20”><br> 
  新闻内容:<textarea name=”content” cols=”10” rows=”25”></textarea><br> 
  <input type=”submit” name=”提交”> 
 </form>
Copy after login

(4) Create an HTML template, save it as model.htm, and add.php. in the same directory.
Sample source code:

 <html> 
  <body> 
  此新闻的标题:{title} 
  此新闻的内容:{content} 
  </body> 
  </html>
Copy after login

{ } The content within the curly brackets is the content to be replaced. The design of the entire static template can be based on your own ideas, but the replaced content within { } must include Inside, such as {title}, {content}; Kaka~ Simply put, after designing a good-looking news template, put the tags to be replaced, such as {title}, {content}, etc. into the required Just place it.

(5) Detailed explanation of add.php source code

##

 <?php 
  require_once(“conn.php”); //引用conn.php,连接数据库 
  $title=$_POST[“title”]; 
  $content=$_POST[“content”]; //获得表单变量 

  //以下建立一文本文档,其值自动计数 
  $countfile="count.txt"; 
  if(!file_exists($countfile)) 
  { 
  fopen($countfile,"w"); //如果此文件不存在,则自动建立一个 
  } 
  $fp=fopen($countfile,"r"); 
  $num=fgets($fp,20); 
  $num=$num+1; //每次其值自动加一 
  fclose($fp); 
  $fp=fopen($countfile,"w"); 
  fwrite($fp,$num); //更新其值 
  fclose($fp); 

  //利用上面自动计数的值获得HTML的路径$path 
  $houzui=”.html”; 
  $path=$num.$houzui; 
  //这样形成的路径是自动增长的,如1.html,2.html,3.html……….添加一条新闻便自动加上1 

  //以下用SQL语句添加数据至表 news 
  $sql=”insert into news (title,content,path) values (‘”.$title.”’,’”.$content.”’,’”.$path.”’)”; 
  $query=mysql_query($sql); 

//以下为关键之处,把从表单获得的数据替换模板中的{title},{content}标记   

  $fp=fopen(“model.htm”,”r”) //只读打开模板 
  $str=fread($fp,filesize(“mode.htm”));//读取模板中内容 
  $str=str_replace(“{title}”,$title,$str); 
  $str=str_replace(“{content}”,$content,$str);//替换内容 
  fclose($fp); 

  $handle=fopen($path,”w”); //写入方式打开新闻路径 
  fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件 
  fclose($handle); 


//收尾工作: 
  echo “<a href=$path target=_blank>查看刚才添加的新闻</a>”; 
?>
Copy after login


OK, the entire sample source code for generating HTML is here. The key is to use the replacement method. $str=str_replace("{replaced content}",$replaced content,$str);

Therefore, to summarize the above approach: first design the news template and put the information that needs to be replaced Use { } to put the content in the corresponding position in the template, then design the form, and then the final form handler, replace the variables obtained from the form with the corresponding content in the template, so that different HTML will be generated every time; The same is true if you need to modify the HTML content. After obtaining the modified form content, first update the database with the update statement, and then replace the content in the template; if you want to delete, first delete the content to be deleted in the table, and then use unlink($path) to delete the physical file of HTML.

Related recommendations:

php htmlspecialchars example code detailed explanation

php HtmlReplace input filtering security function example code

Recommended 10 articles about the php html_entity_decode() function

The above is the detailed content of Ideas on how PHP generates HTML. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles