In one of the comments on this question I learned that it is technically wrong to do a separate php file for each page in excess of index.php . Then the question is: how can we make it so that when switching to another page, the previous content is deleted and the new one appears if we are in the same index.php ?

Yes, I understand, for this purpose, special classes are written for routing, but I want to start with something simple, that I understand how it works, and then complicate it. Therefore, in this question, I ask you to show this example: when you click on the “Other page” menu item, we should stay at index.php , but the “This is the main” message should disappear, and something else should appear instead - it will be a transition imitation to another page using the navigation menu. Naturally, I ask you to do this in pure PHP without JavaScript .

 <ul> <li><a href="#">Главная</a></li> <!-- неверно --> <!-- <li><a href="/other_page.php">Другая страница</a></li> --> </ul> <div> Это главная </div> 
  • The point is not to shove all the pages in index.php. Quite the contrary - in index.php there is nothing at all about any pages, only routing, which calls the necessary function, and a page is already generated inside this function. The approach that you represent is much worse than the approach “every page by file”. - Alexey Ukolov
  • Well, well, here we are, routing caused some function, and we generated HTML-code. Now we go to another page, which means routing should call the necessary function again. What does this feature do? Reloads the page, removes the output HTML-code and substitutes the new content? Why I said “reloads the page” and not “leaves it” because we both remain in the index.pnp file and remain in it. - Bokov Gleb
  • one
    Do you understand that what you are describing are two different invocations of the index.php file with different parameters? The script, as soon as it returns the page, immediately stops its work, you just can not stay in it. - Alexey Ukolov

2 answers 2

If you want to stay on the index page, and this should have the effect of switching to other pages, then use the $ page parameter to identify other pages:

 <ul> <li><a href="/">Главная</a></li> <li><a href="/?page=another_page">Другая страница</a></li> ... </ul> 

In the script code itself, you divide the contents by the condition:

 <?php ... if($_GET['page'] == 'another_page'){ Вывод содержимого другой страницы } else { Вывод содержимого индекс страницы } 

In this case, you get the effect of multipage. The content of the pages itself can be split into html files and uploaded via file_get_contents (), or placed in php files and included with include:

 <?php ... if($_GET['page'] == 'another_page'){ include_once 'include/another_page_content.inc.php'; } 
  • Great, this is exactly what I wanted to see! Thank you for your reply! - Lateral Gleb

Each time you click on a link, the page is reloaded from the server. Even if in fact the server handles everything using the same php file. Separate in your mind the URL and the physical address of the file — these things are not related to each other, although it seems so.

A module such as mod_rewrite redirects requests from different URLs to a single file, as if this file is indeed located at that address. It can be anything: index.php , notindex.php , engine/core/rewrite/lib/start.php - any file can respond to the request http://site/news/all , if mod_rewrite configured like this

For example, when talking to the server at http://site/foo/bar you can configure mod_rewrite so that this request goes to another file, and url goes as a variable: http://site/startpoint.php?url=foo/bar

By getting the page address in the form of a GET variable, you can determine which page title and content to display.