Are PHP functions case sensitive?

2019-01-03 02:32发布

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.

8条回答
混吃等死
2楼-- · 2019-01-03 03:08

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.

查看更多
你好瞎i
3楼-- · 2019-01-03 03:15

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
查看更多
该账号已被封号
4楼-- · 2019-01-03 03:20

No.

PHP functions are not case sensitive.

查看更多
老娘就宠你
5楼-- · 2019-01-03 03:25

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
查看更多
看我几分像从前
6楼-- · 2019-01-03 03:26

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.

查看更多
家丑人穷心不美
7楼-- · 2019-01-03 03:29

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.

查看更多
登录 后发表回答