Jul 26, 2008

Formatting Php With Html

Free Web Hosting, No Ads > CONTRIBUTE > The Internet > Web Design

free web hosting

Formatting Php With Html

xClawx
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!

Reply

andrewsmithy
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!

 

 

 


Reply

SystemWisdom
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!

Reply

Lozbo
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.

Reply

xJedix
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

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Similar Topics

Keywords : formatting php html

  1. Naming Web Page Files - Which way you like- MyPage.html or my-page.html or my_page.html (7)
  2. Flash Media Into Html/css Website - does anyone know how to import a flash into a webpage with transparenc (1)
    Hi I need some help , Im designing this website for school studies However, I made a flash drop down
    menu, works perfectly, but you know how flash has a background when you export it in to a SWF file?
    For example my flash is width= 800, and height = 200 but my div box on my html page for my
    navigation is only 50 px my buttons is width of 50px and the rest of the content is the drop down
    animations i want to insert it into my navigation div box but i want to set the flash background to
    transparent so that when the drop down menu comes down it overlaps the text or whateva...
  3. Has Anybody Tried Ms Expression Web Html Editor - It's much better than Frontpage (3)
    I was desperate to finish a site I was designing after I lost my hard drive and my software. I
    downloaded Expression Web from the Microsoft site for a 3 month free trial, and guess what? It's
    surprisingly good! Nothing like that crappy frontpage, it's standards compliant and very
    good for writing CSS. Haven't heard anybody mention the program and wondering if anybody else
    has tried it out. It's free to try from the MS site....
  4. Where Is There A Good Site To Learn Web Html? - (18)
    im a noob when it comes to web html to design web sites, can some one tell me where to find a good
    website that has good tutorials on how to use web html?...
  5. Help! Php Or Just Html? - i want to start buliding my website. which is better, php or basic htm (13)
    i try to start this topic in webhost category but it seems like i cant. i dont have the permission
    so i just post my topic here. im sorry mod.. i want to build a website which contains: - Links to
    videos - Informations - photos - flash i don't know if i should use php or just HTML. guys,
    what are your opinions..??...
  6. What Is The Best Free Html Editor? - (8)
    im looking for any kind that is free really.... i use windows but if you have one for a different
    platfor, go ahead and add it for reference for other users. If you have one for normal text editor
    and wysiwyg, add them both. i have been using notepad as my normal html editor but im looking for
    something different... at least so i can see the different colors of the codes. Right now i am
    downloading moxilla's seamonkey suite but im not sure how good it is... i will post a review if
    i ever use one. If you have used the html editor you suggest or you just heard abou...
  7. Html Problem - (9)
    how to make my intro page in the middle not CENTER ...
  8. A Twist On Basic Authentification - html help (1)
    Alright, i am working on a website where a number of different users from different companies will
    be looking hooking into one website. What i want is to know how i can differentiate between the
    users based on the information passed by the webserver. I've been told to that information
    will be passed along html_user(and if i have a distinct user then i can just query the database with
    that info and get what i want) But how does this work? I am sorry i know this is cryptic i'm
    kind of searching to see if this strikes a bell with anyone. So to sum, many user...
  9. A Small Html Problem - How to display foreign characters correctly when designing a site. (4)
    I was wondering how I could solve a small problem. I was told that some people see accented and
    umlauted letters (such as " é " and " ë ") as question marks (" ? ") on my website. I come across
    the same thing sometimes when looking at websites which use non-English characters. Funnily enough,
    the other day, I looked at a site and the apostrophy ( ' ) was also shown as a question mark.
    That is a very common character usually, I would think. I thought it had something to do with the
    character encoding settings, and let me also mention I use Mozilla Firefox as my brows...
  10. Css List Vs Html List - (1)
    I am currently learning the basics of web building and i am wondering if and when you should use
    html lists or css lists? The way i see it is if you are going to make a css list you are going to
    use that list setup later and just make a html list when u just need it once? did i just answer my
    own question? or is there a web standard where you should always use css for lists? idk....
  11. Psd --> Editable Html - (2)
    Here's the deal. I am new at working with web objects in photoshop. I have CS3, and I created a
    really nice template. I'm told I have to do something with slicing. So I have the template, and
    it has images, buttons, and text on the buttons, and text fields, etc. How can I export this to HTML
    and images, and be able to open it up in dreamweaver in an editable web page? I've tried
    exporting to "web and devices" and exporting as HTML and Images, but when I open it up in
    dreamweaver, it looks right, but it is all images. Thanks...
  12. How To Make Pure Html Preloader? - anyone know's the answer? (7)
    Hello all today i run out on this website and I found it very interesting... Anyone know how they
    did that page? and im specialy interested in that HTML preloader... ?? how to make one??...
  13. I Buy A Book "html,xhtml&css Sixth Edition" By Elizabeth Castro - (2)
    After few years when I take a look of the web, I find that what I have learnt before is totally
    obsolescent, the standard is changed a lot. So I go to book store to look for a book to learn it
    from the beginning. I find there is too many books about web design. What I want is a book can tell
    me the situation about the popular using method. After looking for about 1 hour, I find this book
    "HTML,XHTML&CSS Sixth Edition" which was writen by Elizabeth Castro. I think this book is meet the
    needs of me. What I want is to get the information of the new standard of web desig...
  14. Php Or Html? - which do you use? (35)
    Well being a HTML its easy for me but what do people use the most? Is it HTML or PHP. It depends
    really on which you learnt first. So im gonna have a poll.Post HTML for Html and PHP for php and we
    will see which is more popular....
  15. How To Use Html In Flash - I need to import html into an iframe like box (1)
    As of this post I am trying to convert my current html website to a flash site. I use iframes in my
    html site and i am wondering if i can import html into a dynamic text box. I am wondering if i will
    need to redo the entire design in flash or if I can just import the html. If anything is confusing,
    please tell. Thanks in advance for the help /biggrin.gif" style="vertical-align:middle" emoid=":D"
    border="0" alt="biggrin.gif" />...
  16. Best Free Html/flash Templates - (13)
    Before I began creating websites myself, i used templates. The best selection of free ones is here:
    TemplatesBox . Tons of free HTML and Flash templates (although you need to pay for the nicer ones),
    I'm not affiliated with them I just thought there were alot of threads looking for templates and
    this is a great resource....
  17. Free Html Editor? - (37)
    Hi, I'm looking for a free HTML editor. Not one of those new-fangled WYSIWYG ones... the only
    conditions are.... * Its free * It has syntax highlighting * It has a preview mode (like on
    dreamweaver or whatever). * It doesn't have any spy/adware! I figured that theres probably
    a great program out there somewhere, I just havent found it yet. I used to use one that I liked but
    since the Great Hard Drive Crash of 2004 it has disappeared from my PC, and I can't find
    anywhere to download it either.... Which (free) editor do you use? - Vacant...
  18. Besides Html / Css - what kind of of coding do you use in your sites? (11)
    Hey guys I have a question for normal sites what kind of coding do you use besides the regular HTML,
    and CSS I realize for logins and forums etc.. php is common and the language of choice for database
    interaction but besides that for more cosmetic appealing appearence do you use anything else? Im
    asking this question cause im thinking of learning Java but im not sure how much that would help my
    web site design skills. And if thats not the one(in your opinon) to learn if I want to get better
    at website coding which one is? I have a shallow knowledge of VB and C++ but c...
  19. Tsw Webcoder - Anyone use this html editor? (3)
    Hi all, does anybody use the TSW webcoder? I ave been using this to do my site for about 3 months
    now and it's great. It's easy to use and just got alot of functions. Has anyone got te
    newest version of it? and wats new in the new version? Thanks...
  20. Tools For Photo Album With Html - (12)
    I'm new in web design and I want to build a web site where my family can view thumbnail pages
    with family photos and select the thumbnails to view the original photo. As a starter I thought
    about a couple of thumbnail pages to increment or decrement. I want to generate the pages with a
    free tool in order to review the html code for self learning purpose. Is this a good approach and is
    there any free tool suitable for this? Tillo...
  21. How To Create Html Feedback Form - (3)
    Hello... I want to know a very simple thing...For example i want to add a simple HTML form on my
    website ,.for example , something like User Name (text box) User Email: (text box) COmments (text
    box) Submit form Reset Form Just like that, so that when user click on UBMIT form, button, it
    takes all the inputs from the above boxes and sends to a pre defined email adress.and on submitting
    it shows a new thanks page. Well..there are some forms when you fll everything, they open ur default
    mailbox and then send etc etc i want that to send direct (i.e. without opening the ...
  22. Html Editor - Text editor or HTML Editor (7)
    What is a HTML editor? A HTML editor i basically a text editor but it has some advanced
    features.The important one being able to translate written text into HTML codes.Nowadays everyone
    uses HTML editors but still there are some guys doing it over a Text editor What do you fell about
    this At present iam creating a webpage with MS FP .It is user friendly and actually a very good
    tool for a beginner.Beginers look no further than FP.Also one cannot expect,atleast 99% ,a flop show
    from Microsoft.It has an integrated FTP. But all sites do not support FP extensi...
  23. Html Editor - some few questions.. (16)
    Hello, I have some few questions about a HTML Editor... :/ does it help make your site easier? what
    does it do? where can i get a good one, any recommendations? If it helps make your site easier then
    i need one /rolleyes.gif' border='0' style='vertical-align:middle' alt='rolleyes.gif' /> thanks
    for your help .. -Microsoft...
  24. Features Of Coffeecup Html Editor 2005 - (4)
    QUOTE The CoffeeCup HTML Editor is two editors in one. If you know HTML or want to learn more,
    use the code editor that has set the standard since 1996. To make pages fast or without knowing
    HTML, use the drag and drop WYSIWYG Visual Editor. You also can go back and forth between the Code
    and Visual Editors to create remarkable Web sites in a snap. CoffeeCup comes with more than 100
    DHTML and JavaScripts, a DHTML Menu wizard, 25,000 graphics and photos including more than 1,000 XP
    Style Icons, and access to more than 500 free Web site templates. It also has built-in...
  25. Html Code Problem - (7)
    Hi! Im working on a little HTML project for myself and am teaching myself.. So I would like to
    know how to set roll over effects and a short guide on it! What I mean is, I would like to know
    the code of making the mouse over on links make the links go red... Or if anyone can give me a way
    to do it on frontpage, because I cant find it! Thnx in advance!...
  26. Html Form Help Please - finding a form script (2)
    Does anyone know of any tutorial or can help me with making a form. I'll describe the situation
    and lets see if you can help me. Ok like a few form or text areas that when you put some text into
    them they'll input the words from the form area above into another form or textarea under it.
    Sort of like an online html maker where you can select your background colors, put an image url in a
    field, and some other options THAN you hit a button that says generate. and it generates a website
    html source code where you can copy the whole thing and paste into your own we...
  27. Form Mail (in Csv Or Straight Html) - All suggestions are welcome (0)
    I have been searching for scripts to write to do the followings: 1) validate that an applicant has
    all required field checked (this is to thin out who is serious and who's just browsing)--DONE 2)
    application page (applicant fills in all form fields)--DONE 3) mail the entire page content either
    in CSV or as is in HTML as attachment via email through web 4) "optional" confirmation page I was
    led to this site (http://www.email-form.com/) for a program called Form Processor Pro. This program
    will take care of the step 3 and 4 without saving to the server. My question...
  28. Html Editors That Work With Asp Php - wondering mind (7)
    Out of the many editors that i have seen not to many support asp or php those that do its more of
    converting so i was wonder which ones do have full blow asp and php capabilities on them?...
  29. Html/flash/swish Templates - (0)
    Just go here: http://comteche.com They have some good html, and flash/swish templates....
  30. Html Linking - (2)
    Probably a noobish question, But how do i get rid of the Underlining under text links...



Looking for formatting, php, html

Searching Video's for formatting, php, html
advertisement



Formatting Php With Html



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE