I have a question about an if statement.
if($_POST['billing_first_name'] == $tet['data']['0']['first_name']) {
}
else
{
run code
}
This code compares a string that it gets from a form with an already existing string.
It works. But when i dont add a capital letter, it runs the code after all.
So for example if i compare String with String it doesnt run the code (which is what i want) But when i compare string with String it runs the code (which i dont want) Only because theres no capital letter included at the beginning. Is there a way to fix this?
I solved it thanks to cmorrissey.
using strlower on both variables allows me to compare 2 strings no matter if there are capital letters inside one of the strings.
if(strtolower($_POST['billing_first_name']) == strtolower($tet['data']['0']['first_name'])) {
//do nothing
}else {
//run code
}
Based on the comment the code is running fine it is an understanding of which block your are in.
if(condition){//code}
will only run code when the condition is true ie
if("String" == "String"){echo "foo";} // will echo foo
if("string" == "String"){echo "foo";} // will not echo foo
In order to run code when false you add an else
// Will echo foo
if("String" == "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo bar
if("string" == "String"){
echo "foo";
} else {
echo "bar";
}
A trick to getting ifs to execute when something is false is to use the !
(meaning not)
// Will echo bar
if("String" != "String"){
echo "foo";
} else {
echo "bar";
}
// Will echo foo
if("string" != "String"){
echo "foo";
} else {
echo "bar";
}