PHP Include - inserting a file into a page

11. 9. 2019

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.

Jan Barášek
Jan BarášekVíce o autorovi

Autor článku pracuje jako seniorní vývojář a software architekt v Praze. Navrhuje a spravuje velké webové aplikace, které znáte a používáte. Od roku 2009 nabral bohaté zkušenosti, které tímto webem předává dál.

Rád vám pomůžu:

Newsletter

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