I have created a simple function that returns a value depending on which Keys have matching values with the exception that if the key is empty or null then this does not count. My code is below.
function getFontData(){
$font = [];
$font['Font'] = 'Arial';
$font['Font2'] = 'Arial';
$font['Font3'] = 'Arial';
return $font;
}
function compareFonts(){
$compare = 0;
$font = getFontData();
if (!empty($font['Font']) && $font['Font'] === $font['Font2']) {
$compare = 1;
} elseif (!empty($font['Font']) && $font['Font'] === $font['Font3']) {
$compare = 2;
} elseif (!empty($font['Font2']) && $font['Font2'] === $font['Font3']) {
$compare = 3;
}elseif (!empty($font['Font']) && $font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
$compare = 4;
};
return $compare;
}
$matches = compareFonts();
var_dump($matches);
The issue that i am having is that the code which I have written for the function to recognize three identical array keys does not seem to get executed in my if statements. The output in this example is 1. which indicates to me that my code is returning the first if statement which in essence would be true since both 'font' and 'font2' match so there would be no need for the code to go further.
I am assuming that there is a better way of doing this or I am completely wrong in my approach. All suggestions welcome.
regards
w9914420
As Naryl kindly pointed out that the Nested If statement was the way forward.
$compare=0;
if (!empty($font['Font']){
if($font['Font'] === $font['Font2']) {
$compare++;
}
if ($font['Font'] === $font['Font3']) {
$compare++;
}
if ($font['Font2'] === $font['Font3']) {
$compare++;
}
if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
$compare++;
}
}
This code would of been perfect apart from that the if statement would only execute the first statement if the condition of font1 was the same as font 3. Therefore I had to create a more explicit search criteria and came up with this.
function compareFonts(){
$compare = 0;
$font = getFontData();
if (!empty($font['Font'])){
if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
$compare = 1;
}
if($font['Font'] === $font['Font2'] && $font['Font3'] == NULL) {
$compare = 2;
}
}
if (!empty($font['Font2'])){
if ($font['Font2'] === $font['Font3'] && $font['Font'] == NULL) {
$compare = 3;
}
}
if (!empty($font['Font3'])) {
if ($font['Font'] === $font['Font3'] && $font['Font2'] == NULL) {
$compare = 4;
}
}
return $compare;
}
My thinking behind the problem was that since the condition is based on the values of all three fonts then it makes sense that the if statement arguments address this.
// this will execute regardless of the value of [font'3]
$font['Font'] === $font['Font2']
// this is very specific in that [font'3] must be null.
$font['Font'] === $font['Font2'] && $font['Font3'] == NULL
// This works because empty array key is converted to null by non-strict equal
// '==' comparison. For example the following code shows this.
$cho = [];
$cho['key'] = '';
if ($cho['key'] == Null){
echo 'true';
}
// returns true.
Again it is a very unique condition but there may be a better way of doing this so please feel free to comment.
thanks again w9914420.
you have a problem with elsif
:
if (!empty($font['Font']) && $font['Font'] === $font['Font2']) {
$compare = 1;
} elseif (!empty($font['Font']) && $font['Font'] === $font['Font3']) {
$compare = 2;
} elseif (!empty($font['Font2']) && $font['Font2'] === $font['Font3']) {
$compare = 3;
}elseif (!empty($font['Font']) && $font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
$compare = 4;
};
In this code, the 1st if
will evaluate TRUE, so none of the elsif
will be tested and the code will jump straight to the end. You would need something like this:
$compare=0;
if (!empty($font['Font']){
if($font['Font'] === $font['Font2']) {
$compare++;
}
if ($font['Font'] === $font['Font3']) {
$compare++;
}
if ($font['Font2'] === $font['Font3']) {
$compare++;
}
if ($font['Font'] === $font['Font2'] && $font['Font2'] === $font['Font3']) {
$compare++;
}
}
That problem aside, this code a bit too specific, what if you now need 10 more fonts? Maybe we can try to make it a bit simpler:
$font = [];
for($x=0; $x<10;$x++){ //10 fonts
$font['Font'.$x] = 'Arial';
}
$compare=0;
if (!empty($font['Font']){
for($x=0; $x<count($font); $x++){
for($y=1;$y<(count($font)-$x+1);$y++){
if($font['Font'.$x] == $font['Font'.$y]){
$compare++;
}
}
}
}
I don't have much time to test this, but it should be easier to maintain than the previous code.