Conditional function inconsistency

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 ($...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-20-2008
dae3
 
Posts: n/a
Default Conditional function inconsistency

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).
Reply With Quote
  #2 (permalink)  
Old 07-20-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Conditional function inconsistency

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
==================

Reply With Quote
  #3 (permalink)  
Old 07-20-2008
dae3
 
Posts: n/a
Default Re: Conditional function inconsistency

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.
Reply With Quote
  #4 (permalink)  
Old 07-20-2008
Jerry Stuckle
 
Posts: n/a
Default Re: Conditional function inconsistency

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
==================

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:06 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0