Optimizing Page Load Time in PHP

There are lots of PHP optimization tricks, and all of them are good, they make loading page of Web Site faster, what is very important for end users. For example it can be done by optimizing CSS or HTML code, reducing HTTP requests, and of course optimizing the PHP code with using some function instead of other.

But, what is the solution in case our page contains many forms, and every form has some number of actions. For example:

<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form1” value=“action1” />

<input type=“submit” name=“form1” value=“action2” />

</form>

<form action=“test.php” method=“post”>

<input type=“text” name=“txt” />

<input type=“submit” name=“form2” value=“action1” />

<input type=“submit” name=“form2” value=“action2” />

</form>

The basically code of processing looks like:

if (isset($_POST[‘form1’]))

{

if ($_POST[‘form1’]==“action1”)

{

do_action1_form1();

}

else if ($_POST[‘form1’]==“action2”)

{

do_action2_form1();

}

else if ($_POST[‘form1’]==“action2”)

{

do_action3_form1();

}

}

This is too redundant, and for a big number of forms it will be too difficult to generate this sequence of statements.

What we are doing?

The simple solution will be to use “eval” function from PHP.

First off all, we must define what forms must be processed.

// List of forms from page

$forms = array(“form1”, “form2”, “form3”);

Of no less importance is to define what method is using forms.

// Which method uses forms for sending data

$method = “POST”;

Now we must get all values, after submission action.

// Get values from submission

eval(“\$values = \$_$method;”);

Now, let’s check if some of form was submitted.

// Get the name of every form

foreach($forms as $form_name)

{

// check if any form was submitted

if (isset($values[$form_name]))

{

// Processing code

}

}

If there has been found any submitted forms then we should get the action name of this event.

// Get action name

$action_name = $values[$form_name];

Well at least after collecting all necessary information, the necessary function have to be called.

// Get function name

$function_name = $form_name.“_”.$action_name;

//Check if function exists

if (function_exists($function_name))

{

// call function

eval(“$function_name();”);

}

// quit the foreach statement,

// because only one form from page can be submitted,

// and with one action

break;

Now just define functions of needed action processing. The function for submit action with name action2 at form with name form3, will look like:

function form3_action2()

{

// Code . . .

}

After this type of optimization, the time of loading page will dramatically improve.

Enjoy!

Advertisements

One Response to “Optimizing Page Load Time in PHP”

  1. M.S. Babaei Says:

    wow, it’s working!

    really simple,
    thanks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: