0

Below is a code for a kids' webpage, and the program should print a Christmas tree (with optional decorations, code not yet ready for those). When I run it, however, I get an error message saying "Undefined variable: h on line 60". How can I define it?

And another one, why doesn't my for-loop work? It should print out the following way depending on chosen height: * * ***



...and so on. Now (testing) it obviously prints nothing, and if i manually put it printing 5 times, it puts everything on one line. So I am missing the line feed too, but I don't know how to write it.

Thank you!

<html>
<head><title>Christmas Tree Shop</title></head>
<body>
<form>
        echo = "How long a tree would you like to buy (3-12)? "
        
        <input type = "int"  name = "height" placeholder = "Height (3-12)">
    <br>
    <br>
    <select name = "candle" placeholder = "candles (í) ">
        <option>Yes</option>
        <option>No</option>
    </select>
        
    <select name = "bauble" placeholder = "baubles (@) ">
        <option>Yes</option>
        <option>No</option>
    </select>
    
    <select name = "pretzel" placeholder = "pretzels (&) ">
        <option>Yes</option>
        <option>No</option>
    </select>
    <button type = "submit" name = "submit" type = "submit">Purchase</button>

</form>
<p>Here is your tree!</p>

<?php

    $candle = "í";
    $bauble = "@";
    $pretzel = "&";
    
    if(isset($_GET['submit'])){
       $h = $_GET['height'];
       $candles = $_GET['candle'];
       $baubles = $_GET['bauble'];
       $pretzels = $_GET['pretzel'];
    }
    
    $decorations = array();
    switch ($candle){
        case "Yes":
            $decorations = "í";
    }
    switch ($bauble){
        case "Yes":
            $decorations = "@";
    }
    switch ($pretzel){
        case "Yes":
            $decorations = "&";
    }
    
    $line = $h;
    $star = 1;
    
    echo str_repeat('&nbsp;',$h) . '*' . str_repeat('&nbsp;', $h);

    for ($i = 0; $i < $h ; $i++) {
      echo nl2br(str_repeat($i, '*'));
    }
    
    
?>
</body>
</html>
1
  • $h will only be defined/exist if the form is submitted (since you only define it inside that if-statement). You should define it and give it a default value before it, just like you've done with your other variables. Commented Jun 20, 2021 at 14:05

1 Answer 1

1

i checked your code $h undefined because you did not define it as you defined others like, i changed the code check this code.

<?php

$candle = "í";
$bauble = "@";
$pretzel = "&";
$h = '100'; //what ever height value you need to set its , here i put 100 for example

if(isset($_GET['submit'])){
   $h = $_GET['height'];
   $candles = $_GET['candle'];
   $baubles = $_GET['bauble'];
   $pretzels = $_GET['pretzel'];
}
$decorations = array();
switch ($candle){
    case "Yes":
        $decorations = "í";
}
switch ($bauble){
    case "Yes":
        $decorations = "@";
}
switch ($pretzel){
    case "Yes":
        $decorations = "&";
}

$line = $h;
$star = 1;

echo str_repeat('&nbsp;',$h) . '*' . str_repeat('&nbsp;', $h);

for ($i = 0; $i < $h ; $i++) {
  echo nl2br(str_repeat($i, '*'));
}

?>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.