Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 9th, 2004, 1:08 PM   #1
bulio
Hobbyist Programmer
 
bulio's Avatar
 
Join Date: Jul 2004
Location: Location
Posts: 138
Rep Power: 5 bulio is on a distinguished road
Php Tutorial 2

Hi, if you are viewing this tutorial I am hoping that you have looked over tutorial #1 as it is essential that it is done, otherwise I think you will be completely lost.
~SyringeX


What's all the advanced stuff your going to teach me?
Well, I don't think we're quite done with The Basics yet, and we won't be starting MySQL until either lesson 3 or lesson 4. In this tutorial, you'll be learning things that will help you maintain an easier web site.

What will help me maintain an easier website?

Well, first of all, your variables will, we already covered those, and will be working with them a bit more in this tutorial. We are also going to be looking over includes, one of the best commands in php.

Alright, open up notepad and get ready. Let's take a look at the simple include statement. What the include statement does is include an external file from one of your folders. How would this help maintain an easier layout?
Well, let's say you had your navigation links table. You have about 50 pages, and you want to add a new link. You could add the link on each page, but that would be very tedious, this is where includes come in handy. By including a file named navigation.php that you've created, you can simply add the link in the navigation.php file, and all your pages with the included navigation.php page would then have that link! (navigation.php must be made manually, and should be constructed the same as your navigation table, without any other features)
Let's look at the include statement code:
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $admin = "SyringeX";
  4. echo '<h3>My Web Page...</h3>It is maintained by' . '$admin';
  5. include ('navigation.php'); /*This includes the navigation file onto the page.*/
  6.  
  7. ?>

Save it, open it up. Cool huh? If it didn't work, make sure you have a navigation.php file made with your nav table on it. You need to make sure it's in the same directory as the file with the include on it, otherwise you'll have to specify the folder it is inside. EX:
php Syntax (Toggle Plain Text)
  1. <?php include '/otherfolder/navigation.php'; ?>

Can I make everything else easier by creating a php layout?

YES. I highly recommend using includes. Things that often use includes are your navigation tables, tagboard and polls tables, headers and footers, and other optional information. Let's take a look at how we can construct this simple php layout.

php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. include 'headerfile.php';
  4. echo '
  5. <table><tr><td valign="top">include 'navigation.php</td>
  6. <td valign="top">inside this table is the main content</td><td valign="top">include ('extras.php');</td></tr></table>';
  7. echo '
  8. ';
  9. include 'footerfile.php';
  10.  

Make sure when you do this that you have the separate files that you want created on here. That pretty much does it for the include statement, so let's take a look at a new one.

Now we are going to look at the IF statement

The IF statement is also a very important part of php. It reads to see if a part of a script is equal to, less than, or greater than something else. Depending on the answer, it releases different information. Let's look at the basic construct of an IF statement:
php Syntax (Toggle Plain Text)
  1. <?php
  2.  
  3. $admin = "SyringeX"; /*Our variable we're using */
  4. if ($admin == "SyringeX") { /*The IF statement. Inside of the parentheses is what you are comparing, in this case, we are checking if the variable $admin is equal to SyringeX. If it is, everything after the bracer { (Make sure you have it!) will be what is displayed */
  5.  
  6. echo 'You have an awesome admin';
  7. } else { /*Remember to use the closing bracer }. Now, Else is what happens if our variable is not equal to SyringeX. Say you changed the variable value to "dogfood", then everything after the Else bracer { would be displayed. */
  8.  
  9. echo 'This must not be a very good admin then :\';
  10. }; /* Our closing bracer and our semicolon */
  11. ?>
  12.  

So basically, if whatever is inside the parentheses are true (also known as 1) all information from the first bracer to the closing bracer will be submitted, otherwise everything after the else bracer to the ending bracer will be submitted. Try playing around with the IF statement a bit and see what you can come up with. If you've seen the generators I've created, a big part of these are using IF statements.

You can also check to see if two variables are equal to eachother:
php Syntax (Toggle Plain Text)
  1. <?php
  2. $variable1 = "1";
  3. $variable2 = "2";
  4.  
  5. if ($variable1 < /*Less than */ $variable2 { /*Checks to see if the first variable is less in value than variable 2. In this case, 1 is less than 2 */
  6. echo '$variable1' . 'is less than' . '$variable2';
  7. } else {
  8. echo '$variable2' . 'is less than' . '$variable1';
  9. };
  10.  
  11. ?>

Cool isn't it? Now we are going to move on to the wonderful and delightful world of...

Functions!

Functions help save time, and a good ton of it. They allow you to store code that can be called by typing in the functions name followed by your parenthese ();.

Let's take a look at declaring, and then calling our function:
php Syntax (Toggle Plain Text)
  1. <?php
  2. function sometext() { /*Just like the IF statement with your bracers, anything inside is what the function containts. Sometext is the name of this function. */
  3.  
  4. echo 'This is the text that will be displayed by our function';
  5. }
  6. /* Now the function is over, now it's time to call our function, or else the text won't be displayed */
  7.  
  8. sometext(); /*Simply type the name with the parentheses and you've called the function! Anywhere where you type sometext() is where the text specified will be.*/
  9.  
  10. ?>

Pretty easy isn't it? Everything within the braces is the value of the function. It doesn't have to be just text, it can be variables with the your mysql user settings, cookies, sessions and so on. They come in handy for portal and game making. And to call it, all's you do is type the name of your function, followed by parentheses and semicolon, and you're done! This is pretty much it for lesson 2, soon we'll be hitting the advanced things, but look below for this lesson's Cool Script!

Cool Script!

This lesson's cool script is a hit counter. All's you have to do is create a file called hits.txt -- Insert the number 1 on it, and that is all. Then type in the following code, which will use a blend of some of the aspects we've talked about. It adds 1 everytime a page is opened to hits.txt, giving us a hit counter! Once you've created the actual hit counter file, include it by using the include statement (You ought to know how by now B) )
php Syntax (Toggle Plain Text)
  1. <?php
  2. if(!$open = fopen("hits.txt", "r")) {
  3. echo "Sorry, failed opening the file!";
  4. }
  5.  
  6. if($read = fread($open, 1000)) {
  7. $hits = $read + 1;
  8. echo "Page hits: $hits";
  9. } else {
  10. echo "Sorry, unable to read file!";
  11. }
  12.  
  13. if(!$open = fopen("hits.txt", "w")) {
  14. echo "Sorry, failed opening the file!";
  15. }
  16.  
  17. if(!$write = fwrite($open, "$hits")) {
  18. echo "Sorry, failed writing to the file!";
  19.  
  20. if(!$close = fclose($open)) {
  21. echo "Sorry, failed closing the file!";
  22. }
  23. ?>
As for now, that concludes lesson 2. Lesson 3 will contain arrays, methods, and more!

Created by Syringex

Last edited by big_k105; Nov 16th, 2007 at 9:27 AM.
bulio is offline   Reply With Quote
Old Jun 30th, 2005, 7:56 PM   #2
crawforddavid2006
Expert Programmer
 
crawforddavid2006's Avatar
 
Join Date: Apr 2005
Location: Not sure yet
Posts: 572
Rep Power: 0 crawforddavid2006 is an unknown quantity at this point
Send a message via AIM to crawforddavid2006 Send a message via MSN to crawforddavid2006
Exclamation I need help in the arrays turorial

Could someone help me figure out what is wrong. I tryed everything in the turorial and it did not work. :mad:
crawforddavid2006 is offline   Reply With Quote
Old Jul 1st, 2005, 7:30 AM   #3
Zephir
Newbie
 
Join Date: Jun 2005
Posts: 3
Rep Power: 0 Zephir is on a distinguished road
I was wondering if you could edit in which one is the first tutorial I figured them out. Just for future reference though.
Zephir is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Tutorial 3 bulio PHP 4 Jul 16th, 2006 4:00 AM
Assembly tutorial, part one. Mad_guy Software Design and Algorithms 21 Apr 15th, 2006 7:02 PM
Basic HTML Tutorial - Reuben Keeney ReubenK HTML / XHTML / CSS 14 Mar 26th, 2006 5:50 AM
Tutorial - Using MySQL in C# Darkhack C# 12 Jan 17th, 2006 9:28 AM
[tutorial] Simple G++ compiler tutorial coldDeath C++ 7 Nov 27th, 2005 12:33 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC