7
How to optimize this code to make it faster?
if (strpos($qt, "blood") !== FALSE){
if (preg_match("/^blood (?<blood>.+)$/", $qt, $match)){
switch ($match['blood']) {
case "a+":
$result = "A+";
$sndline = "Ideal donor: A+<br>Other donors: A+ or O+<br>Only if no Rh(+) found: A- or O-";
break;
case "a-":
$result = "A-";
$sndline = "Ideal donor: A-<br>Other donors: A- or O-";
break;
case "b+":
$result = "B+";
$sndline = "Ideal donor: B+<br>Other donors: B+ or O+<br>Only if no Rh(+) found: B- or O-";
break;
case "b-":
$result = "B-";
$sndline = "Ideal donor: B-<br>Other donors: B- or O-";
break;
case "ab+":
$result = "AB+";
$sndline = "Ideal donor: AB+<br>Other donors: AB+ or A+ or B+ or O+<br>Only if no Rh(+) found: AB- or A- or B- or O-";
break;
case "ab-":
$result = "AB-";
$sndline = "Ideal donor: AB-<br>Other donors: AB- or A- or B- or O-";
break;
case "o-":
$result = "O-";
$sndline = "Ideal donor: O-<br>Other donors: O-";
break;
case "o+":
$result = "O+";
$sndline = "Ideal donor: O+<br>Other donors: O+<br>Only if no Rh(+) found: O-";
break;
}
}
}
What is the purpose of this code? what content is in the variable
$qt
? letters, numbers, special characters, etc..– stderr
you ever heard of design patterns? thought of creating a Factory?
– Israel Zebulon
@qmechanik The variable
$qt
can get all this you said, but in this case, she gets the text:blood a+
.– hsbpedro
@Israelzebulon I’ve never heard of it (I’m still new to PHP). Could you give me an example?
– hsbpedro
@hsbpedro The variable Qt
pode vir nesse formato
texto_blooda+_outrotextobla`, no spaces? or there will always be spaces?– stderr
@qmechanik What I am creating is a text interpreter. In this case, I want when the user type
blood <...>
, the interpreter takes what is in the group...
and print the message in question.– hsbpedro