Question: how to draw the capital T using PHP?

Discussion in 'PHP' started by sunnyboy1984, Feb 22, 2007.

  1. #1
    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>";

    ?>
     
    sunnyboy1984, Feb 22, 2007 IP
  2. SilkySmooth

    SilkySmooth Well-Known Member

    Messages:
    1,583
    Likes Received:
    269
    Best Answers:
    0
    Trophy Points:
    180
    #2
    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?
     
    SilkySmooth, Feb 22, 2007 IP
  3. sunnyboy1984

    sunnyboy1984 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
     

    Attached Files:

    sunnyboy1984, Feb 23, 2007 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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:
     
    TwistMyArm, Feb 23, 2007 IP
  5. sunnyboy1984

    sunnyboy1984 Peon

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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;
     
    sunnyboy1984, Feb 23, 2007 IP
  6. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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...
     
    TwistMyArm, Feb 23, 2007 IP