This thread is dedicated to all the tips and trick to ease the usage of PHP/MySQl.
Feel free to list or ask about any Trick you use or want to know.
This function great to debug large arrays. It can be used for string or other variables too, but its real handy with arrays. in every project i always make a function then include that file(which includes that function) in every file. it makes it real easy to debug problems.
The normal output from this function is really cluttery and is hard to figure things out. But, when used with <pre></pre> tags, its realy useful.
Will list more functions as i remember them.
Also point out any errors that i migh thave made.
Reading from a file
Ther are many methods of reading from a file. it depends what you want to do.
1) file('filename'). it reads the whole file and return an array with each line as new index in array
2) file_get_contents('filename'). it returns the whole file.
the above 2 are useful if you just wana read the file. it saves you from the hassle of opening, reading and closing files.
also, its always a good idea to check if the file exists, before doing anything with it. it just saves you from making errors. so you do something like
Writing to a file
file-put_contents('filename','data') will write the 'data' to the file. This again saves fromthe hassle of opening, writing and closing the file.
You can use this feature to tackle with the MySQL errors.
Why this is handy?
Suppose, you have to insert a new user in database and before inserting you have to check, if that username already exists or not. To check this, first you have to run a a select statement to see if MySQL returns a record with that username. If records is returned it means user already exists. If not then we can safely insert the record in database.
Now it needs to access database twice, which takes up resources (especially on large scale website).
So, now we'll use our new solution. For this new solution to work, the username field should be set to unique, it means, no other user can have the same username.
Then we run our insert statement. If the username already exists, then it won't let the query run and will generate an error, which will say "Duplicate entry 'username' for key 1". and the error number will be 1062;
now we can say
Where its useful?
Like the example i shown you. You can store an encrypting function the nuse it alter. Then suppose somebody wants you to change all the encoding functions throughout the site
So, if you can have a variable in which you can store function name and then store it in your config file. Then whenever you want to change it just change it in your config file and it'll change everywhere globally.
there are 2 types of functions for matching regular expressions
ereg, and preg_match
ereg, is PHP's regular expression matching function (as far as i know, can be wrong)
and preg_match is based on perl regular expression matching.
Both consume time, so try to use other this like substr, strstr if they can be applicable in your situation, but if its must to use regular expression then use
its twice as much faster than ereg.
theres nothing which you can't do in both, just speed difference is a lot, and it counts in fairly large applications.