PHP中的函数
我正在与一个php函数作斗争 我试图创建一个接受5个参数的函数。 用户需要选择他们选择的活动,带有他们选择的活动的消息应该回显给用户 我在函数中使用了数组和foreach语句,但它不起作用 请帮忙PHP中的函数,php,Php,我正在与一个php函数作斗争 我试图创建一个接受5个参数的函数。 用户需要选择他们选择的活动,带有他们选择的活动的消息应该回显给用户 我在函数中使用了数组和foreach语句,但它不起作用 请帮忙 <?php // Declare the function and variables error_reporting (0); $msg = ""; $activity = ""; function activites ($paintballing , $ice_skating, $hor
<?php
// Declare the function and variables
error_reporting (0);
$msg = "";
$activity = "";
function activites ($paintballing , $ice_skating, $horse_riding, $para_gliding, $water_rafting ){
// Make the activity array
$activites = array (1=> 'Paintballing', 'Ice_skating', 'Horse_riding', 'para_gliding', 'water_rafting');
// make the activity pull down menu
echo '<select name ="activity">';
// use a foreach loop to loop through the activity
foreach ($activites as $key =>$value) {
echo "<option value \"key\"$key\">$value</option>\n";
}
}
if ($activity == ""){
$msg = "<font color = 'red'> Please select an activity from the drop down menu</font>";
}
?>
<html>
<head>
<title>Function </title>
</head>
<body>
<center> <a href="index.php">Home </a> | <a href ="calc.php">Calc</a> | <a href="string.php">String</a> | <a href ="array.php"> Array </a> | <a href = "function.php">Function </a></center>
</center>
<form name = 'function' method = "POST" action = 'function.php' >
<p>Select one of the activity you would like to do today:</p>
<select name="Activity" size="5">
<option selected value = "">
<option> Paintballing </option>
<option> Ice-skating </option>
<option> Horse-riding </option>
<option> Para-gliding </option>
<option> Water-rafting </option>
</select>
</form>
<input type = "submit" name = "submit" value = "submit" />
</option>
<?php echo $msg;?>
<p align ="center"><a href ="function.txt" Onclick ="window.open ('function.txt', 'function', resizable =no, height=500, width=500, scrollercars=yes'); retrun false;">Function.txt</a></p>
尝试调用该函数
if ($_POST['submit']) {
echo activities($_POST['Activity']);
}
你的函数被修改了
function activities($activity) {
$activities = array(
'Paintballing',
'Ice_skating',
'Horse_riding',
'para_gliding',
'water_rafting'
);
$activity = trim($activity);
if (! in_array($activity, $activities))
return false;
return sprintf("You have selected : %s", $activity);
}
您的错误消息是什么?
$activites=array(1=>$paintballing、$ice\u sling、$mars\u riding'、$para\u slideng'、$water\u rafting')你真的在某处调用了这个函数吗?@Allendar说这是不正确的语法。需要$activites=array('Paintballing'),…
或$activites=array(1=>array('Paintballing',…
此外,您应该会收到错误消息否?他的数组按照他定义的方式很好。他只是设置了第一个数字键,之后的键将是2、3、4。他只是从1开始初始化数组,而不是从0开始。另外,活动
是活动
拼写错误,您可能需要修复虽然你的代码很小。@PedroCordeiro..是的,我知道…我应该自己更正它…:)