Reputation: 37
I have php code as below:
if($ptp_car_1 == "ptp_Denpasar_Ubud_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Sanur_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Seminyak_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Kuta_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Jimbaran_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
if($ptp_car_1 == "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
// many code over 1,000
How can i optimize it ? I want the page run faster, now is too slow for it.
Upvotes: 1
Views: 1521
Reputation: 184
This function will directly convert the strings as long as the other 1000+ strings follow the same pattern.
// reorder parts if the following pattern is true for all entries
//
// starts with "ptp" (store as group 1)
// then contains an '_'
// then has at least 1 char that is not an '_' (store as group 2)
// then contains an '_'
// then contains at least 1 char that is not '_fee_' (store as group 3)
// then contains an '_'
// then contains any number of chars (store as group 4)
//
// group 4 will contain 'fee_', $way and '_car_1' but we really don't need to care what
// those chars are, they just get tacked on to the end.
//
// Once the original is broken up, parts 2 & 3 are swapped and the
// results are imploded into the desired string with '_' separators.
//
function swapPtpParts($ptp_car_1) {
$matches = [];
if(preg_match("/(ptp)_([^_]+)_(.+?(?=_fee_))_(.*)/", $ptp_car_1, $matches)) {
$parts = [
$matches[1],
$matches[3],
$matches[2],
$matches[4],
];
return implode('_', $parts);
}
return $ptp_car_1;
}
Usage:
$ptp_car_1 = swapPtpParts($ptp_car_1);
Upvotes: 4
Reputation: 99
How about using array ?
function getResult($key){
$way = 'hello';
$array = [
'ptp_Denpasar_Ubud_fee_'.$way.'_car_1' => "ptp_Ubud_Denpasar_fee_".$way."_car_1",
"ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1" => "ptp_Sanur_Denpasar_fee_".$way."_car_1",
];
return $array[$key];
}
echo getResult('ptp_Denpasar_Ubud_fee_hello_car_1');
Upvotes: 0
Reputation:
I haven't seen all 1000 if statements but if the patten holds, you can use a function that replaces the text for you. As before:
function convertDenpansarFeeString($ptp_car, $way)
{
// number of characters in ptp_Denpasar_ = 13
// number of characters in _car_ = 5
return "ptp_"
. substr($ptp_car
, 13
, strpos($ptp_car, "_fee_") - 13
)
. "_Denpasar_fee_"
. $way
. "_car_"
. substr($ptp_car
, strpos($ptp_car, "_car_") + 5
);
}
And use like so:
$ptp_car1 = convertDenpansarFeeString($ptp_car1, $way);
$ptp_car2 = convertDenpansarFeeString($ptp_car2, $way);
Upvotes: 0
Reputation: 1368
in this case, SWITCH CASE should be better.
go through this...
Why the switch statement and not if-else?
Is "else if" faster than "switch() case"?
give a try to this,
switch ($ptp_car_1){
case "ptp_Denpasar_Ubud_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Sanur_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Seminyak_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Kuta_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Jimbaran_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
break;
case "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1":
{
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
break;
/*
//OPTIONAL
default:
$ptp_car_1 = $ptp_car_1;
*/
}
Upvotes: 0
Reputation: 413
Use else if like below:
if($ptp_car_1 == "ptp_Denpasar_Ubud_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Ubud_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Tanah_Lot_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Tanah_Lot_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Sanur_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Sanur_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Seminyak_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Seminyak_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Kuta_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Kuta_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Jimbaran_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Jimbaran_Denpasar_fee_".$way."_car_1";
}
elseif($ptp_car_1 == "ptp_Denpasar_Nusa_Dua_fee_".$way."_car_1"){
$ptp_car_1 = "ptp_Nusa_Dua_Denpasar_fee_".$way."_car_1";
}
It would be faster.
Upvotes: 0