我正在进行Mad Libs类型的分配。我试图让heroname决定我的教授让我在故事中使用的青蛙的名字,但由于某种原因,我重命名青蛙的if语句不起作用。
每当我尝试输入heroname的名字时(当它要求“你的名字”时,它总是最终默认为Taylor并将亨利作为frogname。我怎样才能使这个if-else语句正常工作以使只有当泰勒或杰米为了这个名字而进入时,名字才是亨利?
HTML
<!DOCTYPE html>
<html>
<head>
<title>Mad Libs Time</title>
</head>
<body>
<h1>MAD LIBS TIME, HOMIE!</h1>
<p>Y'arr, this be a tale all about the lives of ye and yer pirate mateys. Full of adventure, loss, love, and magic this story be. In order to tell it to ye the way ya like, answer a few questions before I throw ye in the brig!</p>
<form name="gatheringinfo" method="post" action="tellthatstory.php">
Aye, what's yer name? <input type="text" name="heroname"><br/>
What be yer best matey's name? <input type="text" name="friendname"><br/>
Name the wizard of this tale: <input type="text" name="wizardname"><br/>
What be the wizard's power?<select name="power">
<option value="fire">Fire</option>
<option value="earth">Earth</option>
<option value="water">Water</option>
</select><br/>
Pick a number, 1 to 10..<select name="pickanumber">
<option value="one">1</option>
<option value="two">2</option>
<option value="three">3</option>
<option value="four">4</option>
<option value="five">5</option>
<option value="six">6</option>
<option value="seven">7</option>
<option value="eight">8</option>
<option value="nine">9</option>
<option value="ten">10</option>
</select><br/>
And lastly, before we get started, pick ye weapon...<select name="weapon">
<option value="scimitar">Scimitar</option>
<option value="musket">Musket</option>
<option value="dagger">Dagger</option>
</select><br/>
<input type="submit" name="submit" value="Spin Me A Tale"/>
</form>
</body>
</html>
PHP
<?php
$heroname=$_POST['heroname'];
$friendname=$_POST['friendname'];
$wizardname=$_POST['wizardname'];
$power=$_POST['power'];
$pickanumber=$_POST['pickanumber'];
$weapon=$_POST['weapon'];
switch ($power) {
case "fire":
$power="Fire";
$fight_sentence=2;
break;
case "earth":
$power="Earth";
$fight_sentence=4;
break;
case "water":
$power="Water";
$fight_sentence=6;
break;
}
$frogstory = array("", "", "", "", "", "", "", "", "", "");
switch ($pickanumber) {
case "one":
$reasonforfrog="frogstory[0]";
break;
case "two":
$reasonforfrog="frogstory[1]";
break;
case "three":
$reasonforfrog="frogstory[2]";
break;
case "four":
$reasonforfrog="frogstory[3]";
break;
case "five":
$reasonforfrog="frogstory[4]";
break;
case "six":
$reasonforfrog="frogstory[5]";
break;
case "seven":
$reasonforfrog="frogstory[6]";
break;
case "eight":
$reasonforfrog="frogstory[7]";
break;
case "nine":
$reasonforfrog="frogstory[8]";
break;
case "ten":
$reasonforfrog="frogstory[9]";
break;
}
switch ($weapon) {
case "scimitar":
$weapon="scimitar";
break;
case "musket":
$weapon="musket";
break;
case "dagger":
$weapon="dagger";
break;
}
if($heroname="Taylor"):
$frogname="Henry";
elseif($heroname="taylor"):
$frogname="Henry";
elseif($heroname="Jamie"):
$frogname="Henry";
elseif($heroname="jamie"):
$frogname="Henry";
else: $frogname="Matthew";
endif;
echo " The day the fearless pirate $heroname and the fearless friend $friendname took their first step offshore, they never knew the adventures the future would hold for them both. They plundered and pillaged for years and years, always getting their fill of the booty and rum that flowed effortlessly into their lives. Just as they were getting used to all of the excitement.. just as they were about to cash out.. just as they were about to transfer back to life on land.. they were approached by their superior, Captain Rumbeard.<br/>
He told $heroname and $friendname that he was visited by the wizard $wizardname in a dream and was told where the biggest treasure trove he'd ever heard of lied in wait. $frogname";
?>
答案 0 :(得分:4)
你正在做作业而不是比较。将if($heroname="Taylor"):
更改为if($heroname=="Taylor"):
(即=
应为==
)。
(注意:你需要在所有四个条件中修复它,而不仅仅是第一个条件。)
答案 1 :(得分:1)
您使用的是赋值运算符(=)而不是比较运算符(== / ===)
if($heroname="Taylor"):
$frogname="Henry";
elseif($heroname="taylor"):
$frogname="Henry";
elseif($heroname="Jamie"):
$frogname="Henry";
elseif($heroname="jamie"):
$frogname="Henry";
else: $frogname="Matthew";
endif;
到
if($heroname == "Taylor")
$frogname = "Henry";
elseif($heroname == "taylor")
$frogname = "Henry";
elseif($heroname == "Jamie")
$frogname = "Henry";
elseif($heroname == "jamie")
$frogname = "Henry";
else
$frogname = "Matthew";
更好:
switch($heroname){
case 'Taylor':
case 'taylor':
case 'Jamie':
case 'jamie':
$frogname = 'Henry';
break;
default:
$frogname = 'Matthew';
}
答案 2 :(得分:0)
这是实用的东西。试试这种方式:
if (in_array(strtolower($heroname), array("taylor", "jamie"))) $frogname="Henry";
else $frogname=="Matthew";