Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic question about PHP

    Hi there

    I have a simple question that I havent found the answer to yet...

    Okay, I know the format of PHP (start and end tags and the code in between has to be formatted a specific way), by looking at the tutorials here and on other sites.

    My question is:

    Does PHP code have to be in a separate file? ie code.php? And you insert something in the body of your .html document to call the php code (much like javascript? Or are there variations to this? And what are they?

    The reason I am asking, is because I keep on finding some good PHP code I want to try out, so that I can modify to suit my own purposes, but I dont know how to integrate it into my HTML... and if someone could tell me how this is supposed to be done, first principles, then that would be fab!!

    Thanks
    Natasha

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    In the end any outputted php is just html. Here is a real simple example
    PHP Code:
    <?php
    $name 
    'Aero';
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Untitled Document</title>
    </head>

    <body>
    <?php echo $name?>
    </body>
    </html>
    Its recommended that your page has a .php extension though its not necessary because you can always setup the server to parse .html files as php.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    Its recommended that your page has a .php extension though its not necessary because you can always setup the server to parse .html files as php.
    how can i do that
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahhhhhhhh!!!!!!!!!!!!!

    Thanks sooooooo much!!

    Now I get it.... I thought it was necessary to have the PHP in another file only, but thats great that I can put it at the start of the HTML page....

    So when I save the page with a .php extension, that means even though its a PHP page, the server parses the PHP, but does not display it on the web (when I upload the page)? Right?

    I am slowly starting to think that PHP is easier to understand (and learn) than Javascript....

  • #5
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,107
    Thanks
    2
    Thanked 326 Times in 318 Posts
    @timgolding -

    For an Apache web server, change the following line in your httpd.conf that tells the web server which file extension to parse through the php language engine, from -
    Code:
    AddType application/x-httpd-php .php
    to -
    Code:
    AddType application/x-httpd-php .php .html
    Stop and start your web server to get any changes made to httpd.conf to take effect.

    This can also be done in a .htaccess file.
    Last edited by CFMaBiSmAd; 08-04-2007 at 03:49 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    @natasha15

    PHP and Javascript are different things, and each does things the other cannot. PHP is code that the server parses before sending the page. After parsing it, the server sends the result of the code to the page. Often, code will do something like display one row of HTML for a logged in user, or display a different row of HTML for a guest. However, PHP code itself doesn't actually get sent to the browser. Javascript on the other hand, is embedded in the code sent to the client, and it's functions control the behavior of the page. (such as image rollovers, etc.)

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Quote Originally Posted by natasha15 View Post
    Now I get it.... I thought it was necessary to have the PHP in another file only, but thats great that I can put it at the start of the HTML page....
    you don't have to use it only at the beginning of your html page. you intersperse it throughout the page depending on what you want to do. you can open and close the php tags as many times, in as many different places in your html page that you would need.

  • #8
    New Coder
    Join Date
    Jul 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yeah I noticed (now) that you can add PHP to other parts of the page too... and I didnt realise that PHP (before) was used for different things to Javascript - which I had used in my coding.... but now I do... and the more I delve into PHP I see just how much it can do...

    I am starting to understand it, little by little... and its pretty cool, I must say... and I didnt think I would ever get any part of it, but its quite easy... the basic stuff... which I am just learning, the baby steps

    So another question.... part of this beginners stuff... helping me (and others too, I am sure)

    What are "classes" and how do you use them? I have seen them, but I dont know what to do with them....?

    Thanks
    Natasha
    Last edited by natasha15; 08-05-2007 at 01:52 AM.

  • #9
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    Consider the classes like little "subroutines" or functions ...
    You upload them into the same directory as your main script and
    at the beginning of your script, you have a line something like this:

    require_once('WebSafeColors.inc.php');

    Now, when you need to use the class, it is loaded and ready to go.
    In the example I gave, the class is used to find the closest safe color
    to a particular input.

    http://webscripts.softpedia.com/scri...php-20381.html

    Download the example script and the class to see how they work together.

  • #10
    New Coder
    Join Date
    Jul 2007
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ahaa. This is really interesting...

    So they are in effect, global functions that can be used again and again, without having to retype them in each page? Is that right?

  • #11
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,509
    Thanks
    8
    Thanked 1,090 Times in 1,081 Posts
    natasha,
    Yes ... that's a good way to look at it.

    If you put all of your classes in their own directory, and keep adding to them
    as you go along, you can reference them anytime you need them.

    Just make sure you adjust the path to your directory where your classes are located:
    require_once('/classes/WebSafeColors.inc.php');


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •