![]() |
PHP/MySQL tips and tricks
Hi,
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. print_r() 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. function printR($var) { echo '<pre>'; print_r($var); echo '</pre>'; } 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 if(file_exists('filename')) echo file_get_contents('filename'); 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. mysql_errno() You can use this feature to tackle with the MySQL errors. for eg. PHP Code:
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 PHP Code:
|
Include vs readfile
Both are kind of same. The difference is in the speed and time taken for execution. Readfile is more faster than include. BUT, usage of both are different test1.php PHP Code:
Now if you do include.php('test1.php'); it'll first execute the PHP, prouce the result and then get the content. so the result will be Quote:
Quote:
Conclusion If you want to include static files (with no PHP DYNAMIC CONTENT), use readfile. its more faster. if you want to include a fie which includes dynamic contents (like test1.php) then use include. |
Commas in echo
This might be the first time you are seeing this. you can echo something like PHP Code:
and if you do PHP Code:
So, using commas is more faster than using periods. Note: it only works in echo. YOU CANNOT DO THIS:: $str = 'This ','is ','a ','sentence.';//THIS IS WRONG SYNTAX |
using variables to store and call functions
You can use variables to store function names and then call them. PHP Code:
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 from md5() to sha1() 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. |
Ternary Operator
This is great operator. its actually an if-else statement, but they gave it a fancy name. the syntax is (condition)?this:else; it says if the condition is TRUE then do this or do else example: PHP Code:
The more general example is using different colors for rows PHP Code:
|
Save image using file_get_contents
I am using talkphp logo for this example, hoping Ryan wouldn't mind :D I din't know i could do this until i did. you can rad an image file and then save it. its like saving images automatically. PHP Code:
|
Regular Expression
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 preg_match() 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. |
| All times are GMT. The time now is 02:39 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0