我正在学习php基础知识,并且在练习这项练习时遇到了困难 请帮忙吗?我提前感谢您的帮助 丹尼尔。
请注意,我只允许使用 if ... elseif ... else-statement
进行练习编写一个PHP脚本,根据表单中输入的信息打印如下所示的语句。 男性0-55岁:“你是一个鼎盛时期的男人!” 超过55岁的男性:“你是个聪明人!” 0-55岁的女性:“你最美丽的女人!” 超过55岁的女性:“你的年龄看起来很年轻!”。
发送信息的表单如下所示:
<form action="printinfo.php" method="get">
Choose your gender: <input type="radio" value="male" name="gender" checked>male
<input type="radio" value="female" name="gender">female
<br>
Write your age:
<select name="age">
<option value=1 selected>0-55</option>
<option value=2>Over 55</option>
</select>
<br>
<input type="submit" value="Send">
</form>
示例输出
You’re a man in his prime!
我的脚本是:
<?php
$male=$_GET["male"];
$female=$_GET["female"];
if($male > 55){
echo "You’re a man in his prime!";
}else if ($male <= 55){
echo "You are a wise man!";
}else if ($female <= 55){
echo "You are a damsel at her most beautiful!";
}else if ($female > 55){
echo "You look young for your age!";
?>
答案 0 :(得分:5)
您以错误的方式访问全局GET变量:
$gender = $_GET['gender'];
$age = $_GET['age'];
if($gender == 'male' && $age == '0-55')
echo "You’re a man in his prime!";
现在将其改为代码的其余部分。
答案 1 :(得分:2)
<?php
$gender = $_GET["gender"];
$age=$_GET["age"];
if($gender == 'male' && $age == 2)
{
echo "You’re a man in his prime!";
}
else if ($gender == 'male' && $age == 1)
{
echo "You are a wise man!";
}
else if ($gender == 'female' && $age == 1)
{
echo "You are a damsel at her most beautiful!";
}
else if ($gender == 'female' && $age == 2)
{
echo "You look young for your age!";
}
?>
HTML:
<html>
<form action="printinfo.php" method="get">
Choose your gender: <input type="radio" value="male" name="gender" checked>male
<input type="radio" value="female" name="gender">female
<br>
Write your age:
<select name="age">
<option value=1 selected>0-55</option>
<option value=2>Over 55</option>
</select>
<br>
<input type="submit" value="Send">
</form>
</html>
你忘了使用年龄单选按钮.. 而你是否if语句不正确使用这样。 在你的If语句中你没有使用这个'人'的年龄,你可以看到,代码永远不会起作用,因为'55'和你之间没有选择。你的男性和女性 很奇怪,他们没有价值。原因,
<input type="radio" value="male" name="gender"
您需要使用'name =“gender”'而不是'value =“男/女”'
答案 2 :(得分:0)
<?php
if(isset($_GET['gender'])){
$gender=$_GET['gender'];
}
if($gender = 'male' && $_GET['age'] > 55){
echo "You’re a man in his prime!";
}else if ($gender = 'male' && $_GET['age'] <= 55){
echo "You are a wise man!";
}else if ($gender = 'female' && $_GET['age'] <= 55){
echo "You are a damsel at her most beautiful!";
}else if ($gender = 'female' && $_GET['age'] > 55){
echo "You look young for your age!";
}
?>
答案 3 :(得分:-1)
我希望你能对这项任务的分析有所帮助 (也为我笨拙的英语道歉)。
首先,您希望在PHP脚本中获取的变量具有您为“name”标记指定的名称“input”
现在获取值时可以先检查变量if (isset($_GET['gender']))
是否存在
然后使用应用程序说明切换
以下是示例代码:
<?php
//new variable for age
$age_human = $_GET['age'];
// for example
//if (!isset($age_human, $_GET['gender'])) die "Empty value";
// male or female
switch($_GET['gender'])
{
case "male": echo "male ";
if($age_human > 55)
{
echo "You’re a man in his prime!";
}
else if ($age_human <= 55)
{
echo "You are a wise man!";
}
else if ($age_human <= 55)
{
echo "You are a damsel at her most beautiful!";
}
else if($age_human > 55)
{
echo "You look young for your age!";
}
break;
case "female": echo "female ";
if($age_human > 55)
{
echo "You’re a man in his prime!";
}
else if ($age_human <= 55)
{
echo "You are a wise man!";
}
else if ($age_human <= 55)
{
echo "You are a damsel at her most beautiful!";
}
else if($age_human > 55)
{
echo "You look young for your age!";
}
break;
}
?>