Dear experts!

There is such a construction:

//Страницы сайта $page = isset($_GET['p']) ? $_GET['p'] : 'error'; switch($page){ //Home page case 'home': include($_SERVER['DOCUMENT_ROOT'].'/page/home.php'); break; //Страница услуг case 'services': include($_SERVER['DOCUMENT_ROOT'].'/page/service.php'); break; //Страница ошибок case 'error': include($_SERVER['DOCUMENT_ROOT'].'/page/error.php'); break; //Home page (default) default: include($_SERVER['DOCUMENT_ROOT'].'/page/home.php'); break; exit(); } 

This code is located in the index.php file in the root directory. According to the idea, when you open the index.php file, the default home.php file should be included - but for some reason it gives error.php (the error.php file opens) what is written wrong here? correct me please

  • your address is open like this mysite.ru?p= ? - Alexey Shimansky
  • @ Alexey Shimansky, other pages are opened in this way /? P = services, etc. - Maqsood
  • 2
    I'm not talking about other pages .... is this parameter present on the main page or not? ... if not, then you have p in the gett request does not exist .... respectively in the condition $page = isset($_GET['p']) ? $_GET['p'] : 'error'; $page = isset($_GET['p']) ? $_GET['p'] : 'error'; the variable is assigned the error value .... and then in the case according to the rule, it goes there - Alexey Shimansky

1 answer 1

The problem here, of course, is not in the switch / case statement, but in the logic of the person writing the code.

Here

 $page = isset($_GET['p']) ? $_GET['p'] : 'error'; 

literally written

when opening the file index.php without parameters, error.php should be included

which is somewhat contrary to the stated desires.

to open the default home page, and for non-existent pages - an error, you must do so

 $page = isset($_GET['p']) ? $_GET['p'] : 'home'; switch($page){ //Home page case 'home': include($_SERVER['DOCUMENT_ROOT'].'/page/home.php'); break; //Страница услуг case 'services': include($_SERVER['DOCUMENT_ROOT'].'/page/service.php'); break; //Страница ошибок default: include($_SERVER['DOCUMENT_ROOT'].'/page/error.php'); break; } 

In general, the word default is quite interesting. It would seem, what is the relationship between "default" (bankruptcy) and "default" (default value)? But just the right translation and puts everything in its place: default is, relatively speaking, a shortage, a shortage. A "by default", respectively - "in the absence of other options", "if no one option came up."

That is, in this case, the default page should be exactly error.php

  • Thank you very much! I was looking for a video lesson so much now - where I learned about switch / case at all - in that video lesson I learned about such code. Logic, of course I do not understand, to be honest, I'm just starting to learn PHP. - Maqsood