|
PHP & MYSQL TIPS The purpose of this page is to collect together some of the things I've learned whilst programming in PHP & MySQL over the last few years. I don't claim to be the fount of all knowledge, but some of these things have been of great help to me, and they might be to you too. If anyone disagrees with anything I have here, has anything they would like to include, or has any questions, you can email me at dan@dandylogic.com. 1. Single quotes vs double quotes Double quotes (") are inherently 'cleverer' than single quotes ('). 2. Arrays are GREAT A bit of a sweeping statement, but for those new to PHP (and most c-style languages), you may not realise how much more flexible arrays are than in some other languages. As a basic programmer, I was always used to arrays with numbers as keys - PHP comes with bags of great array handling functions. And making an array is wonderfully simple - there's a few ways to do it:
This will return an array with 4 values, numbered 0 to 3. The wondrous print_r function can be used to show the contents of an array (no matter how complex or multi layered).
will return:
You can assign new values to an array extremely simply - the simplest way is Each element in an array can be anything you want - an instance of a class, a variable, another array, whatever you need. Because of this, you can use them to store quite complicated data structures, for example:
would return:
Array
This is a GREAT HELP when debugging things - you get a one-shot view of your entire data structure, which can really help to orient yourself. Note that you need to view the source of the page to see it formatted nicely. You can loop through arrays really easily too - there are a few ways, but my favourite is:
This will return a list of all the variables a page receives in the querystring. Note also, that as $_POST and $_GET are just arrays, you can muck about with them all you want - adding values, changing values, assigning one to the other, or whatever. If you are unsure whether a page is going to have stuff sent to it using POST or GET, you can say:
You can then refer to $arrData in your page, and not worry about which array to get your data from. I could go on about arrays all day, but my fingers would fall off. The last tip is that you can convert an array to a string using the That's it for now - there's lots more to come, I'll update the page soon. |
| Monsterland is brought to you by dandylogic. It is still very much in it's infancy, so if you have any problems, or just want to say hello, drop us a line. We are very interested in your feedback. |