PHP Include - inserting a file into a page

The include construct automatically inserts additional files/scripts into the current page.

As far as PHP is concerned, it will be automatically executed and understood as if it had always been at that location.

Example:

php
include 'news.html';

Practical use

  • Common menus and menus,
  • News, updates, news and the same content,
  • Header or footer, ...

Dynamic file loading

We often need to load files dynamically, for example based on a variable.

For example:

php
$clanek = 'neco';
include $clanek . '.html';

Or we can dynamically load articles into the page:

php
include 'articles/' . $_GET['page'] . '.html';

Calling a URL with the page parameter will automatically insert the article, for example: https://example.com/?page=novinky.

Newsletter

Nejlepsi tipy a triky o PHP do Vaseho e-mailu. Clanky a novinky nejen ze sveta PHP a programovani.