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(' ',$h) . '*' . str_repeat(' ', $h);
for ($i = 0; $i < $h ; $i++) {
echo nl2br(str_repeat($i, '*'));
}
?>
</body>
</html>
$hwill 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.