Assign variables while disregarding capitalization
✔ Recommended Answer
Is this PHP? If so, try this:
if (strtolower($place) == 'new york') {
http://php.net/manual/en/function.strtolower.php
Source: stackoverflow.com
Answered By: delboy1978uk
To assign variables in PHP while disregarding capitalization, you can convert all the input to lowercase or uppercase using the strtolower()
or strtoupper()
functions, and then use the resulting string to check and assign the variables.
Here's an example of how to do this:
php// Define the input string
$input = 'Foo';
// Convert the input string to lowercase
$lowerInput = strtolower($input);
// Check the lowercase input string and assign variables
if ($lowerInput === 'foo') {
$var1 = 'Value 1';
} elseif ($lowerInput === 'bar') {
$var2 = 'Value 2';
}
// Convert the input string to uppercase
$upperInput = strtoupper($input);
// Check the uppercase input string and assign variables
if ($upperInput === 'FOO') {
$var3 = 'Value 3';
} elseif ($upperInput === 'BAR') {
$var4 = 'Value 4';
}
// Output the assigned variables
echo $var1 . "\n"; // Outputs "Value 1"
echo $var2 . "\n"; // Outputs nothing
echo $var3 . "\n"; // Outputs "Value 3"
echo $var4 . "\n"; // Outputs nothing
In this example, we define an input string ($input
) and convert it to lowercase using the strtolower()
function. We then use the resulting lowercase string to check and assign variables ($var1
and $var2
) using an if-else statement. We repeat the process with the strtoupper()
function, and check and assign variables ($var3
and $var4
) using another if-else statement.
Note that you should be careful when using this approach, as it may introduce some ambiguity if you have multiple input strings that can be converted to the same lowercase or uppercase value.
Comments
Post a Comment