Search code examples
phphtmlcssgoogle-chromerequire-once

Is there a -webkit for Chrome or a way to get rid of whitespace caused by PHP require_once()?


enter image description here

Okay, so I have this little issue with PHP creating whitespace at the top of my web site when I use include(), include_once(), require(), or require_once(). I have figured out how to adjust the positioning of the appropriate elements using the CSS -webkit and -moz keywords but I can't figure out how to adjust the positioning in Chrome.

I've included a screenshot of three different browsers (Edge, Firefox, and Chrome).

Here is the CSS for the blue div on the left side of my web site.

#nav_menu {
position: fixed;
height: 100%;
width: 300px;
background-color: #3C7AF3;
opacity: .6;
padding: 50px 0px 0px 0px;
z-index: 2;
-moz-transform: translateY(-2px);

-moz-transform: translateY(-2px); worked in Firefox...

• I've tried to fix the whitespace by "converting to UTF-8 w/o BOM" in Notepad++ (it didn't work).

• I need there to be a sort of -chrome-transform: translateY(-2px); here but that's not a real code.

• NOTICE: -webkit-transform: translateY(-20px); doesn't work for Chrome

}

If I can get rid of the whitespace caused by PHP's require_once() then everything will be fine...

This is on the first line of the web site (account.php):

<?php require_once('../includes/initialize.php'); ?>

This is initialize.php:

<?php 
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);

defined('SITE_ROOT') ? null : 
    define('SITE_ROOT' , 'C:'.DS.'wamp64'.DS.'www'.DS.'my_company');

defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');

// 1. load config file first
require_once(LIB_PATH.DS."config.php");

// 2. load basic functions next so that everything after can use them
require_once(LIB_PATH.DS."functions.php");

// 3. load core objects
// require_once(LIB_PATH.DS."obj".DS."session.php");
require_once(LIB_PATH.DS."database.php");
require_once(LIB_PATH.DS."obj".DS."database_object.php");

// 4. load database-related classes
require_once(LIB_PATH.DS."obj".DS."user.php");
require_once(LIB_PATH.DS."obj".DS."item.php");
?>

Every single one of those php require_once()'s calls pure PHP.

Here's the kicker: I can comment out (or even delete) the entire initialize.php and it doesn't fix the whitespace.

Here's the code that require_once() is supposed to leave in the <head> but rather causes the <head> tag to end:

enter image description here


Solution

  • Junk characters at the beginning of the file is often a unicode byte-order mark. These characters tell the parser what endianess to use when parsing the file. They're irrelevant in UTF-8, but it's still possible to configure your IDE to insert them. You probably have these in your included files.

    If it were just regular whitespace, the usual culprit is PHP closing tags ?> appearing to early or where they don't need to be. For example, if you have a class file that you include and you have ?> with a blank line at the end, you've inadvertently included a blank line into the output of anything that includes this class. It's recommended to never close your PHP tags in files that are purely PHP. Also double check to ensure there is nothing before the opening PHP tag <?php.