Insert PHP code in HTML. How to do it right?

The Internet has firmly entered our lives, and we cannot imagine our existence without it. Waking up, the first thing we do is open our favorite social network to read the latest news. And during the day we post on our blogs new photos and stories from our lives. Thousands of sites are born every day, and the same amount dies. But few people think what is on the other side of the monitor. Few people know what the site consists of and by what principles it works. Today we want to tell how to embed PHP code in HTML and what it is for.

What is HTML?

In order to go directly to the topic, you need to find out what the site consists of, how it is designed. If you open any page in the browser, you can see that it has text, pictures, and sometimes videos. And there is no code there. But he is there! What we see in front of us is the display of the code. It is necessary to press the key combination CTRL + U, and in the next window we will find it. This is HTML.

Code writing

But HTML is not a programming language; it is just hypertext markup. It cannot perform any logical operations, as programming languages ​​do. In another way, HTML is responsible for the location on the page of content: text, images, media, video, tables. Once, even at the dawn of the Internet, all sites consisted of solid HTML, but this turned out to be not very convenient. Such sites are called static, and to change the content on the page you had to edit all the code. So server programming languages ​​were invented, and one of these languages ​​is PHP. After that, there was a need to embed PHP code in HTML. How to do this, we will describe below.

What is PHP

PHP code is not displayed in the browser. This is a server-side programming language. How to understand this? A server is a remote computer. When the user performs some action on the page: fills out the registration form, presses buttons, writes a message - all the results of his actions are sent to the server. There they are processed and displayed back on the screen. So the user sees the result of their actions.

HTML code in browser

When server programming languages ​​began to be used, it became necessary to embed PHP code in an HTML page. How to do this correctly will be described below. PHP can interact not only with HTML, it also acts as an intermediary when working with databases. There are special functions that make queries to databases: select data from them, modify, delete, update, and so on.

Why it is impossible to create a site on one HTML

As mentioned above, sites on HTML only are static sites. You cannot change content on them without editing the code itself. PHP provides great site management capabilities. In particular, to manage content from the administrative panel directly in the browser without going to the server to change the code. This turned out to be a very convenient solution: the site administrator can edit or add content on their own, without asking the programmer for help.

PHP code in the editor

Thanks to the PHP language, site management systems have appeared. The most famous of them are Wordpress, Joomla, DLE. There are a lot of CMCs, and more and more are appearing every day. But every self-respecting webmaster should be able to edit the code if such a need arises. He should know how to embed PHP code in HTML without having to go to the programmer every time because of such a trifle.

Why do you need to insert PHP

When layouting HTML templates, dynamic insertion is almost always used. For what? Modern sites are multi-page portals, and the number of pages that they contain may vary depending on the task. Therefore, all modern sites use templates. The same template is displayed on all pages. This simplifies the work of programmers and provides an easy change in the display of the site in the browser.

PHP code insertion

To ensure this, a special PHP code is introduced into the HTML template, which provides a dynamic replacement of the entire interface. Without such inclusion, it is impossible to make a modern site. And how to insert html code into a php file, we will consider further.

Insert Options

Depending on how the site template is written, the insertion options may be different. If the template is written in HTML, then PHP code is embedded in it, and if it is written in PHP, on the contrary, HTML is included in PHP. Both methods will be considered here, and at the same time it will be possible to learn how to insert html code in php into a script.

In the first embodiment, everything is quite simple. The required PHP function is inserted into the HTML break. In the right place, open the tag with which PHP begins, after it the necessary function is written, then the tag closes. This is the most common insertion option, although it is not the only one.

Another way is to mount the file in the right place. All necessary content is placed in a separate file and connected to the template using the include directive. Now if you change the file you are looking for, then the contents of the template will change. There is another unconventional way, but you can learn more about how to insert html code in php in eof in the next article.

PHP code in a laptop

In the second option, consider a template written in PHP. In this example, inserting will be a little more complicated. In such cases, the ECHO function is used, inside which HTML code is enclosed. This design is also common, although it is not the only one. The HTML construct can be directly inserted into the PHP gap: then you need to close the function and, after inserting the HTML, write the PHP code again, starting with the opening bracket.

Conclusion

From this article, we learned how to embed php code in html. This is not at all difficult. There are many options, and everyone chooses his own, in his opinion more simple and acceptable in his situation. It is important to know that programming is a complex science, and, having read only one article, you cannot become a real programmer. You have to go a long way. Errors are inevitable at all stages, and this must be put up with. Only experience gained can give the result that you are striving for.

Source: https://habr.com/ru/post/C10899/


All Articles