|
|
|
|
![]() ![]() |
Aug 28 2005, 07:46 PM
Post
#1
|
|
|
Newbie ![]() Group: Members Posts: 8 Joined: 28-August 05 Member No.: 11,242 |
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! |
|
|
|
Aug 28 2005, 09:30 PM
Post
#2
|
|
|
Newbie [Level 1] ![]() Group: Members Posts: 19 Joined: 16-March 05 From: United States Member No.: 4,570 |
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: CODE <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: CODE <?php include "header.php"; // This line include the header file echo '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: CODE <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: [CODE] <?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! |
|
|
|
Aug 29 2005, 12:13 AM
Post
#3
|
|
|
Advanced Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 117 Joined: 3-May 05 From: A Canadian South of the 49th Parallel Member No.: 6,544 |
QUOTE(xClawx @ Aug 28 2005, 03:46 PM) You should use single-quotes when outputting text and HTML for 2 reasons:
I hope you understood that, and I hope it helps! |
|
|
|
Sep 2 2005, 12:36 AM
Post
#4
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 282 Joined: 1-September 05 From: Wanatos Member No.: 11,382 |
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. |
|
|
|
Sep 2 2005, 01:20 AM
Post
#5
|
|
|
Premium Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 183 Joined: 27-June 05 From: Minnesota Member No.: 8,734 |
QUOTE(SystemWisdom @ Aug 28 2005, 06:13 PM) You should use single-quotes when outputting text and HTML for 2 reasons:
I hope you understood that, and I hope it helps! 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 |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 13th October 2008 - 01:05 AM |