Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8809023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:46:07+00:00 2026-06-14T02:46:07+00:00

I have recently begun building webpages using PHP, and being inexperienced, I have a

  • 0

I have recently begun building webpages using PHP, and being inexperienced, I have a question about coding conventions.
Is it considered bad style to use require to dynamically load page content or page elements in a web page?

For example, if you are modularizing your code, and have a /resources/pageContent/ folder with different files containing the content of different pages. Would it be considered bad style to say require ("resources/pageContent/profile.php")?
Another way I’ve seen this done is using fopen(), but of course, it won’t allow you do use any PHP code in your dynamically loaded pages and will only print the HTML and CSS.

I am just trying to put things into modules, since putting things in functions (For example, loadPageProfile) can get really messy-looking.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:46:08+00:00Added an answer on June 14, 2026 at 2:46 am

    First of all,
    If you want to write clean and maintainable code, DO NO USE error suppressor @
    Listen to all errors – big and small ones. This will help you greatly. Really.

    error_reporting(E_ALL);
    

    When you’re done, then just change

    error_reporting(0);
    

    I am just trying to put things into modules, since putting things in
    functions (For example, loadPageProfile) can get really messy-looking.

    Well, then you’re getting on a right track…
    All you gotta do is to create one module called, like, Page. (we are talking about pages now)

    It’s bad practice to use procedual code to create the modules since we are talking about them.

    Just encapsulate all logic into a class, like this:

    File Page.php

    abstract class Page {
    
       /**
        * Includes chunk of the page
        * it's Useful, when you have number of pages
        * and want for example only one chunk to be displayed
        * everywhere
        * This could be footer or menu or something like this "static" parts
        * 
        * @param string $block
        * @return void
        */
       public static function useBlock($block)
       {  
          $file = 'path_to_blocks' . DIRECTORY_SEPARATOR . $block; 
    
          //Ensure this is valid stream before we include it;
          if ( is_file($file) ){
             // No need to use require() here
             // Because we are sure that file exists and valid for inclusion
             // include is a bit faster that require()
             include($file);
          }
       }
    
       /**
        * Displays some page
        * This is just simply form of require, but
        * this method would simplify inclusion 
        * 
        * @param string $page
        * @return void
        */
       public static function DisplayPage($page)
       {
         $file = 'path_to_your_pages' . DIRECTORY_SEPARATOR . $page;
    
         if ( is_file($file) ){
            include($file); 
         }
       }
    
    }
    

    Now assume that you have pages: contact, index, profile, login, register So instead of using require() everywhere you simply call that “comfortable” method.

    While, the footer and menu could be similar to this ones:

    File: footer.phtml

    <div id="footer">Copyrigth (c) you and bla bla bla</div>
    

    File: menu.phtml

    <li><a href="/">Home</li>
    <li><a href="/register/">Register</a></li>
    <li><a href="/contact/">Contact</li>
    

    To require particular class you can create, some module, like this one, as well:

    class Import {
    
       /**
        * 
        * @param string $class Class File name to be required
        * @param string $ext filename extension (just to simplify )
        * @return bool
        */
       public static function getSomeClass($class, $ext = '.php'){
    
          $location = 'folder_of_classes' . DIRECTORY_SEPARATOR . $class . $ext;
          return spl_autoload_register(function() use ($location){
             // We won't use include() here
             // Because we'd to stop (producing fatal error) if inclusion would fail
             require_once ($location);
          });
       }
    
    }
    

    Then when you need particular class, just call

    <?php
    // requires MySQL_PDO.php located in defined foldet
    Import::getSomeClass('MySQL_PDO');
    

    And please remember, when we talk about Modules, in 99% cases we talk about classes that has its implementation within this one.

    Another advices, would be:

    1) Do no mix CSS with HTML (create separated css file and include it on particular page, via <link href="path_to_css.css" rel="stylesheet" type="text/css" />

    Because it makes markup clear and easy to maintain in future (say when you want to change the style or add something)

    2) Do not mix PHP and JavaScript code. Keep JavaScript files in separated files as well as CSS. Use Ajax to share variables between PHP and JavaScript

    3) Do not mix them all HTML,CSS,JavaScript with PHP. Especially HTML. Keep that modules (classes or business logic) in separated files. Then just include the part you need for particular task.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently begun working on a pre-existing PHP application, and am having a
I have recently begun writing unit tests (using GoogleTest ) for a C++ project.
I have recently begun using Scala. I've written a DSL in it which can
I've recently begun a fairly small 2D project using SFML and TinyXML and have
I usually script/program using python but have recently begun programming with JavaScript and have
I have recently begun learning the Win32 API using this tutorial: http://www.winprog.org/tutorial/ (though I'm
I have recently begun to think about optimisation, now I know there are many
I have recently begun coding a 3d computer game as part of my IB
I have recently begun using coderush. One thing that annoys me is that the
I have recently begun the practice of using <%# Ruby on Rails comments %>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.