Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Formatting Php With Html
xClawx
post 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!
Go to the top of the page
 
+Quote Post
andrewsmithy
post 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!
Go to the top of the page
 
+Quote Post
SystemWisdom
post 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)
[...]

Now we can combine that the print() function, like so:
print("Welcome to my website!");
?>

[...]
*



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!
Go to the top of the page
 
+Quote Post
Lozbo
post 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.
Go to the top of the page
 
+Quote Post
xJedix
post 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:

  • 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!
*



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
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. where did you learn html from?(89)
  2. Does Anyone Know Where I Can Get A Free Html Maker(15)
  3. The Best And Free Website/html Editors + Text Editors(48)
  4. Html Tag For A Code Box(4)
  5. Html Countdown For Your Wallpaper For Your Computer?(5)
  6. Adding Rows & Columns In Html Table Using Javascript(1)
  7. Wanting To Touch Up/learn My Html Again(27)
  8. Learn Html Quick And Easy(14)
  9. Psd --> Editable Html(4)
  10. Help! Php Or Just Html?(13)
  11. Html Code Tester. Online Script(15)
  12. Where Is There A Good Site To Learn Web Html?(20)
  13. Has Anybody Tried Ms Expression Web Html Editor(3)
  14. Html Form!(4)
  15. Learn Html, Css, C And C++ Online For Free(12)
  1. Flash Media Into Html/css Website(1)
  2. Html Problem(9)
  3. What Is The Best Free Html Editor?(20)
  4. Add Flashing Inbox To Invisionfree Forum(2)
  5. Naming Web Page Files(9)
  6. Making A Picture Viewer Website(3)
  7. Sitepoint: The Ultimate Html Reference(0)
  8. Html, Xhtml, And Css, Sixth Edition (visual Quickstart Guide) Review(0)
  9. Html Based Emails On Hotmail(0)
  10. Flippingbook Html Edition(0)
  11. Create Dynamic Html/php Pages Using Simple Vb.net Code(1)
  12. Some Questions On Html(8)
  13. Accidently Deleted Template.html(3)


 



- Lo-Fi Version Time is now: 13th October 2008 - 01:05 AM