简体   繁体   English

为什么我的if,elseif,else语句不起作用?

[英]Why isn't my if, elseif, else statement working?

I'm working on a Mad Libs type assignment. 我正在研究Mad Libs类型分配。 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. 我试图让男主人公决定我的教授在故事中使用的青蛙的名字,但由于某种原因,我的重命名青蛙的if语句不起作用。

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? 每次我尝试输入英雄名称时(当要求输入“你的名字”时,它总是默认使用泰勒并将亨利作为青蛙名。我如何使此if-else语句正确工作以使名称成为亨利仅当输入泰勒或杰米作为英雄名称时?

HTML 的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

<?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";
?>

You're doing assignment instead of comparison. 您正在做分​​配而不是比较。 Change if($heroname="Taylor"): to if($heroname=="Taylor"): (ie = should be == ). if($heroname="Taylor"):更改为if($heroname=="Taylor"):=应为== )。

(Note: You need to fix that in all four conditions, not just the first one.) (注意:您需要在所有四个条件下解决此问题,而不仅仅是第一个条件。)

You're using the assignment operator (=) not the comparison operator(==/===) 您使用的是赋值运算符(=),而不是比较运算符(== / ===)

if($heroname="Taylor"):
$frogname="Henry";
elseif($heroname="taylor"):
$frogname="Henry";
elseif($heroname="Jamie"):
$frogname="Henry";
elseif($heroname="jamie"):
$frogname="Henry";
else: $frogname="Matthew";
endif;

to

if($heroname == "Taylor")
    $frogname = "Henry";
elseif($heroname == "taylor")
    $frogname = "Henry";
elseif($heroname == "Jamie")
    $frogname = "Henry";
elseif($heroname == "jamie")
    $frogname = "Henry";
else
    $frogname = "Matthew";

Even better: 更好的是:

switch($heroname){
    case 'Taylor':
    case 'taylor':
    case 'Jamie':
    case 'jamie':
        $frogname = 'Henry';
        break;
    default:
        $frogname = 'Matthew';
}

Here is something practical. 这是实用的东西。 Try it this way: 尝试这种方式:

if (in_array(strtolower($heroname), array("taylor", "jamie"))) $frogname="Henry";
else $frogname=="Matthew";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM