Why are functions and methods in PHP case-insensitive?
Asked Answered
H

2

118

Functions and methods in PHP are case-insensitive as illustrated in the following example.

function ag()
{
    echo '2';
}

Ag();
class test {
    function clMe()
    {
        echo 'hi';
    }
}

$instance = new test;
$instance->clme();

But that's the not case with variables. What's the rationale?

Himyarite answered 1/5, 2010 at 11:48 Comment(8)
PHP doesn't need no stinkin' rationale!Barite
"Because the soup man says so." No rationale, but references: php.net/manual/en/functions.user-defined.php (between examples 3 and 4), php.net/manual/en/language.variables.basics.phpFrederiksen
class Share{ share($str){ echo $str; } } $sh = new Share(); $sh->share("string"); You may be surprised to see two calls to function share($str). first one because of c'tor and second explicit call for same reasons!!Hadfield
@outis, What do you mean by "soup man"?Raimondo
@Pacerier: it's from an old SNL skit, meaning here that there's no reason, that it's just the way it is. Sadly, I couldn't find a clip.Frederiksen
I am just making this up because I don't know what the creators of PHP where thinking but it seems to me an easy way to justify this would be error prevention. Having classes and functions be case-insensitive minimizes the possibility of re-declaration. For example having a class Foo and then redeclaring class foo (forgetting that you already have Foo) and then mixing them up. Basically it forces you to not have class and method names that are too similar.Billion
@outis, you're referring to the "Soup Nazi" from Seinfeld. YouTube, WikipediaAlumna
@KevinTraas: that's a different bit, with a different catchphrase. "Because the soup man says so." is a line from SNL's "Aunt Jane Creeley's Vegetable Soup".Frederiksen
T
110

Let me quote from Interview – PHP’s Creator, Rasmus Lerdorf

The first version of PHP was a simple set of tools that I put together for my Website and for a couple of projects. One tool did some fancy hit logging to an mSQL database, another acted as a form data interpreter. I ended up with about 30 different little CGI programs written in C before I got sick of it, and combined all of them into a single C library. I then wrote a very simple parser that would pick tags out of HTML files and replace them with the output of the corresponding functions in the C library.

The simple parser slowly grew to include conditional tags, then loop tags, functions, etc. At no point did I think I was writing a scripting language. I was simply adding a little bit of functionality to the macro replacement parser. I was still writing all my real business logic in C.

I have read somewhere that since all the functions introduced essentially felt like tags in an HTML document and since HTML tags were case insensitive, he chose function names in PHP to be case insensitive. Later on this feature remained on in the language.

Turkoman answered 10/6, 2011 at 5:52 Comment(8)
I also remember a quotation from Rasmus in a PHP conference in Paris saying more or less: "I'm definitely not a good programmer, in terms of following strict coding rules or standards, but I can say that if you rely on case sensitivity to recognize one function name from another, you're in kind of serious trouble!"Navicert
is it still case-insensitive in future?Kacykaczer
So that's why php programmers use underscore instead of camelcase when naming their functions.Alysaalyse
@Alysaalyse Some do. The good ones don't. At least, not anymore.Propertied
@dan, plenty of "good" programmers use underscores. In fact, PSR-0 purposely doesn't make a recommendation. A good programmer picks underscores or camelcase and sticks to that convention. Making that choice the measure of whether someone is a good programmer is pretty silly.Yellowthroat
I totally do not agree with the comment from @TomDesp above: If a function can be called by typing myFunction or MyFunction or MyFunCTION or ..., then it makes the code base inconsistent and harder to read. IMO it also makes it harder to find usages or refactor. I do agree that you should not use case sensitivity as an excuse to write method names that only differ in case.Kiker
I believe you misread my comment. I do think you should use the appropriate function calling case for obvious readability purpose. I just quote Rasmus saying that case insensitiveness of functions is not a language limitation for any serious programmers. Besides, coding standards as a whole (function, variable naming and calling) should probably be monitored and controlled by a tool other than the PHP syntax parser, like a code sniffer for instance.Navicert
@Yellowthroat PSR-1 states "Method names MUST be declared in camelCase." I believe you're referring to variable naming conventions that while not a particular style, should be uniform and consistent.Snail
B
37

Yes, functions and methods names are not case-sensitive.

And yes, variables names are case-sensitive.

I am not sure there's a reason for that -- except it's been this way for a long time, and, so, remains the case, for backward compatibility reasons.



As a reference, a couple of links / quotes to various pages of the manual:

For functions (quoting):

Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

And methods are not much more than functions in objects -- especially when we think about PHP 4 and backward-compatibility.


And, for variables ([quoting][2]):

Variables in PHP are represented by a dollar sign followed by the name of the variable. The variable name is case-sensitive.

And object properties are not much more than variables in objects -- same remark about PHP 4 and backward-compatibility.

Bronk answered 1/5, 2010 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.