Jump to content

Recommended Posts

So i have a quick question if someone could be kind enough to shed some light on.

 

Say i have 5 forms on a page and i process them all on that same page at the top

 

if (isset($form1)){
//code
}
if isset($form2)){
//code
}
//so on..

 

That will pile up to alot of php code on one page now.

Now i know that only one peice of that code will be run when there respective button is clicked but would the rest of the code slow down the page or cause load even know it cant run untill we click there button, i mean after all the code is still on the page weather its being ran or not.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/
Share on other sites

It won't be a problem to have all the process code together. I would add a hidden input element to each form to give it a name e.g.

 

<input type="hidden" name="form_name" value="contactus" />

 

Then in your PHP process code I would handle it like

 


if($_SERVER['REQUEST_METHOD'] == 'POST')
{

	$formName = isset($_POST['form_name']) ? $_POST['form_name'] : '';

	switch($formName)
	{
		case 'contactus':
			// handle contact form

			break;
		case 'search':
			// handle search form

			break;
		default:
			// unknown form

	}

}

 

If there is a lot of process code then it could be better to create for example a base class form handler, then extend it for each form, to keep the code organised etc.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/#findComment-1254915
Share on other sites

There should be no significant performance if those lines are not actually being executed. There is always a cost as you add more complexity, but unless the files grew to many megabytes in size you should never see a difference.

 

But, on the other hand I would suggest breaking out the code just for the sake of maintainability and flexibility. Have your five forms submit to the same page, but break the processing code for each form into separate file and use a switch to determine which processing code to include.

 

if (isset($form1)){
    include('form1processing.php');
}
if isset($form2)){
    include('form2processing.php');
}
// Etc.

 

That makes your files much more manageable IMHO. I abhor files with tons of code. It makes any modifications very difficult.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/#findComment-1254916
Share on other sites

Php is a parsed, tokenized, and interpreted language.

 

All the lines of code in your main file will be parsed and tokenized when the page is requested (included/required code is parsed/tokenized/executed when the include/require statement is executed), so yes each line of code that is present will take an amount of time to parse and convert to tokens.

 

During the interpretation/execution phase, each conditional statement will be evaluated when execution reaches that point in the code and if the condition evaluates as false, execution jumps to the end of the conditional block. The statements that are within the conditional block are not executed and don't consume processing time.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/#findComment-1254918
Share on other sites

Php is a parsed, tokenized, and interpreted language.

 

All the lines of code in your main file will be parsed and tokenized when the page is requested (included/required code is parsed/tokenized/executed when the include/require statement is executed), so yes each line of code that is present will take an amount of time to parse and convert to tokens.

 

During the interpretation/execution phase, each conditional statement will be evaluated when execution reaches that point in the code and if the condition evaluates as false, execution jumps to the end of the conditional block. The statements that are within the conditional block are not executed and don't consume processing time.

 

I see thank you for the information.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/#findComment-1254920
Share on other sites

Yeah i though that i would use includes for them but then i thought that it would be the same outcome as including the file is basically the same as having the code on the page as it gets included.

 

As PFMaBiSmAd explained, the conditional blocks are not executed unless the condition is true. That means the include files would never be read into memory, except for the ones where the condition is true. But, I think you are really over thinking this. unless you are building a site that will have massive traffic from day one, build it to be logical and functional. The additional time for the parser to read all the code in one page would be inconsequential. But, as I stated above you should break the code out into separate pages just for the fact that it would be more logical and be easier to manage. The fact that the code won't need to be read (unless needed) is an added benefit.

 

You should always take performance into consideration, but don't go overboard. You can spend 10x the effort to squeeze every ounce of performance out of the application and only get 5% increase in performance. That's not to say there aren't some things you should absolutely do and/or not due. The biggest performance issue I see in most scripts posted to this site is where the scripts run queries within loops. You should never do that.

Link to comment
https://forums.phpfreaks.com/topic/244325-quick-question/#findComment-1254940
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.