如何用函数替换这些简单的if-else PHP语句?

如何用函数替换这些简单的if-else PHP语句?,php,if-statement,optimization,Php,If Statement,Optimization,我如何改进或替换这些if-else PHP语句,使其成为一个函数,该函数可以为(例如images/dogtypeX.jpg)接受一个文件名变量,而不是对每个狗类型进行硬编码?感谢所有PHP高手 header('Content-type: image/jpg'); $dogtype = $_GET["dogtype"]; $dogname = $_GET["dogname"]; if($dogtype== "dogtype1") { $

我如何改进或替换这些if-else PHP语句,使其成为一个函数,该函数可以为(例如images/dogtypeX.jpg)接受一个文件名变量,而不是对每个狗类型进行硬编码?感谢所有PHP高手

header('Content-type: image/jpg');
$dogtype = $_GET["dogtype"];
$dogname = $_GET["dogname"];

if($dogtype== "dogtype1")
{ $jpg_image = imagecreatefromjpeg('images/dogtype1.jpg');}
else if ($dogtype== "dogtype2")
{ $jpg_image = imagecreatefromjpeg('images/dogtype2.jpg');}
else if ($dogtype=="dogtype3") 
{ $jpg_image = imagecreatefromjpeg('images/dogtype3.jpg');}
else if ($dogtype=="dogtype4")
{ $jpg_image = imagecreatefromjpeg('images/dogtype4.jpg');}
else if ($dogtype=="dogtype5")
{ $jpg_image = imagecreatefromjpeg('images/dogtype5.jpg');}
imagettftext($jpg_image, $dogtype, $dogname);
imagejpeg($jpg_image);

在您的情况下,您似乎可以做一些非常简单的事情,例如:

header('Content-type: image/jpg');
$dogtype = $_GET["dogtype"];
$dogname = $_GET["dogname"];
supported_dogtypes = array("dogtype1", "dogtype2", "dogtype3"); // ...
if(in_array($dogtype, supported_dogtypes){
    $jpg_image = imagecreatefromjpeg("images/$dogtype.jpg");
    imagettftext($jpg_image, $dogtype, $dogname);
    imagejpeg($jpg_image);
}

在您的情况下,您似乎可以做一些非常简单的事情,例如:

header('Content-type: image/jpg');
$dogtype = $_GET["dogtype"];
$dogname = $_GET["dogname"];
supported_dogtypes = array("dogtype1", "dogtype2", "dogtype3"); // ...
if(in_array($dogtype, supported_dogtypes){
    $jpg_image = imagecreatefromjpeg("images/$dogtype.jpg");
    imagettftext($jpg_image, $dogtype, $dogname);
    imagejpeg($jpg_image);
}

只需在函数中传递文件名即可

function dogType($dogtype){
   $jpg_image = imagecreatefromjpeg("images/$dogtype.jpg");
// DO your other stuf..
}

//function call
$dogtype = $_GET["dogtype"];
dogType($dogtype);

只需在函数中传递文件名即可

function dogType($dogtype){
   $jpg_image = imagecreatefromjpeg("images/$dogtype.jpg");
// DO your other stuf..
}

//function call
$dogtype = $_GET["dogtype"];
dogType($dogtype);

您可以使用preg_match匹配“狗型”的编号,然后自动指定应基于该编号创建的图像

header('Content-type: image/jpg');
$dogtype = $_GET["dogtype"];
$dogname = $_GET["dogname"];

$dog_type_number = preg_match('/\d+/', $dogtype, $matches, PREG_OFFSET_CAPTURE);
$dog_type_image = 'dogtype' . $matches[0][0];

$jpg_image = imagecreatefromjpeg("images/$dog_type_image.jpg");
imagettftext($jpg_image, $dogtype, $dogname);
imagejpeg($jpg_image);

例如,如果图像名称为“dogtype124”,则preg_match将仅查找数字,即“124”,然后自动将其附加在末尾。

您可以使用preg_match匹配“dogtype124”的编号,然后自动指定应基于该编号创建的图像

header('Content-type: image/jpg');
$dogtype = $_GET["dogtype"];
$dogname = $_GET["dogname"];

$dog_type_number = preg_match('/\d+/', $dogtype, $matches, PREG_OFFSET_CAPTURE);
$dog_type_image = 'dogtype' . $matches[0][0];

$jpg_image = imagecreatefromjpeg("images/$dog_type_image.jpg");
imagettftext($jpg_image, $dogtype, $dogname);
imagejpeg($jpg_image);

例如,如果图像名称为“dogtype124”,则preg_match将只找到数字,即“124”,然后自动将它们附加在末尾。

@Rookie PHP如果您最终使用我的代码,您能接受答案吗?Thanks@Rookie你能接受我的回答吗?我真的很感激。只需点击我答案左边的“检查”。@新手PHP如果你最终使用我的代码,你能接受答案吗?Thanks@Rookie你能接受我的回答吗?我真的很感激。只需点击我答案左边的“检查”。