In this example code, I am running wild card searches in all the functions. Chances are that function alkali() will give a right answer and the other function a wrong answer and vice versa.
How do I suppress the fail part on one function once I get a right / correct answer on another function? I.e, I want to not show "metal is unknown" if I already show "this is an alkali metal".
<?php
class metals
{
function alkali()
{
if (($rowC['description'] == " Alkali Metal")) {
echo '';
} else {
echo 'metal is unknown';
}
}
function alkaline_earth()
{
if (($rowC['description'] == " Alkali earth Metal")) {
echo ' this is an alkali earth metal';
} else {
echo 'metal is unknown';
}
}
//end of class
}
// create an object for class name
$abc = new metals();
$abcd = new metals();
// call the functions in the class
$abc->alkali();
$abcd->alkaline_earth();
?>
A shorter and more elegant way to achieve the wanted result is:
<?php
class metals {
// Consider defining this as a static function if $rowC is not
// intended to be a member of the class `metals`.
function isMatch($search) {
return $rowC['description'] == $search;
}
}
$abc = new metals();
$searches = array('Alkali Metal', 'Alkali Earth Metal', 'Some Other Metal');
foreach ($searches as $search) {
if ($abc->isMatch($search)) {
echo 'Match found: ' . $search;
break;
}
}
?>
The loop will output the first match and then exit.