abdfahim Posted August 26, 2006 Share Posted August 26, 2006 hi friends, i need a little information. Here I am writing two codes that can be used in same purpose.[code]$new=$_GET['new'];if ($new==0){ //Do something}else{ //Do something}[/code]an alternative is[code]$new=$_GET['new'];//$params= something needed inside functions;if ($new==0){ func_1($params);}else{ func_2($params);}func_1($params){ //Do something}func_2($params){ //Do something}[/code]Now my question is which is better and most importantly faster and why ??? Quote Link to comment https://forums.phpfreaks.com/topic/18722-solved-which-procedurecode-is-better/ Share on other sites More sharing options...
wildteen88 Posted August 26, 2006 Share Posted August 26, 2006 First one. However you can use a function if you wish. But first one is fine. Although I'd do this:[code]$new= isset($_GET['new']) ? $_GET['new'] : '';if ($new == 0){ //Do something}else{ //Do something}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18722-solved-which-procedurecode-is-better/#findComment-80739 Share on other sites More sharing options...
Barand Posted August 26, 2006 Share Posted August 26, 2006 The first is going to be faster as there is always an overhead with function calls as the params and the return address have to placed on the stack. However we're talking nonoseconds here and you are not going to notice in practice.As far as I am concerned, then, time to execute is not an issue but rather code readabilty and maintenance (that's what costs the money). If "Do something" is just a few lines of simple code I'd use the first. If we are talking something more complex I'd go with second and use meaningful function names so it's obvious what is happening[code]switch ($_GET['action']) { case 'New' : insert_new_record($params); break; case 'Edit': display_edit_form($params); break;}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18722-solved-which-procedurecode-is-better/#findComment-80742 Share on other sites More sharing options...
abdfahim Posted August 27, 2006 Author Share Posted August 27, 2006 Thanx guys ... thanx v much Quote Link to comment https://forums.phpfreaks.com/topic/18722-solved-which-procedurecode-is-better/#findComment-81002 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.