简体   繁体   中英

Introduction to php

I am studying php basics and am having difficulties getting this exercise right please any body help? I appreciate your help in advance Daniel.

Note that I am only allowed to do the exercise using if...elseif...else-statement

Write a PHP script that prints a statement like below based on the information inputted in the form. male 0-55 years: "You're a man in his prime!" male over 55 years: "You are a wise man!" female 0-55 years: "You are a damsel at her most beautiful!" female over 55 years: "You look young for your age!".

The form that sends the information looks like this:

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

Example output

You’re a man in his prime!

My scripts is:

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

?> 

You access your global GET variables the wrong way:

$gender = $_GET['gender'];
$age = $_GET['age'];

if($gender == 'male' && $age == '0-55')
    echo "You’re a man in his prime!";

Now adapt this for the rest of your code.

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

You forgot the use the age radio button.. And you're if else statement aren't correct use like this. In your If statements you aren't using the age of this 'person' as you can see, the code shall never work because there is no link between '55' and you're option. Your $male and $female
is weird, they have no value.. The cause of this,

<input type="radio" value="male" name="gender"

You need to use 'name="gender"' instead of 'value="male/female"'

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

I hope you'll help a little with the analysis of this task (also apologize for my clumsy English).

Firstly the variables you want to get in his PHP script have names that you specify for the "name" tag the "input"

Now when getting values ​​can first check the existence of a variable if (isset($_GET['gender'])) and then perform all your operations with application instructions switch

Here is the sample code:

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

?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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