Php 在Else If语句中使用数组
只是需要一些个人学习项目的帮助。我搜索过很多类似的帖子,但都没有找到好运气 目标是通过在下面的elseif语句中使用一个数组来缩短代码(而不是让40多个elseif重复相同的内容) 下面的代码是有效的,如果我拼出每个if语句,我只是想改进它Php 在Else If语句中使用数组,php,arrays,if-statement,Php,Arrays,If Statement,只是需要一些个人学习项目的帮助。我搜索过很多类似的帖子,但都没有找到好运气 目标是通过在下面的elseif语句中使用一个数组来缩短代码(而不是让40多个elseif重复相同的内容) 下面的代码是有效的,如果我拼出每个if语句,我只是想改进它 <?php $affiliate1 = array('productA', 'productB', 'productC', 'productD', 'ProductE', 'productF'); ?> <?php $affiliate2
<?php
$affiliate1 = array('productA', 'productB', 'productC', 'productD', 'ProductE', 'productF');
?>
<?php
$affiliate2 = array('productG', 'productH', 'productI', 'productJ', 'ProductK', 'productL');
?>
<?php $returnaddress = $_POST['product_name']; ?>
<?php if ($returnaddress == "$affiliate1") $returnaddress = 'Address1';
elseif ($returnaddress == "$affiliate2") $returnaddress = 'Address2';
?>
<?php echo $returnaddress;?>
非常感谢您的帮助/解释!我已经搜索了几个小时,但还没有找到足够具体的例子。感谢肖恩指出这一点!我需要在_数组中使用,我不知道它的存在 这是他提供的工作代码
<?php
$affiliate1 = array('productA', 'productB', 'productC', 'productD', 'ProductE', 'productF');
?>
<?php
$affiliate2 = array('productG', 'productH', 'productI', 'productJ', 'ProductK', 'productL');
?>
if (in_array($returnaddress, $affiliate1)) $returnaddress = 'Address1';
elseif (in_array($returnaddress, $affiliate2)) $returnaddress = 'Address2';
如果(在数组中($returnaddress,$affiliate1))$returnaddress='Address1';
elseif(在数组中($returnaddress,$affiliate2))$returnaddress='Address2';
您正在寻找->如果(在数组中($returnaddress,$affiliate1))$returnaddress='Address1';elseif(在数组中($returnaddress,$affiliate2))$returnaddress='Address2'代码>?为什么$affiliate1介于“在您的代码中?为什么不使用?不确定为什么在引号中有$affiliate1和$affiliate2。”。试着用php搜索数组中的匹配项。我学习php才一个月——因此所有的语法错误。我正在研究in_阵列!只是一个提示——如果您正在编写一个仅为php的脚本,那么不需要每行打开和关闭
声明;只要用开始脚本,谢谢!知道了这一点,您可能会对完整的代码感到畏缩。我会开始调查的。无论如何,这应该可以节省你一点打字时间!:D