This is a discussion on Conditional function inconsistency within the PHP Language forums, part of the PHP Programming Forums category; Since 'function' is a single statement, I would think that the 'if' part shouldn't need the curly braces: if ($...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Since 'function' is a single statement, I would think that the 'if' part
shouldn't need the curly braces: if ($foo) function bar() { echo 'FOO!'; } Alas, this fails: the function must be embraced: if ($foo) { function bar() { echo 'FOO!'; } } Not a big deal, but this is inconsistent behaviour. -- I am dae3 and I approve my own message. ---------------------------------------' History is on our side (as long as we can control the historians). |
|
|||
|
dae3 wrote:
> Since 'function' is a single statement, I would think that the 'if' part > shouldn't need the curly braces: > > > if ($foo) > function bar() { echo 'FOO!'; } > > > Alas, this fails: the function must be embraced: > > > if ($foo) > { function bar() { echo 'FOO!'; } } > > > Not a big deal, but this is inconsistent behaviour. > > Not a big deal because you really shouldn't be doing it. It's a poor programming practice which makes code very hard to debug later. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |
|
|||
|
Jerry Stuckle <jstucklex@attglobal.net> wrote:
> Not a big deal because you really shouldn't be doing it. It's a poor > programming practice which makes code very hard to debug later. That really isn't the case, Jerry. I'm doing this to define stripos() IF my program is running on PHP < 5. This enables it to run on both PHP4 and PHP5 with absolutely no changes. If you still think this is a poor programming practice or that it makes the program hard to debug, well... I guess we'll have to disagree on what "good programming" means. -- I am dae3 and I approve my own message. ---------------------------------------' Avoid cliches like the plague. They're a dime a dozen. |
|
|||
|
dae3 wrote:
> Jerry Stuckle <jstucklex@attglobal.net> wrote: > >> Not a big deal because you really shouldn't be doing it. It's a poor >> programming practice which makes code very hard to debug later. > > > That really isn't the case, Jerry. > > I'm doing this to define stripos() IF my program is running on PHP < 5. > This enables it to run on both PHP4 and PHP5 with absolutely no changes. > > If you still think this is a poor programming practice or that it makes > the program hard to debug, well... I guess we'll have to disagree on > what "good programming" means. > > If you want to determine if a function exists, you should be using function_exists(). And you can use it to define something like stripos(). But the problem you can run into is if you have stripos() defined like this in different different locations - and now someone changes ONE of those functions? Better would be to conditionally include a file which defines the function, i.e. if (!function_exists('stripos')) include 'stripos.php'; That way you are sure to have only one definition of the function. You could put the test itself in stripos.php, but that means you're going to include the file all of the time. -- ================== Remove the "x" from my email address Jerry Stuckle JDS Computer Training Corp. jstucklex@attglobal.net ================== |