phpphp-include

PHP Include - Error Fixing


I have the following PHP code that produces an error as the include files don't exist. I have not yet made them but I want to stop errors being produced (not just hidden). Is there anything I can put into my code that says "don't record any errors if the file doesn't exist, just ignore the instruction"

<?php
  $PAGE = '';
  if(isset($_GET['page'])) {
    $PAGE = $_GET['page'];
  };

  switch ($PAGE) {
    case 'topic': include 'topic.php';
    break;
    case 'login': include 'login.php';
    break;
    default: include 'forum.php';
    break;
  };
?>

Solution

  • Include the files only if they exist. You can add a check for existing file -

    switch ($PAGE) {
        case 'topic': 
           if(file_exists(path_to_file)) {
               include 'topic.php';
           }
        break;
        ......
    };
    

    file_exists()