PHP & Case Sensitivity

PHP & Case Sensitivity

Why is PHP partially case senstive?

I can only speculate that this stems from very early versions, probably PHP/FI 2.0. The manual explicitely states:

Keep in mind that PHP/FI function names are not case sensitive.

Most user input, such as GET and POST parameters, has always been registered as global variables, back then. Treating these as case insensitive would likely have caused issues, and supposedly therefore all variables have been treated as being case sensitive.

From what I can tell these have been the only kinds of identifiers in PHP/FI 2.0. All others have been introduced later, apparently mimicking the case-insensitive function names.

Constants, which are special, have only been introduced as of PHP 4 (the PHP 3 manual mentions "constants", but these are nowadays referred to as "literals"). For some mysterious reason (maybe no consensus could be found), it had been decided to allow constant identifiers to be define()d either case sensitive or insensitive on the developers discression. Interestingly, while define() defaults to case sensitive constants, the respective C counterparts (REGISTER_*_CONSTANT) default to case insensitive.

to check a condition in any case sensitive in php

Just convert the string to either upper or lower case and then do your comparison:

if ( $red == '50' && strtolower($city) === 'bali' ) {
//My code here
}

or

if ( $red == '50' && strtoupper($city) === 'BALI' ) {
//My code here
}

Is PHP function names case-sensitive or not?

They are case insensitive, see this:

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

http://www.php.net/manual/en/functions.user-defined.php

How to perform case insensitive str_contains?

You want a case-insensitive version of str_contains()
The short answer is: There isn't one.

The long answer is: case-sensitivity is encoding and locale dependent. By the time you've added those are information to a hypothetical str_icontains(), you've recreated mb_stripos(). TL;DR - Don't do it.

Force case sensitive method calls

PhpStorm has an inspection for that -- you just need to activate it.

  • Settings (Preferences on macOS) | Editor | Inspections
  • PHP | Code Smell | Case mismatch in method call or class usage

PHP Sample Image 5

Sample Image

How to make sure case insensitive usernames are always accepted by Symfony UserBadge?

You could create a custom User Provider.

Then in your provider simply call strtolower() on the identifier, and LOWER() on the identifier column.

A rather simple implementation, assuming you inject the Entity Manager on your User Provider.


public function loadUserByIdentifier(string $identifier): UserInterface
{
$user = $this->em->createQueryBuilder()
->select('u')
->from(YourUser::class, 'u')
->where('LOWER(u.username) = :username')
->setParameter('username', strtolower($identifier))
->setMaxResults(1)
->getQuery()
->getOneOrNullResult();

if (!$user instanceof YourUser::class) {
throw new UserNotFoundException('No user found for ' . $username);
}

return $user;

}

Adjust to your application constraints. You can probably move the query itself to the specific entity repository, and create the query there, instead of the provider.



Related Topics



Leave a reply



Submit