-1

I'm working on a Mad Libs type assignment. I'm trying to have the heroname dictate the name of the frog my professor is making me use in the story, but for some reason, my if statement to rename the frog isn't working.

Every time I try to enter names for heroname (when it asks for "your name", it always ends up defaulting to Taylor and putting Henry as the frogname. How can I make this if-else statement work correctly to make the name be Henry only if Taylor or Jamie are entered for the heroname?

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 "&nbsp;&nbsp;&nbsp;&nbsp;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/>
&nbsp;&nbsp;&nbsp;&nbsp;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";
?>
4

3 回答 3

4

你在做分配而不是比较。更改if($heroname="Taylor"):if($heroname=="Taylor"):(即=应该是==)。

(注意:您需要在所有四种情况下都解决这个问题,而不仅仅是第一个。)

于 2013-09-13T02:39:37.617 回答
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';
}
于 2013-09-13T02:41:36.293 回答
0

这里有一些实用的东西。试试这种方式:

if (in_array(strtolower($heroname), array("taylor", "jamie"))) $frogname="Henry";
else $frogname=="Matthew";
于 2013-09-13T02:46:23.657 回答