473,406 Members | 2,843 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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).
Jul 20 '08 #1
3 1089
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.
js*******@attglobal.net
==================

Jul 20 '08 #2
Jerry Stuckle <js*******@attglobal.netwrote:
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.
Jul 20 '08 #3
dae3 wrote:
Jerry Stuckle <js*******@attglobal.netwrote:
>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.
js*******@attglobal.net
==================

Jul 20 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

46
by: Scott Chapman | last post by:
There seems to be an inconsistency here: Python 2.3.2 (#1, Oct 3 2003, 19:04:58) on linux2 >>> 1 == True True >>> 3 == True False >>> if 1: print "true" ....
8
by: neblackcat | last post by:
Would anyone like to comment on the following idea? I was just going to offer it as a new PEP until it was suggested that I post it here for comment & consideration against PEP 308. I'm far...
3
by: Jouke Langhout | last post by:
Hello all! For quite some time now, I've got the following problem: Access won't close properly when a user closes the application. An ACCESS process stays active and that process can only be...
1
by: GGerard | last post by:
Hello Is there a way to use a variable in the Conditional Formatting of a Textbox? Example : I want the background of a textbox in a continuous form to change color when the value of...
12
by: wanghz | last post by:
Hi, Could I ask some questions about the conditional compilaion? Suppose I have three simple files: a.c, b.c and c.h /* --------a.c--------- */ #include <stdio.h> #include "c.h" int...
9
by: Marty | last post by:
Hi, Does using the the conditional operator (?:) instead of the common "if" statement will give a performance gain in a C# .NET 2003 application (even in C# .NET 2005?). What is the advantage...
8
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
5
by: paulo | last post by:
Can anyone please tell me how the C language interprets the following code: #include <stdio.h> int main(void) { int a = 1; int b = 10; int x = 3;
4
by: Zack | last post by:
Hi, Let's say you have this function named validate. validate looks like this: function validate() { return ($F("Email") != '' ? ($("Name").value = $F("Email")) : false); }
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.