Lodius2000 Posted June 2, 2008 Share Posted June 2, 2008 can you define a function inside an if statement, or does it need to be outside the statement? Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/ Share on other sites More sharing options...
dezkit Posted June 2, 2008 Share Posted June 2, 2008 anything is possible with php Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/#findComment-555313 Share on other sites More sharing options...
Lodius2000 Posted June 2, 2008 Author Share Posted June 2, 2008 ...and you can do anything at zombocom, but, i have a call to a function inside and if statement and the function is defined later in the same if statement, and i am getting a call to undefined function error. bu before i go and do all sorts of reworking of code , i would like to know if that is the problem thanks Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/#findComment-555315 Share on other sites More sharing options...
chronister Posted June 2, 2008 Share Posted June 2, 2008 I have found that the best thing to do is to define functions in an include file that is loaded before you need to use it. To the best of my knowledge there is not any noticeable loss in performance by loading a declared function that is not used. I typically have a standard include file that holds generic functions that may be used site-wide. I will then have several more include files that are specific for parts of the site. I load each of them dynamically by reading the dir where I store includes and then loading what it finds. Makes it nice because I can include files just by placing them in that directory. Nate Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/#findComment-555327 Share on other sites More sharing options...
soycharliente Posted June 2, 2008 Share Posted June 2, 2008 I agree that you should bring out your function definitions and simply call them when you need them. You write a function when you're doing something multiple times in multiple places. Don't just write a function because you can. If you're doing something 1 time only in 1 case, don't even write the function. Just leave the code in. Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/#findComment-555359 Share on other sites More sharing options...
PFMaBiSmAd Posted June 2, 2008 Share Posted June 2, 2008 A function definition inside a conditional statement will only be defined when the condition is true and the code in that conditional statement is executed. In this case, a call to the function before the function definition won't work. If your function definition is not within a conditional statement, then it will be found during the parse phase and the definition and a call to it can be in any order in the logic. Link to comment https://forums.phpfreaks.com/topic/108315-solved-quickie-regarding-functions-and-if-statements/#findComment-555374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.