Jump to content
xisto Community
Sign in to follow this  
xClawx

Formatting Php With Html

Recommended Posts

PHP is only a scripting language, meaning you will need HTML to make the output look good to the user. However if you insert HTML normally PHP will generate some errors, so read this tutorial to learn exactly how you need to format HTML for it to work with PHP.

 

Lets say we want to display to our users the following:

 

Welcome to my website!

 

In HTML the code for that would be:

 

Welcome to my website!

 

However in PHP we need to add a forwards slash () in front of every quotation mark so that we dont interfere with the quotation marks in the PHP functions. Lets take a look at the same code as above, only this time it has been formatted to work with PHP commands:

Welcome to my website!

 

Now we can combine that the print() function, like so:

print("Welcome to my website!");

?>

 

That will produce the following given it is saved on a .php page.

Welcome to my website!

Share this post


Link to post
Share on other sites

Formatting in PHP can be a very hard to re-trace, with code output that looks very messy. Here are a couple of helpful ways to make your PHP code look like a neatly formatting HTML page, that is easy to debug and read.

 

The problem with using PHP is that the script and the HTML code become very unorganized and cluttered if you don't plan your pages properly. One very good technique that I use, I like to call the "header/footer" technique. It's very simple. You make a .html file or .php that simply has the html head></head> tag in it. You can use the same file for all of your pages on your site, without having to re-write it in very page. Here's how it works. First, create a file called header.php, like this:

 

<html><head><title>My Website</title></head><body>

This is your header file. Now, you can include it in all your PHP pages. You do this by a simple include directive, that tells the PHP processor to include the specified resource. Here is an example PHP script:

 

<?phpinclude "header.php"; // This line include the header fileecho 'This is page 1.';?>

If you check this page out, you will see the the header has been included into the output. You could even make a footer that includes copyright information, and the closing tags. You would just include it after your generated content. Now here's where it gets fun. You can dynamically set the title and other attributes, without having to modify the header for every PHP script. That's what PHP is all about! Dynamically generated content. Ok we need to modify our header.php script to look like this:

 

<html><head><title><?php if (isset($title)) { echo $title; } else { echo "Default Title!"; } ?></title></head><body>

We changed it to check for the variable $title and if it was set, display it. Otherwise it would display "Default Title!". Back in the php page, we can set the $title variable, of course, before we include the header.php file. It will look somewhat like this:

 

<?php

 

$title = 'Page 1\'s Title!';

include "header.php";

 

// rest of the page...

 

include "footer.php";

?>

 

So, that's it. Go start coding some php!

Share this post


Link to post
Share on other sites

[...]

 

Now we can combine that the print() function, like so:

print("Welcome to my website!");

?>

 

[...]

179526[/snapback]


You should use single-quotes when outputting text and HTML for 2 reasons:

Your HTML may then contain normal double-quotes without requiring the slashes that make it look ugly.. Ex: '<table border="0">' instead of "<table border=\"0\">"

By using double-quotes you are telling PHP to invoke the string parser, which looks for variables and such inside strings, but by using single-quotes you avoid the overhead of the string-parser in PHP... this can decrease your scripts execution time.. Only downfall is that you have to end your strings to add variables.. Ex: '<table border="'.$BorderWidth.'">' instead of "<table border=\"$BorderWidth\">"

I hope you understood that, and I hope it helps!

Share this post


Link to post
Share on other sites

Yes... About the php "include" function, and the way andrewsmithy states he builds pages, i think that it is a great way to optimize and save time, in case of a major update or something, becouse all you need to do is change one .php file that is included in a big quantity of other files which use that content.So instead of using a frameset, which will contain one page as a menu, you can have a menu.php and including it in all your other pages, so when a change is needed, menu.php will be the only file to be changed, and once uploaded, it will take place in all your documents.There was i time when i thought that this kind of including files was not so apropiate, i used to think that the include function was only designed for adding variable definitions or other functions written in php code within a document, but it what php does when it includes a file, is take that file and pastes it right as it is into your document, so if it does not contain any kind of php code, it will still work, for example if you put only html code, like a menu inside a table or any kind of tags, so even if this function was actually designed for that purpose (including files written with more php code), you can take advantage of it doing this kind of including for pasting whatever you want.

Share this post


Link to post
Share on other sites

You should use single-quotes when outputting text and HTML for 2 reasons:

Your HTML may then contain normal double-quotes without requiring the slashes that make it look ugly..  Ex: '<table border="0">' instead of "<table border=\"0\">"

 

By using double-quotes you are telling PHP to invoke the string parser, which looks for variables and such inside strings, but by using single-quotes you avoid the overhead of the string-parser in PHP... this can decrease your scripts execution time..  Only downfall is that you have to end your strings to add variables..  Ex: '<table border="'.$BorderWidth.'">' instead of "<table border=\"$BorderWidth\">"

I hope you understood that, and I hope it helps!

179734[/snapback]


That is totally true. But i think some of it is personal preference and which way you learned to do it in the beginning. For me, I am just used to doing "" and using slashes with html, but the single quotes can come in handy in many cases. Its a nice feature they added to php, I forget which version they did that.

 

Also, you can use the require function instead of the include function for doing headers and footers. Does anyone know the difference between require and include? I have never figured that out cause both of them work in the same general way.

 

Another thing, using headers are very handy. Especially when your site has a login system. On mine, I have most of my login scripting in my header which I include on everyone of my .php pages on my site. So, just another example for what headers can be used for... But there are endless amount of possiblities in using headers and footers.

 

xJedix

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.