Are PHP functions case sensitive?
Asked Answered
P

8

104

I was digging through some code, and I found some calls to mySQL_fetch_array. Is PHP case sensitive about function names? I recall reading this somewhere but can't seem to find any reference to it.

Plausive answered 13/4, 2011 at 1:31 Comment(5)
No they don't seem to be. preference is usually lower caseMissionary
Duplicate of #2750281. No, functions are not case sensitiveDroughty
For reference: it's buried here in the middle php.net/manual/en/functions.user-defined.php in a Note: block.Tyrus
I vote to leave this open — the title is much more general and explicit, thus it is likely to help more people.Strohl
Why didn't you simply try it out?Midget
M
104

I am quoting from this:

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

So, its looks like user-defined functions are not case-sensitive, there was a vote for making functions/objects under PHP5 case-sensitive.

Mcadoo answered 13/4, 2011 at 1:41 Comment(12)
Too bad, they should make announcement, so every body can vote.Mcadoo
PHP5 is already out, your only hope is PHP6 and I don't think they would take that step. It would break to much for little gain.Coneflower
There is some one said [php6 already released]giorgiosironi.blogspot.com/2010/04/php-6-finally-released.html.Mcadoo
It's not on the main PHP website as a stable, release candidate, or beta. It only has the latest 5.3 branch on there. So no, PHP6 is not out yet.Coneflower
@Mark Tomlin I disagree. The best practice is to maintain the same case that the manual has, so it should break little, while increasing clarity.Plausive
@Ben Bauphinee, I never said I supported one or the other. I'm just stating the fact.Coneflower
wow. that is sad. although ImAgEcReAtEfRoMpNg() is a nice way of obfuscating code...Gamber
@Mcadoo that was an april fools' jokeFreeland
Since the quote says "case-insensitive", why do you say "So it looks like user-defined is case-sensitive"? If that's a typo, you really should fix it.Taintless
Wow! This is really nice to find out after 7 years of coding!Sharrisharron
I don't understand why so many people voted for "yes", what's so good in case-sensitive names? I always hated them, they only make language more confusing.Milliary
If it'd be case sensitive, getUrl would be a different method than getURL, or geturl... This would make it more difficult to debug in some SituAtioN.Labarum
E
37

No.

PHP functions are not case sensitive.

Extrude answered 13/4, 2011 at 1:34 Comment(0)
T
21

TL;DR: class names are case-insensitive, but use always the same case as in the declaration (same as with functions). Also, instantiating classes with different case as they were defined may cause problems with autoloaders.


Also, class names are case-insensitive:

<?php
class SomeThing {
  public $x = 'foo';
}

$a = new SomeThing();
$b = new something();
$c = new sOmEtHING();
var_dump($a, $b, $c);

This outputs:

class SomeThing#1 (1) {
  public $x =>
  string(3) "foo"
}
class SomeThing#2 (1) {
  public $x =>
  string(3) "foo"
}
class SomeThing#3 (1) {
  public $x =>
  string(3) "foo"
}

Problem is using autoloaders and case-sensitive file-systems (like ext2/3/4), in that you must call the class name with the same case the file containing the class is named (not how the class name is actually cased), or use strtolower:

The class file:

<?php
// filename something.php
class SomeThing {
   ...
}

The autoloader function (__autoload or a function to register with spl_autoload_register)

function my_autloader($className) {
  $filename = CLASSES_DIR . DIRECTORY_SEPARATOR . $className . '.php';
  if (file_exists($filename)) {
    require($filename);
  }
}

Now with this code:

$a = new something(); // works
$b = new SomeThing(); // does not work
$c = new SOMETHING(); // does not work

You may made this work (ie. having effectively case insensitive class names using an autoloader) if you added a call to strtolower() in the autoloader code, but as with functions, is just better to reference a class in the same way as it is declared, have the filename with the same case as the class name, use autoloaders, and forget using strtolower and the likes.

Therianthropic answered 14/6, 2013 at 11:52 Comment(0)
B
7

No, they are not case sensitive, however, you should always use the case that is in the manual, for consistency.

However, variables are case sensitive.

Brash answered 13/4, 2011 at 1:37 Comment(0)
T
5

And method names also case-insensitive. eg:-

<?php
       class C { 

           public function method() { } 

           public function METHOD() { } 
       }

output:

PHP Fatal error:  Cannot redeclare C::METHOD() in ....php on line 6
Textuary answered 10/12, 2012 at 14:33 Comment(0)
J
4

In PHP variables are case sensitive but functions have no issue like this.You can use following statements for displaying output, all will show the same result.

<?php
Echo "This is a test script";
ECHO "This is a test script";
echo "This is a test script";
?>

On the other hand, if you will change the case sensitivity of variables then it will show the error.

Example:

<?php
$a=5;
echo $A;// It will show the error.
?>

Output:

Notice: Undefined variable: A in C:\xampp\htdocs\test.php on line 3
Jodiejodo answered 26/8, 2015 at 13:7 Comment(0)
M
2

In conclusion of everyone's response. Even though PHP does not require character case consistency in all instances even till now in PHP5.

Best practice will be

always use same cases when reference back to either variables(its' mandatory) or functions(its' optional, but recommended).

You never know maybe one day the vote get through and you will save the whole nightmare of changing cases in your applications made couple of years ago that require update in PHP.

Hope it helps in anyway.

Mononucleosis answered 29/8, 2013 at 16:49 Comment(1)
Re "save the whole nightmare of changing cases": No, my understanding is that if the change is ever made, case-sensitivity would be an option, set in php.ini. Thus preserving backwards compatibility.Quaquaversal
D
-2

May be this is too late, but...

Everyone has already known here: PHP engine does not care about letters' case.

And there is the vote on PHP bugtracker, where majority says: "Yes, I am pro case sensetivity".

But I am contra, because in my MVC framework, I call controller action:

    $currentController = new $controller($parameters);
    $currentAction = $currentController->{$action}();

$controller and $action are taken from URL as is.

Think, if a user publishes a link to my site: https://my-site/MyPartnerController/MyPartnerGallery

while class is named myPartnerController...

It means noone ever gains this page if PHP's classes and functions names are case sensitive.

Yes, I always use all the names in code as declared. But I pray they never make functions' and classes' names case sensitive.

Thank you!

Dumpish answered 12/7, 2015 at 21:58 Comment(1)
that's really bad... if I'd go to my-site/myLogInPage/loginsuccess, and take a few guesses at what you called your login success function i'd be in, or able to erase a few things....Wagonette

© 2022 - 2024 — McMap. All rights reserved.