Total noob -- So I learned the basic functions of php...

So I learned some basic stuffs about php and want to test it out. I mean basic stuffs till if() {result}.

I also have a standard website based on a pure .html template.

So my questions are:

  1. What do I need on my server to start using php functions? Do I need to download the whole pack on php.net and put it in the root folder?
  2. If my webpage exists of the following menus: index.html / about.html / contact.html / links.html / games.html / products.html etc…
    So when I use a $function in it, how do I do it? I want to put only the $function one every .html file but able to change the value of $function in an instant.

Bottom line: I don’t want to open all my .html files to change the $function. I want the $function linked to a special php “definitions” list where I can edit/update them.

Okay, I’m going to give your questions a shot…hopefully this helps:

What you need on your server:

You need to have PHP installed on your server. If you have web hosting through any of the common providers, that’s probably already taken care of for you. You should be all set to start using PHP right away.

The trick is that in order for PHP to parse your files, they need to have the .php extension. Practically speaking, there’s nothing wrong with having a .php file that contains only HTML code, so my advice is to rename all of your .html files to the .php extension. That way, any PHP code found within them will be properly executed.

As to your question about linking a function, this is accomplished by way of the PHP include() or require() function. These functions accomplish basically the same thing with the exception that require() will cause a failure if it’s unable to find the file you wish to include.

Let’s say you have a file called index.php. Inside of that you have the following PHP code:

[php]

<?php $result = my_special_function(); ?>

[/php]

As you can see, I haven’t defined my_special_function() anywhere inside of index.php, so when I try to run this code, it’s going to break. I have two options for making index.php aware of my_special_function(). I can either include the function inside of index.php (which is basically what you’re doing), or, I can put it inside of a different file and simply include that file inside of index.php:

Let’s say I have another file, functions.php:

[php]

<?php function my_special_function(){ return "This is what I do"; } ?>

[/php]

I define my_special_function() inside of that file, and I want to be able to use that definition in my index.php. I simply do this:

[php]

<?php include('functions.php'); $result = my_special_function(); echo $result; ?>

[/php]

The above code will work. Just keep in mind that the argument to the include() or require() methods is the path to the php file that contains the functions/html/information you need.

You can check out more info on include() and require() here:

http://php.net/manual/en/function.include.php
http://www.php.net/manual/en/function.require.php

This probably won’t answer most of your questions, but you can have a header file. For example, my localhost website has a header file. In my header file I have my links and my login form. However, if I didn’t use this file and I changed one of my links, it wouldn’t be the same for all of my files and I would have to keep changing every page. This is how I would do it:

[php]

<?php include("header.php"); ?>

content . . .

[/php]

If you have any problems trying to install php, please PM me. You may also want the MySQL, or you could probably get XXAMP or WAMP. (they install it all for you. ;))

Sponsor our Newsletter | Privacy Policy | Terms of Service