the_source
the_source

Reputation: 648

Best practice: where to put the PHP code?

I do admit this question is going to be a bit vague, but I will try to explain what I'm trying to accomplish by a few examples. I have some PHP code that loads a bunch of variables from the MySQL database, contains some declarations, some functions to quickly output HTML code etc. However I would love to do all that stuff before anything is sent to the client.

So I do:

<?php
include("somefile.inc");
function bla()
{
    ...
}

if (fails)
    echo "Error: ...<br />";

?>
<!DOCTYPE>
<html>
    <head>
        <script>
            ...
            <?php echo $someString; ?>
            ...
        </script>
    </head>
    <body>
        ...
    </body>
</html>

This is all fine and ok, until I get an error. The echo will not show in the browser because it's before all HTML... So I modified:

<!DOCTYPE>
<html>
    <head>
        <script>
            ...
            <?php echo $someString; ?>
            ...
        </script>
    </head>
    <body>
        <div class="error_block">
            <?php
            include("somefile.inc");
            function bla()
            {
                ...
            }

            if (fails)
                echo "Error: ...<br />";

            ?>
        </div>

        ...

    </body>
</html>

Now I can actually see errors, which is good. But now the problem arises that in the header, or scrips, I cannot access variables that will be loaded later on in the newly created error_block.

I really don't like splitting the code in the error_clock to some above the HTML document and some in the error_block. And I also don't want to use PHP's die() function which abrubtly ends the execution.

Anyone can give their 2 cents on this issue? Thanks.

Upvotes: 9

Views: 37148

Answers (3)

marklu
marklu

Reputation: 96

You should look into using try-catch blocks and generating exceptions if you want to do some post-processing with the error message, which includes display.

Upvotes: 0

Phil
Phil

Reputation: 11175

If you're looking for an alternate solution, I have one for you. What I like doing is having the logic in before the DOCTYPE

if(error) { $error = "Please do something" }

Than, down in the document I have a div just for the error (Thanks @Dave for the input)

<?php echo $error != '' ? '<div id="error">' . $error . '</div>' : ''; ?>

This div will not appear if there isn't an error (meaning $error is empty) and it makes it easier for you to style the error message the way you would like

#error { color:red; }

If you want to get fancy, you can use some jQuery to hide/show the div so that the error doesn't have to persist.

$('#error').show().delay(7000).fadeOut();

Upvotes: 5

Ryan
Ryan

Reputation: 1888

What is often forgotten is that PHP is an INLINE programming language in essence, this means it is designed to be processed by the server as the server reads down the page, and with this it is designed to be split up into chunks. Recently OOP (Object Oriented Programming) has been introduced to PHP making it more flexible.

So with this information in hand I would take the OOP path in this case and do something like:

<!DOCTYPE>
<?php
include("somefile.inc");
function bla()
{
    ...
}
function failureError($code){
    if(!empty($code)) ...
}
if ($a = $b) {
    code goes here 
} else {
    $code = 'error123';
}
?>
<html>
    <head>
        <script>
            ...
            <?php failed($code); ?>
            ...
        </script>
    </head>
    <body>
        ...
    </body>
</html>

By writing using functions you can cut down your development time and group the majority of your code just calling what you need when you need it.

Another way of declaring your error class(es)/functions to help with server response time is to do something like:

if ($a = $b) {
    code goes here 
} else {
    include("errorStuff.php");
}

This will only include the error class(es)/functions when an error is encountered.

Just remember when you're writing PHP with OOP techniques like this that the server will take longer to process the script than if you write inline. The biggest advantage to an OOP basis is it will cut down your development time and if done correctly it will make it easier to administer future updates to your script.

Upvotes: -1

Related Questions