keywords-lowercasefixable
Ensure that PHP keywords are lowercase
Examples
- Examples of correct code for this rule using default options
Control structure with lowercase keywords
php
<?php
if(true){
foreach($element as $key => $value){
echo "$KEY";
}
}
for($i = 0; $i < 10; $i++){
if($i === '5'){
continue;
}
echo "$i";
}
if(true){
echo "true";
} elseif(false){
echo "false";
} else {
echo "Neither true nor false";
}
Class and file require
php
<?php
require("test.php");
require_once("test.php");
include_once("test.php");
include("test.php");
class Base
{
protected $anonymous;
public function __construct(){
$this->anonymous = new class extends ArrayObject
{
public function __construct(){
parent::__construct(['a' => 1, 'b' => 2]);
}
};
}
}
function, global and return keywords
php
<?php
function test() {
global $var;
echo "Function keyword is not lowercase";
return $var;
}
Switch control structure
php
<?php
switch($var):
case 1:
echo "1";
break;
case 2:
echo "2";
break;
default:
echo "default";
break;
endswitch;
While, do while loops
php
<?php
while ($i <= 10):
echo $i;
$i++;
endwhile;
do {
echo $i;
$i++;
} while ($i <= 10);
Try catch
php
<?php
try {
throw new Exception('Some message');
} catch (Exception $e) {
echo $e->getMessage();
}
try {
new className();
} catch(Exception $e){
echo $e->getMessage();
}
- Examples of incorrect code for this rule using default options
Control structure
php
<?php
IF(true){
FOREACH($element as $key => $value){
ECHO "$KEY";
}
}
For($i = 0; $i < 10; $i++){
if($i === '5'){
CONTINUE;
}
ECHO "$i";
}
IF(true){
echo "true";
} ELSEIF(false){
echo "false";
} ELSE {
echo "Neither true nor false";
}
Class and file require
php
<?php
rEQUIRE("test.php");
REQUIRE_ONCE("test.php");
INCLUDE_ONCE("test.php");
INclude("test.php");
class Base
{
PROTECTED $anonymous;
PUBLIC FUNCTION __construct(){
$this->anonymous = new class extends ArrayObject
{
public function __construct(){
parent::__construct(['a' => 1, 'b' => 2]);
}
};
}
FUNCTION __destruct(){
FOREACH($this->anonymous as $key => $value){
ECHO "$key => $value";
}
}
}
function, global and return keywords
php
<?php
FunCtion test() {
Global $var;
echo "Function keyword is not lowercase";
RETURN $var;
}
Switch control structure
php
<?php
SWITCH($var):
CASE 1:
echo "1";
BREAK;
CASE 2:
echo "2";
BREAK;
DEFAULT:
echo "default";
BREAK;
ENDSWITCH;
While, do while loops
php
<?php
WHILE ($i <= 10):
echo $i;
$i++;
ENDWHILE;
DO {
echo $i;
$i++;
} while ($i <= 10);
Try catch
php
<?php
TRY {
THROW NEW Exception('Some message');
} CATCH (Exception $e) {
echo $e->getMessage();
}
try {
NEW className();
} catch(Exception $e){
echo $e->getMessage();
}