1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

I need something basic explained about PHP

Discussion in 'PHP' started by tayiper, Nov 3, 2007.

  1. #1
    Hey all, as a sort of a continuing of the relatively recent A contact form that doesn't use an e-mail client and A big PHP file-type related confusion!! threads here on DP forums, I want to ask you all something very basic about PHP ...


    I am interested in the difference between a common HTML file and a PHP file (that's are identical), meaning that I always thought that one should use "<?php print "content"; ?>" or "<?php echo "content"; ?>" to display HTML content if using a file (or in a file) with .php extension.


    As you can see I tried uploading the http://tadejpersic.awardspace.com/index.php PHP file (that's as mentioned identical to the http://tadejpersic.awardspace.com/index.html HTML file, meaning that there's no PHP code in it) to the Awardspace server that supports PHP, and to my surprpise it was displayed correctly, i.e. displayed correctly without using the Print or Echo statements or any other changes made.


    For example, on the Learn PHP - PHP Tutorial - Print Echo page on "About.com" it explains how to use these statements in a document. But as mentioned, I just copied "index.html" file (or rather, just changed its extension) to "index.php", and uploaded it, and it's displayed identically as the first one.


    Oh and I am also curious, is there any benefit in doing so (chaning all my .html documents to .php ones)??!


    tayiper
     
    tayiper, Nov 3, 2007 IP
  2. tamen

    tamen Peon

    Messages:
    182
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Only code inside <?php ?> tags will be parsed as PHP. Any code outside those tags will be send to the browser without being run.

    Example:
    
    Some HTML <?php echo 'Some PHP'; ?>
    
    Code (markup):
    Will display: Some HTML Some PHP

    Whereas this:
    
    Some HTML echo 'Some PHP';
    
    Code (markup):
    Will display:
    Some HTML echo 'Some PHP';

    In the last example, the PHP command, echo, is not parsed as PHP and send straight to the browser.

    With regards to changing the file names. Out of the box, apache will not parse PHP-code in .html files. It is possible, though, to set up Apache to parse .html file in the same way as .php files.
     
    tamen, Nov 3, 2007 IP
    tayiper likes this.
  3. tayiper

    tayiper Active Member

    Messages:
    421
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #3
    Oh well and to clarify my confusion a bit. Basically I was ust interested if one can simply change the extension of a normal HTML file to .php (if the server supports PHP; or maybe this even doesn't matter since there's no PHP code in it as in my example above), and the file/document would be still normally displayed??!


    cheers, tayiper
     
    tayiper, Nov 4, 2007 IP
  4. YIAM

    YIAM Notable Member

    Messages:
    2,480
    Likes Received:
    240
    Best Answers:
    0
    Trophy Points:
    280
    #4
    yes you can.
     
    YIAM, Nov 4, 2007 IP
    tayiper likes this.
  5. phper

    phper Active Member

    Messages:
    247
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    60
    #5
    If there's no PHP code in it at all, it doesn't matter. You can just keep it as .html.

    But if you have other PHP scripts on the same site, you may want to consider making all of them end with .php, just to keep them consistent and better looking.


    If you are ready to be confused a bit more, PHP files are set to end with .php simply as a convention. The decision of what file extension(s) are parsed as a PHP file is configured on the web server. You could configure your web server to parse files ending with .html as PHP files. You could also change the extension to .xyz, .nnn .abc, etc.
     
    phper, Nov 4, 2007 IP
    tayiper likes this.
  6. panama

    panama Peon

    Messages:
    630
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    The basic definition for the use of php scripting is to generate HTML. If apache sees the .php extension on a file that is about to be served to a user (or as phper correctly stated - any extension configured to be parsed for php), apache runs the file through the php engine first so that any scripting can be parsed and the output included in the resulting page served to the user.

    So, if you have an .html file and you change the extension to php, the only thing that will happen is that apache will run the page through the php engine to check for scripting before serving the page. As a general rule of thumb, unless you are running a site with a LOT of traffic (or a tiny CPU and puny memory) this shouldn't be an issue.
     
    panama, Nov 5, 2007 IP
  7. tayiper

    tayiper Active Member

    Messages:
    421
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    78
    #7
    So to repeat that question from the original post: is there any sense/advantage to change the extensions from all the pages/documents from .html to .php??!


    /EDIT: Oh never mind, I see that phper already anywered to this question ...

    tayiper
     
    tayiper, Nov 5, 2007 IP
  8. panama

    panama Peon

    Messages:
    630
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I use php a LOT in the sites I build, so I always use the .php extension.

    The only disadvantage I can see is if you are expecting a LOT of traffic, need a LOT of scripting for each page, AND only have cash for a shared hosting account (or a server with limited resources!!) :)

    Take some open-source php-based CMS/blog software as an example. Wordpress, Joomla, Pligg - none of these have any static .html pages (that I am aware of), and there are some fairly large sites ran on them.

    So, IMHO, use the .php extension. I have lost count of the times i have had a .php/.html mix, and I wanted to implement a sitewide php script, and wished that I'd used .php for all pages.
     
    panama, Nov 5, 2007 IP