Hi everyone: I am new to PHP and I got a question which is how to use for loops, while loops, and/or control structures to do a 10 x 15 design of the capital letter "T". But the "T" is not the one you look here exactly,(the page can show the "T" only like this), it should have a short line at the bottom and two short vertical lines on both the end left and right sides under the first line on the top, if you know what I mean. Thanks in advance. The following is my current code: (this draws the T you see here but not the one I want) <?php #pre tags keep the asterisks more uniformly spaced echo "<pre>"; for ($row = 0; $row < 15; $row++) { for ($column = 0; $column <10; $column++) { if (($row < 1 || $row > 15) ||( $column == 4)) { echo "*"; } else echo " "; } echo "\n"; //the newline character starts the next row } echo "</pre>"; ?>
Its hard to draw something unless you know what it looks like, unfortunately your the only person who currently knows what the T should look like. Maybe if you create a gif of what it should be like and upload that somewhere so we can take a look?
Hi: Please check the picture and refer to my current php code, you will notice the differences and what I mean. Please try my php code first. Thank you
You'll have to excuse me reformatting the code a bit, but this should work... <?php #pre tags keep the asterisks more uniformly spaced echo "<pre>"; for ($row = 0; $row < 15; $row++) { for ($column = 0; $column <10; $column++) { $draw_asterisk = false; // top row if ( $row == 0 ) { $draw_asterisk = true; } // column if ( $column == 4 ) { $draw_asterisk = true; } // top sides if ( ( $row < 4 ) && ( ( $column == 0 ) || ( $column == 9 ) ) ) { $draw_asterisk = true; } // bottom if ( ( $row == 14 ) && ( $column > 1 ) && ( $column < 7 ) ) { $draw_asterisk = true; } if ( $draw_asterisk ) { echo "*"; } else echo " "; } echo "\n"; //the newline character starts the next row } echo "</pre>"; ?> PHP:
Hi: Thanks for this indeed. Any chance to explain a bit more a bout the code. What is the asterisk? and can you explain a little more. I put my questions in the code as comment Thank you. //why made this = false at the beginning? is $draw_asterisk a variable here? $draw_asterisk = false; // why row==0 here? because php begins the count from 0, right? if ( $row == 0 ) { // $draw_asterisk = true; } // column if ( $column == 4 ) { $draw_asterisk = true; } // top sides // why "&&" between $column == 0 and $column == 9 won't work? if ( ( $row < 4 ) && ( ( $column == 0 ) || ( $column == 9 ) ) ) { $draw_asterisk = true; } // bottom if ( ( $row == 14 ) && ( $column > 1 ) && ( $column < 7 ) ) { $draw_asterisk = true;
Well, I'm sure you know what an asterisk is: you had it in your original comments! Essentially the $draw_asterisk variable will define whether or not to show the asterisk (the '*') for each row / column position (via the loops). By default, we don't want to show an asterisk in the current position... the following tests will define if we actually do want to show an asterisk. The double equal is a comparison, not an assignment. I'm testing if $row is 0, not setting it to 0. Essentially, if this is the first row, then we will always want to draw an asterisk, no matter what the column position. The '&&' means AND, the '||' means OR. You can't have '( $column == 0 ) && ( $column == 9 )' because that is essentially saying 'if $column IS 0 and $column IS 9'. Totally not possible. However, by using the brackets in the way we did, that if statement basically reads 'if $row is less than 4 AND if $row is either 0 or 9...'. So that will draw those little tips on the top row. Hope that makes more sense...