Are PHP Keywords Case-Sensitive

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.

Are PHP functions case sensitive?

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.

String type casing in php, is it string or String?

As PHP is a partially case sensitive language and, in particular, keywords are NOT case sensitive, the case doesn't matter for built-in type names. Both string and String will work as type declaration (aka 'type hint').

The official documentation mentions the lowercase variants, so it's probably a good idea to use lowercase keywords as code style — just like most PHP code styles use foreach and not Foreach, for example.

P.S. You can find the documentation on the PHP Function arguments page, 'Type declarations' section.

Which are case sensitive among php/javascript/html?

HTML is not case-sensitive (XHTML is, though).

PHP respects case in variable names, but not functions.

Javascript is case-sensitive.

Highlight keyword without case sensitive?

This should work for you now. It expects the keywords to be a string with spaces in between.

Obviously if this is taking user input then you will need to escape that input somehow.

function highlight($str, $keywords) {

// Convert keywords to an array of lowercase keywords
$keywords = str_replace(' ', ',', $keywords);
$keywordsArray = explode(',', strtolower($keywords));

// if any lowercase version of a word in the string is found in the
// keywords array then wrap that word in <strong> tags in the string
foreach (explode(' ', $str) as $word) {
if (in_array(strtolower($word), $keywordsArray)) {
$str = str_replace("$word ", "<strong>$word</strong> ", $str);
}
}

return $str;
}

The space after the $word var and its replacement are to prevent double encapsulating the keyword if it appears in the string more than once.

example usage:

$str = 'the quick brown fox jumped over Mr Brown the lazy dog';
$keywords = 'the brown fox';
echo highlight($str, $keywords);

will output:

<strong>the</strong> quick <strong>brown</strong> <strong>fox</strong> jumped over Mr <strong>Brown</strong> <strong>the</strong> lazy dog

Uppercase Booleans vs. Lowercase in PHP

The official PHP manual says:

To specify a boolean literal, use the
keywords TRUE or FALSE. Both are
case-insensitive.

So yeah, true === TRUE and false === FALSE.

Personally, however, I prefer TRUE over true and FALSE over false for readability reasons. It's the same reason for my preference on using OR over or or ||, and on using AND over and or &&.

The PSR-2 standard requires true, false and null to be in lower case.

how to make case insensitive search

Avoid using alot of like operators. Full text search is the way to go. Full txtsearch are case-insensitive by default. So you will have no problem with that.

Here is how to integrate mysql fulltext with codeigniter

Using Match and Against in MySQL and CodeIgniter



Related Topics



Leave a reply



Submit