Eion
05-20-2008, 01:58 AM
This is my first tutorial, so any comments, suggestions or corrections are welcomed.
Start Here: A Beginner's Tutorial to Coding PHP
By Eion
First off, let me welcome you to the world of PHP. You will find in your learning that PHP is both powerful and flexible. I know from personal experience that PHP seems like a beast when you first start working with it, but remember this: all of the gurus around here at HostMatrix and elsewhere were once a novice like yourself, they all learned in their own ways and are now where they are because of the learning process you are about to begin. I am not going to be teaching you anything spectacular or even particularly useful in creating the codes you want, I have no codes that are ready for you to use or learn how to use, instead I hope to give you a basic understanding of PHP so that you can better conceptualize PHP so that you can better use the more basic and advanced codes for PHP. I hope that this tutorial helps get you used to PHP and feel comfortable enough to move on to others.
I: The Basics
The Alpha and the Omega
First things absolutely first, all php code begins with <?php and ends with ?> All of your code will lay between those two tags, if you are familiar with HTML, it is analogus to the two tags that HTML code sits inside. When I begin writing a new page of code, I always start off by putting these two tags in first and foremost. I put <?php at the top of the page and then hit Enter a few times to give some room and then I put ?> there. This way I can begin writing my code right there within those two and I dont have to worry about whether or not I did this process since it is the first thing I do. All scripts which will be used as php scripts need this section to come first, afterward you can add all the HTML you need in order to make the page look good.
Double Slash Notes
This is a useful thing about PHP codes which you will probably use quite a bit, and they are certainly used widely in tutorials. Anything behind a double slash (//) in PHP will be ignored, so anything you place behind it, so long as it is on the same line as the double slash will not effect the code or execution of the code. This is useful for tutorial writers to explain the code within the code. I've used the double slashes a million times while trying a new line of code, I just add // to the beginning of an old line of code I want to be excluded to test the new line of code. It also is useful for yourself if you are in the middle of writing a piece of code and you want to lay the format out first and go back and write the code in later and want to leave a note for yourself as a reminder to where the code needs to be written in, an example of this may be the following:
<?php
if($thistutorial == helpful)
{
//Leave Eion some reputation!
}
else
{
//Use the links at the bottom to see if they help!
}
?>
Variables
Variables are extremely important in all PHP codes, not only do they make information from the user (from forms) and from the site (from the database) available, but they often can clean up and make your code much more manageable. Learning the proper way to use Variables will be very important, but after you get used to them it will become second nature.
Unique and Descriptive
Make sure that your Variables are both Unique and Descriptive, when you are using a lot of variables over a large number of pages and over several groupings of code, you need your variables to have unique names so it doesn't mess up the code, and you should have them be descriptive so that you dont forget what they do or which variable goes to what. Stay away from variables that are named generic like "$variable1" or even ones like "$formdata" because over time those will get mixed in with many others and could potentially mess with you and your code. Instead name them something specific, "$username" or "$registerformname" which would help you remember what the code does and where it is supposed to be pointing.
Consistency
Now that you have named your variables uniquely and descriptively, you need to stick with that name. Make sure that the name doesnt change from one page to another or from one line of code to another, because it will immediately make your code unusable.
Brackets
Brackets are often one of the most misunderstood tools for PHP beginners, I will admit that I had significant problems remembering to include them. Brackets form a wide range of uses, but one of the most important of them is brackets like these:
{ and }
These brackets are used to separate parts of code that will be run by functions or other parts of the code.
Brackets are also necessary if you will have more than one line of code being run by a function. This also allows you to have multiple "layers" to your codes like below. You'll find that you will use them a lot with the IF THEN ELSE function, again, like below.
<?php
if($thistutorial == helpful)
{
leave Eion some reputation!
if($numberofrepleft > 100)
{
//Give Eion a medal!
mysql_query("Update members SET `medal` + 1 WHERE username=Eion")
}
else
{
//Give Eion a pat on the back.
mysql_query("Update members SET `patontheback` + 1 WHERE username=Eion")
}
else
{
//use the links at the bottom to see if they help!
}
?>
Each bracket, you will notice, has a mate. Each one that you put down needs another one put on the other side of the code so that everything works properly. If you get an error that says "Unexpected End" you should check to make sure all of your brackets are properly placed and mated. You will get the hang of brackets soon enough, and like all other things in PHP, they will become second nature to you.
II: MySQL and PHP
Grasping what MySQL was and how to properly use it was undoubtedly the most difficult process for me when I was first starting PHP, and I must admit that even at the time of this writing I am still perplexed at how to take full advantage of this relationship between MySQL and PHP.
The Workhorse and the Warehouse
The way I visualize the relationship between PHP and MySQL is thusly, PHP is like the workhorse, the majority of the work is done by the PHP codes, whereas the information is stored in MySQL, much like a warehouse. Think of MySQL as nothing more than where you will store the information for your site. The PHP will both bring the data from the site/user to the database, as well as bring the data from the database to the site/user. The PHP will also regulate the data and change the data in the database.
Think of the PHP code as going into MySQL and changing it, MySQL itself wont do any data editing, this is something that I didnt actually understand when I first started using PHP. I thought that PHP would put the data into MySQL and then from MySQL I could have the information changed without using PHP. Again, this is why I see MySQL as being a warehouse, just assume that it has walls and a roof, a place for data to be secure and neatly stored. PHP is the workhorse that gets the data, rearrages it or changes it and then puts it back again, deletes it or brings it out to the site/user.
PHP and MySQL databases go together because they both fulfill what the other cannot do, and once you get your head around how the two work independently and most importantly together, you will be well on your way to grasping the reigns of the true strength of PHP.
III: Useful Links
There is so much out here on the web that can help you in your learning of PHP that it really can be overwhelming. My advice to you is to start small, take each step at a time and go at your own pace. I've included here some of the sites that have over my (continuing) learning of PHP have really been a lot of help to me. Some of the concepts may still be confusing to you, but the more you learn the more opens up to you. I hope that you find the following links as useful as I did and still do.
Learn HTML
(http://htmlgoodies.com/)
HTML is in some ways a foundation for PHP, HTML makes your PHP forms and pages look the way they do, and learning how to code HTML will open up alot of common learning pathways that you can apply to learning PHP. I started with HTML and it made PHP easier to understand, so I recommend it as a first order to read up on it and look into it. I started learning HTML from HTMLgoodies, but there are plenty of other sites that can help to learn HTML too, if HTMLgoodies just isnt working for you.
Beginner's Links
(http://forums.hostmatrix.org/showthread.php?t=5427)
This is a good thread here at HM that links to other sites and tutorials that you may find helpful in your quest to understand PHP. Those links can also lead to more advanced things on the individual sites as well, so its always a good idea to check it out and take a look around, you'd be amazed at what you can learn just from surfing and searching those sorts of links.
User System by mck9235
(http://forums.hostmatrix.org/showthread.php?t=3681)
(http://forums.hostmatrix.org/showthread.php?t=4371)
This is a fantastic HM tutorial to wet your feet in the world of PHP, I've recommended it a bunch of times already and it was undoubtedly one of the best PHP tutorials available on the net. It will teach you the basics, and walking yourself through the process of creating a User System will help you gain a much better understanding of how to write your own codes.
PHP Gaming by Shwaza
(http://forums.hostmatrix.org/showthread.php?t=3967)
(http://forums.hostmatrix.org/showthread.php?t=5090)
(http://forums.hostmatrix.org/showthread.php?t=5505)
(http://forums.hostmatrix.org/showthread.php?t=9427)
Even if you arent going to be creating a PHP game as this tutorial can help with, many of the things you can learn from this tutorial can help you in other PHP codeworks. Shwaza does a good job moving along piece by piece, and it shows you the beginnings of how dynamic you can make your PHP codes.
PHP Functions Forum
(http://forums.hostmatrix.org/forumdisplay.php?f=263)
This is a great forum here on HM that is dedicated to PHP Functions. It can help you get a better grasp of whats truly possible in PHP code.
Acknowledgments
Firstly, I want to say thank you to HostMatrix.org and all of the moderators and administrators that have gone with it over the years. I want to say a specific thank you to Sage Tech for continuing to support this community and the amazing opportunities that it offers.
Secondly, I want to thank mck9235, Shwaza, kendall14, BigMike and quantumstate for not only providing good tutorials and threads here, but also for answering questions, providing codes and explaining PHP to me during my own personal journey of learning PHP. If I forgot anyone, know that you too are appreciated.
Finally, I want to thank the readers of this tutorial, both novice and guru. I hope that I have provided you with some insight into the world of PHP that will help you code your own codeworks and move closer to what you are looking to do with them. I thank all the comments, suggestions, corrections and reputation left here as well.
~Eion
Start Here: A Beginner's Tutorial to Coding PHP
By Eion
First off, let me welcome you to the world of PHP. You will find in your learning that PHP is both powerful and flexible. I know from personal experience that PHP seems like a beast when you first start working with it, but remember this: all of the gurus around here at HostMatrix and elsewhere were once a novice like yourself, they all learned in their own ways and are now where they are because of the learning process you are about to begin. I am not going to be teaching you anything spectacular or even particularly useful in creating the codes you want, I have no codes that are ready for you to use or learn how to use, instead I hope to give you a basic understanding of PHP so that you can better conceptualize PHP so that you can better use the more basic and advanced codes for PHP. I hope that this tutorial helps get you used to PHP and feel comfortable enough to move on to others.
I: The Basics
The Alpha and the Omega
First things absolutely first, all php code begins with <?php and ends with ?> All of your code will lay between those two tags, if you are familiar with HTML, it is analogus to the two tags that HTML code sits inside. When I begin writing a new page of code, I always start off by putting these two tags in first and foremost. I put <?php at the top of the page and then hit Enter a few times to give some room and then I put ?> there. This way I can begin writing my code right there within those two and I dont have to worry about whether or not I did this process since it is the first thing I do. All scripts which will be used as php scripts need this section to come first, afterward you can add all the HTML you need in order to make the page look good.
Double Slash Notes
This is a useful thing about PHP codes which you will probably use quite a bit, and they are certainly used widely in tutorials. Anything behind a double slash (//) in PHP will be ignored, so anything you place behind it, so long as it is on the same line as the double slash will not effect the code or execution of the code. This is useful for tutorial writers to explain the code within the code. I've used the double slashes a million times while trying a new line of code, I just add // to the beginning of an old line of code I want to be excluded to test the new line of code. It also is useful for yourself if you are in the middle of writing a piece of code and you want to lay the format out first and go back and write the code in later and want to leave a note for yourself as a reminder to where the code needs to be written in, an example of this may be the following:
<?php
if($thistutorial == helpful)
{
//Leave Eion some reputation!
}
else
{
//Use the links at the bottom to see if they help!
}
?>
Variables
Variables are extremely important in all PHP codes, not only do they make information from the user (from forms) and from the site (from the database) available, but they often can clean up and make your code much more manageable. Learning the proper way to use Variables will be very important, but after you get used to them it will become second nature.
Unique and Descriptive
Make sure that your Variables are both Unique and Descriptive, when you are using a lot of variables over a large number of pages and over several groupings of code, you need your variables to have unique names so it doesn't mess up the code, and you should have them be descriptive so that you dont forget what they do or which variable goes to what. Stay away from variables that are named generic like "$variable1" or even ones like "$formdata" because over time those will get mixed in with many others and could potentially mess with you and your code. Instead name them something specific, "$username" or "$registerformname" which would help you remember what the code does and where it is supposed to be pointing.
Consistency
Now that you have named your variables uniquely and descriptively, you need to stick with that name. Make sure that the name doesnt change from one page to another or from one line of code to another, because it will immediately make your code unusable.
Brackets
Brackets are often one of the most misunderstood tools for PHP beginners, I will admit that I had significant problems remembering to include them. Brackets form a wide range of uses, but one of the most important of them is brackets like these:
{ and }
These brackets are used to separate parts of code that will be run by functions or other parts of the code.
Brackets are also necessary if you will have more than one line of code being run by a function. This also allows you to have multiple "layers" to your codes like below. You'll find that you will use them a lot with the IF THEN ELSE function, again, like below.
<?php
if($thistutorial == helpful)
{
leave Eion some reputation!
if($numberofrepleft > 100)
{
//Give Eion a medal!
mysql_query("Update members SET `medal` + 1 WHERE username=Eion")
}
else
{
//Give Eion a pat on the back.
mysql_query("Update members SET `patontheback` + 1 WHERE username=Eion")
}
else
{
//use the links at the bottom to see if they help!
}
?>
Each bracket, you will notice, has a mate. Each one that you put down needs another one put on the other side of the code so that everything works properly. If you get an error that says "Unexpected End" you should check to make sure all of your brackets are properly placed and mated. You will get the hang of brackets soon enough, and like all other things in PHP, they will become second nature to you.
II: MySQL and PHP
Grasping what MySQL was and how to properly use it was undoubtedly the most difficult process for me when I was first starting PHP, and I must admit that even at the time of this writing I am still perplexed at how to take full advantage of this relationship between MySQL and PHP.
The Workhorse and the Warehouse
The way I visualize the relationship between PHP and MySQL is thusly, PHP is like the workhorse, the majority of the work is done by the PHP codes, whereas the information is stored in MySQL, much like a warehouse. Think of MySQL as nothing more than where you will store the information for your site. The PHP will both bring the data from the site/user to the database, as well as bring the data from the database to the site/user. The PHP will also regulate the data and change the data in the database.
Think of the PHP code as going into MySQL and changing it, MySQL itself wont do any data editing, this is something that I didnt actually understand when I first started using PHP. I thought that PHP would put the data into MySQL and then from MySQL I could have the information changed without using PHP. Again, this is why I see MySQL as being a warehouse, just assume that it has walls and a roof, a place for data to be secure and neatly stored. PHP is the workhorse that gets the data, rearrages it or changes it and then puts it back again, deletes it or brings it out to the site/user.
PHP and MySQL databases go together because they both fulfill what the other cannot do, and once you get your head around how the two work independently and most importantly together, you will be well on your way to grasping the reigns of the true strength of PHP.
III: Useful Links
There is so much out here on the web that can help you in your learning of PHP that it really can be overwhelming. My advice to you is to start small, take each step at a time and go at your own pace. I've included here some of the sites that have over my (continuing) learning of PHP have really been a lot of help to me. Some of the concepts may still be confusing to you, but the more you learn the more opens up to you. I hope that you find the following links as useful as I did and still do.
Learn HTML
(http://htmlgoodies.com/)
HTML is in some ways a foundation for PHP, HTML makes your PHP forms and pages look the way they do, and learning how to code HTML will open up alot of common learning pathways that you can apply to learning PHP. I started with HTML and it made PHP easier to understand, so I recommend it as a first order to read up on it and look into it. I started learning HTML from HTMLgoodies, but there are plenty of other sites that can help to learn HTML too, if HTMLgoodies just isnt working for you.
Beginner's Links
(http://forums.hostmatrix.org/showthread.php?t=5427)
This is a good thread here at HM that links to other sites and tutorials that you may find helpful in your quest to understand PHP. Those links can also lead to more advanced things on the individual sites as well, so its always a good idea to check it out and take a look around, you'd be amazed at what you can learn just from surfing and searching those sorts of links.
User System by mck9235
(http://forums.hostmatrix.org/showthread.php?t=3681)
(http://forums.hostmatrix.org/showthread.php?t=4371)
This is a fantastic HM tutorial to wet your feet in the world of PHP, I've recommended it a bunch of times already and it was undoubtedly one of the best PHP tutorials available on the net. It will teach you the basics, and walking yourself through the process of creating a User System will help you gain a much better understanding of how to write your own codes.
PHP Gaming by Shwaza
(http://forums.hostmatrix.org/showthread.php?t=3967)
(http://forums.hostmatrix.org/showthread.php?t=5090)
(http://forums.hostmatrix.org/showthread.php?t=5505)
(http://forums.hostmatrix.org/showthread.php?t=9427)
Even if you arent going to be creating a PHP game as this tutorial can help with, many of the things you can learn from this tutorial can help you in other PHP codeworks. Shwaza does a good job moving along piece by piece, and it shows you the beginnings of how dynamic you can make your PHP codes.
PHP Functions Forum
(http://forums.hostmatrix.org/forumdisplay.php?f=263)
This is a great forum here on HM that is dedicated to PHP Functions. It can help you get a better grasp of whats truly possible in PHP code.
Acknowledgments
Firstly, I want to say thank you to HostMatrix.org and all of the moderators and administrators that have gone with it over the years. I want to say a specific thank you to Sage Tech for continuing to support this community and the amazing opportunities that it offers.
Secondly, I want to thank mck9235, Shwaza, kendall14, BigMike and quantumstate for not only providing good tutorials and threads here, but also for answering questions, providing codes and explaining PHP to me during my own personal journey of learning PHP. If I forgot anyone, know that you too are appreciated.
Finally, I want to thank the readers of this tutorial, both novice and guru. I hope that I have provided you with some insight into the world of PHP that will help you code your own codeworks and move closer to what you are looking to do with them. I thank all the comments, suggestions, corrections and reputation left here as well.
~Eion