Tag Archives: PHP

A simple way to debug a php application in production mode

How to get file extension using PHP

A simple way to debug a php application in production mode

A simple yet powerful approach to get file extension using PHP. We usually need to extract file extension from file name while uploading file so that we could generate new name and rename file to our convenience. We could create new file name using time, random or hashing, example of which has been given at the last paragraph of this post.

function get_file_extension($filename) {
    return substr($filename, strrpos($filename, '.')+1, strlen($filename)-strrpos('.', $filename));


It uses built-in substring, strrpos and strlen functions to extract file extension. A brief on these functions. Continue reading

A simple way to debug a php application in production mode

Useful php debug function to output data with line number

A simple way to debug a php application in production mode

A very useful php debug function i like to use to print debug output to browser window. In some cases where i may not output the debug to browser window i use the same php debug function to receive debug output in my email inbox.

We usually use print_r or var_dump to show debug output. This php debug function uses print_r by default but one can use var_dump to show the output. Continue reading

Caching large menus or header/footer links in WordPress

Even if you are not using some robust caching plugin or tool in wordpress you can cache small chunks of data using basic caching techniques of PHP. In my example i had been creating a few dozens of links in the footer i would cache my links part so every time my page loads wordpress does not have to create each and every link by querying the database for each individual link. Here’s the simple code: Continue reading

How to debug a php application in production/live mode

A simple way to debug a php application in production modeHow to debug a live website built in php?

Here’s a basic, simple and custom method i usually use to debug live or production web applications (web sites).

First of all i place the following lines of code in a config.php or somewhere so it is loaded in all targeted pages of the website. Continue reading

Validating php multiple-value checkbox form element with javascript

In php a multiple-value form element such as checkbox is named suffixed with a set of square brackets. For example,

My commonly used colors are:

<form action="index.php" name="myform" method="post" onsubmit="return validate_form();">
<input type="checkbox" name="colors[]" value="red" />
<input type="checkbox" name="colors[]" value="green" />
<input type="checkbox" name="colors[]" value="blue" />

To validate these kind of checkboxes’ using javascript is little tricky. While on the other hand, having a single checkbox without multiple-value selection it would have been straight forward, something like this:

function validate_form()    {
// considering form element <input type="checkbox" name="color" value="red" />
if(true==document.myform.color.checked)    {
//checked, ok. Let it go.
}    else    {
//not checked, stop it.

Considering multiple-value selection the document.myform[].color.checked or document.myform[].color.length would throw javasript error. To make it work as expected the javascript code in your validate_form function will need some changes. Here we go.

function validate_form()    {
var flag = false; //at the initial state we assume that nothing is checked
for(var i=0; i < eval('document.myform.elements["colors[]"].length') ; i++)    {
if(true === eval('document.myform.elements["colors[]"]['+i+'].checked'))    {

flag=true; //at least some thing is checked, we are good to let them go past this check

//Hint: As a separated implementation somewhere though, if you wanted you could trigger the checked/un-checked status of your checkbox like the following:
//document.myform.elements["colors[]"][i].checked = false; //or true etc.

if(true==flag)    {
//checked (at least something), ok. Let it go.
//Hint: if you wanted that all your checkbox should have been checked check for the value of i variable
}    else    {
//not checked, stop it.

In line 3 of modified function above we use powerful eval function to evaluate the string passed to it. The eval function accepts any valid piece of javascript code and returns output value. In the passed code string to eval function we write normal (parent to child access) algorithm to access the elements property (and its element) of myform object which is a property of our web page (document) object.