View Single Post
Old 09-02-2005   #2
sarettah
Stay Out Of The Box
Want to see your own Advertising Here!
 
sarettah's Avatar
 
Join Date: Oct 2002
Location: Lost in the ozone
Posts: 10,318
Default Re: Group project Step 3 - putting it into a web page

I you noticed. In the two places we used an "include" file, right where we put the include line was where the remot thumbs file was inserted. That is the way an include file works. Whether it is an ssi include, a php include or an asp include, where it is called is where it appears in the code.

Now, the php_parse also appeared where we put it, but if the script could not find the template file it instead showed a message that the file could not be found.

The advantage of parsing the file through a script instead of a simple include statement is that you can apply logic to your output. In this case the logic is "If you can find the file show it otherwise show an error message". This is something you cannot pull off in a straight html page or a straight include statement.

Because I truly believe that a webmaster should never run code (html, style, or program code) that they do not understand 100% I am going to run through the parse routine step by step and explain what we are doing:



All php programs start with
That tells the program where to start translating the php statements and where to stop translating them.

$filename = "your_template_folder/your_template_filename";

$filename is a variable name, a storage area on the server to store a piece of data. In php, pretty much all variable names start with a dollar sign ($)

By stating that $filename= we are placing whatever is on the right hand side of the equal sign into the storage area that we called $filename. In this case, we are storing a set of character data (called a string). We designate that it is character data by using quotes at the beginning and end of the string.

The semi colon at the end of the line tells php that that is the end of the statement.

if (file_exists($filename)) {

The if statement is the only true logic in a program. It is the decision maker. it is structured as:

If (condition) {do this }

The "condition" is something that returns either a true or false.
The { starts the block to be performed if condition is true and the } states that the block ends. Inside the block we can run whatever code we see as necessary.

So, in this case our condition is (file_exists($filename)) . The file_exists looks for a file by the name passed to it (in this case whatever we have stored in $filename) and returns a true if itr is found and a false if it is not found.

If the file we are looking for is there, we are returned a "true" and we start processing the commands inside our block (inside the { and })

$fd = fopen ($filename, "r");

We create a variable called $fd and store into it the results of an fopen command.

The fopen command tries to open a file. If it opens it succesfully then it returns a "file handle" (basically a channel between the program and the file that was opened) The fopen uses 2 pieces of data that are passed into it:

fopen(filename, rights) where filename is the name of the file to open and rights define the permissions that the file is opened with. In this case we are passing in an "r" as the rights. This tells fopen to open the file with "read only" permission.

while (!feof ($fd)) {

The while command starts and then processes a "loop". A loop is a set of statements that are processed over and over (repetitive) with usually some kind of control to tell the program when to stop repeating.

while (condition) { do this..... }

The condition, just like the condition in the "if" statement returns either a "true" or a "false" . In this case, our condition is (!feof($fd))

The "!" means NOT. the "feof" means end of file and the ($fd) is the file channel we opened with our fopen.

So the entire statement can be read as : do this while we have not reached the end of file mark of our input file.

$buffer = fgets($fd);

This creates a variable called $buffer and stores into it whatever the "fgets" statement pulls from the file we have open.

The fgets grabs the next bunch of characters up until it hits an "end of line" mark of some sort.

echo $buffer;

Echo tells the program to write whatever is in $buffer to the browser (standard output)

}

This closes our while loop

fclose ($fd);

fclose closes our input file

}

this closes our "if" statement

else

The else clause of the "if" statement tells the program to run alternate code if the condition inside the if statement returns false.

{

This starts the block to be performed in the else sequence

echo "Could not find " . $filename;

This prints to the browser a message saying "Could not find " with the filename we wanted at the end.

the "." operator in php is the "string concatenation" operator. Which mens simply, "slap those two strings together alongside each other".

ie: If I wrote something like echo "FOO" . "BAR"; I would expect to see FOOBAR as the output.

}

This closes the else clause

?>

This tells the server to stop processing php directives.
__________________


Free Adult Webcams
sarettah is offline   Reply With Quote