...

View Full Version : Basic question about PHP



natasha15
08-04-2007, 02:27 PM
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 :)

_Aerospace_Eng_
08-04-2007, 02:48 PM
In the end any outputted php is just html. Here is a real simple example

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

timgolding
08-04-2007, 02:50 PM
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 :)

natasha15
08-04-2007, 03:14 PM
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.... ;)

CFMaBiSmAd
08-04-2007, 03:45 PM
@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 -

AddType application/x-httpd-php .php
to -

AddType application/x-httpd-php .php .htmlStop 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.

whizard
08-04-2007, 04:19 PM
@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

guelphdad
08-04-2007, 04:40 PM
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.

natasha15
08-05-2007, 01:50 AM
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 :)

mlseim
08-05-2007, 02:07 AM
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/script/PHP-Clases/WebSafeColors-inc-php-20381.html

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

natasha15
08-06-2007, 12:58 AM
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?

mlseim
08-06-2007, 01:19 PM
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');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum