PHP Tutorial - Code Organization
This tutorial aims to show you some tips and tricks showing how to better organize your code.
Indentation
| First of all - indent your code - this is crucial when writing medium/large programs, although when I started I tended to ignore this a bit, now I find it indispensable - this is how to indent your code:
if ($foo = ‘ bar ‘) Now as you can see, you indent the code which is inside a control structure (an ‘if’ or ’select/switch’). |
Comments
| When writing your code USE comments, they are SO simple to implement, you just need 2 forward slashes at the start of the line, or /* */ for multiple line comments (as seen below). // News script - property of Infinite-fire.net Start code here… /* In this section of my code I open the database using a pre-defined connection, then extract the recent news entries, and print the 3 most recent entries in order of the date they were submitted */ As you can see, multi-line comments can be used for longer descriptions, and single-line comments for shorter comments - the more of these you use, the easier it will be to update the code at a later date, also the easier it will be for others to use your code if it is in a large system that many people work on. |
Brackets in control structures
| Now there are 2 primary ways of organizing your brackets, I will show my favorite first, then the other method. I prefer to put the if, or select condition, then a line break, then the code (indented) and then close the brackets on a new line, vertically parallel to the opening bracket - this example should show this better:
if ($foo = ‘ bar ‘) As you can see, it is very clear where the brackets open and close, but here is an even better example of why this is (in my opinion) the most readable method. As you can see, it is very clear where the brackets open and close, but here is an even better example of why this is (in my opinion) the most readable method. if ($foo = ‘ bar ‘) With the nested if statements we can see exactly where the various if brackets start and end. I will also show the other method of organizing control structures, which is fairly popular: if ($foo = ‘ bar ‘) { This method takes up less lines, but in my opinion makes the code a bit harder to read - choose the method that is easiest to you! |
Select / Switch
| This is the alternative to using a lot of if’s in every (that I have seen) programming language - in PHP it is the switch construct, similar to the if construct, this is how to use it:
switch ($i) You must (pretty much) ALMOST use the default option, this is true if none of the previous options are true, and so it prevents errors. |
MySQL_error()
I CANNOT stress how useful this function is, it is used to get a response about the error that you have written into a mysql function, here is how it works:
mysql_connect (’ locakhost ‘,’ username ‘,’ password ‘) or die ( mysql_error ());
– this would return the error: ‘Unknown MySQL Server Host ‘locakhost’ (11001) ‘ - now this instantly shows you that you have mis-spelt localhost.
This function is also useful on queries, simply use:
$result = mysql_query ( ‘Your query here ‘) or die ( mysql_error ());
Now if there are any problems with your query they are displayed, a very common problem is not using quotation marks properly, for example…
$result = mysql_query (’ select id from tablename where name = ‘.$ _SESSION [ 'name' ].”) or die ( mysql_error ());
Will return this error:
You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near ” at line 1
This is how the query should be written: $result = mysql_query (’ select id from tablename where name =’ ‘.$ _SESSION [ 'name' ].”’) or die ( mysql_error ());
Notice the ‘ marks around the variable, this brings me to my final point in this tutorial…
Superglobals
Sounds pretty sweet doesn’t it - superglobals, but what does it mean? Well the superglobal arrays are $_GET, $_POST, $_SESSION, $_COOKIE, $_ENV, $_FILES, $_REQUEST and $_SERVER - and they describe the data they hold, for example to access the variable $foo in a cookie, you would use $_COOKIE['foo'], similarly to access a variable passed in the url called $bar, you would use $_GET['bar'] - the $_ENV contains environmental variables, $_FILES contains variables uploaded via HTTP post file uploads, $_POST contains form submitted variables where the method of the form was ‘post’.
The $_SERVER array contains variables with values concerning the server (ip address and such like), $_SESSION is used to access session variables, and finally, $_REQUEST is used to access variables provided to the script via the GET, POST, and COOKIE input mechanisms, and which therefore cannot be trusted.
You MUST, as of PHP4 (I think) use these arrays, previously you could just use $foo to access $_GET['foo'] - now you can’t, and PHP is much better for this change, and also much more secure - if you feel you NEED to bypass this update, you can turn on global variables in your php.ini file
Conclusion
Well, this tutorial is longer than I had planned, and I didn’t fit in everything I had planned (functions and a few other things were omitted so it wasn’t novel-length) but maybe next time eh?