The open source scripting language has a vast list of features and functions. There are hundreds of easy PHP tricks and hacks which can let any beginner work in a project smoothly. Many functions that PHP offers also depends on the extensions that one has enabled. However, here are some tips which could come in handy for all the beginners.
Tip 1. Basename
A file is usually got through absolute path but if one needs to have the information displayed about the file to the viewer (just the file name and not the path) then the function mentioned will help in displaying just the file name. Passing it in the path as the parameter will strip the suffix just like a file extension and forward it as the second parameter.
Tip 2. Isset
Another PHP trick and hacks is that if you want to check whether or not the variable has been set then the isset function eases your work. All you need to do is pass a variable name and if the variable exists it will show true and it will set it to anything but null.
The isset function also works with items in array and associative arrays, it is useful in checking the existence of certain keys on the $_POST and $_GET: if a given value exists, you will be doing one thing, else something else. For instance, a search page would go as below:
Tip 3. Array_Rand
If you need to get a random out of array then you would use the my_rand or rand in order to get a random number, passing 0 and the last index of the array as the maximum and minimum structures. You will now get a ransom key and would be able to pull a value from the array. In an easy and faster way you can array_rand and it will pass the array and return the random key.
In case you need more than one random value one would have to pass a second parameter ascertaining the number and an array of random numbers would be got back.
Tip 4. Referencing
In order to alter the actual variable without defining it as global or returning anything is a method of passing variables into functions. An amperstand (&) is added before the function parameter which will pass as a reference. For instance:
In above example the lowercase() method is used and passed in $name, the function has changed the main string that was passed and in the function $string becomes $name.
Tip 5. List
This function is one of the easiest PHP tricks and hacks for the beginners. In case you wish to assign the items of an array to their own variables then the list function makes this task extremely simple for you.
In the above, it can be seen that the new variable names have been passed as parameters to the list function and it should be set equivalent to the array. This is somewhat different from the normal syntax as the function call is towards the left but it actually works. An example of PHP docs for exploding is below:
Tip 6. Strftime
Dates are an important part of all the web applications and they can be output in the desired format. If it is pulled out from database it is easy to get a timestamp by using time(). With the strftime, the timestamp can be formatted in the required format.
Tip 7. Range
The range function will let iterate over a list of numbers. All you need to do is pass a starting and ending letter or number and it will give back an array of numbers.
A step parameter to state the addition amid numbers can also be passed:
Tip 8. Strip_tags
However, there are some tags that you would want to permit such as <em>, <strong> etc. for styling. Here is what you need to do:
Tip 9. Changing the Permission of Files Created by Application
While working in linux, handling permission takes a lot of time. If PHP application creates files a chmod will ensure they can be accessed out. Else files might be created by PHP user when you are working as a different user then the system will not grant you permission to open the file. Here is how you could change permissions.
Tip 10. While Checking Form Submission the Submit Button Should Remain Unchecked
However, if the application is multi-lingual then there are different uses of ‘Save’. Instead of depending on submit use the below: