1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Practicing

Discussion in 'HTML, Graphics & Programming' started by Craig321, 5 Mar 2006.

  1. Craig321

    Capodecina

    Joined: 2 May 2004

    Posts: 19,974

    Hi,

    Any ideas what I can do to practice some PHP? I want to learn more ;)
    Today I did this today, but it was too easy:

    Code:
    <?
    
    function tpl($d, $o){
      DEFINE($d, $o);
    }
    
    $layout[1] = '<table width="100%" border="1"><tr><td><b>items</b></td><td><b>_SERVER code</b></td></tr>';
    $layout[2] = '<tr>';
    $layout[3] = '<td>';
    $layout[4] = '</td>';
    $layout[5] = '</tr>';
    $layout[6] = '</table>';
    
    $l = $layout;
    
    tpl('l1', $l[1]);
    tpl('l2', $l[2]);
    tpl('l3', $l[3]);
    tpl('l4', $l[4]);
    tpl('l5', $l[5]);
    tpl('l6', $l[6]);
    
    $uAddr = $_SERVER['REMOTE_ADDR'];
    $uAgent = $_SERVER['HTTP_USER_AGENT'];
    $uHost = $_SERVER['HTTP_HOST'];
    $uDocRoot  = $_SERVER['DOCUMENT_ROOT'];
    $sName = $_SERVER['SERVER_NAME'];
    $sSoftware = $_SERVER['SERVER_SOFTWARE'];
    $sSig = $_SERVER['SERVER_SIGNATURE'];
    $rURI = $_SERVER['REQUEST_URI'];
    
    echo l1;
    
    echo l2;
    echo l3;
          echo $uAgent;
    echo l4;
    echo l3;
          echo 'HTTP_USER_AGENT';
    echo l4;
    echo l5;
    
    
    echo l2;
    echo l3;
      echo $uAddr;
    echo l4;
    echo l3;
      echo 'REMOTE_ADDR';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $uHost;
    echo l4;
    echo l3;
      echo 'HTTP_HOST';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $uDocRoot;
    echo l4;
    echo l3;
      echo 'DOCUMENT_ROOT';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $sName;
    echo l4;
    echo l3;
      echo 'SERVER_NAME';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $sSoftware;
    echo l4;
    echo l3;
      echo 'SERVER_SOFTWARE';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $sSig;
    echo l4;
    echo l3;
      echo 'SERVER_SIGNATURE';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $rURI;
    echo l4;
    echo l3;
      echo 'REQUEST_URI';
    echo l4;
    echo l5;
    
    echo l2;
    echo l3;
      echo $uDocRoot;
      echo $rURI;
    echo l4;
    echo l3;
      echo 'DOCUMENT_ROOT & REQUEST_URI';
    echo l4;
    echo l5;
    
    
    echo l4;
    
    ?>
    
    Unecessarily (sp?) complicated, but it was to practice. I did it all off the top of my heae, no manuals or anything, but as I said it was too easy and I didn't learn anything from it >.<

    So, any other ideas of what I can write to practice / learn more ?

    Craig.
     
  2. SamHandwich

    Gangster

    Joined: 11 Apr 2004

    Posts: 341

    Location: Loughborough/Stafford

    Not to sound patronising, but I'd reccomend at least having a look at the manual at http://uk.php.net. I don't think I've ever seen such a long winded way of going about something. Maybe to practice you should try and cut that code down to, ooh I dunno, I reckon it could be done in 10 lines or less.
     
  3. robmiller

    Capodecina

    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    Why would you write a function that has the same arguments as and does exactly the same thing as define(), and why would you assign HTML to constants that have absolutely no indication of their contents from their name?

    Also, you effectively prevent yourself from printing numbers if you assign constants as numbers:

    Code:
    echo 1; // prints 1
    define('1', 'foo');
    echo 1; // prints foo
    
    So you end up having to write all your numbers as strings.

    Try this, which achieves the same in a much easier and more modifiable way:

    Code:
    <?php
    
    echo '
    <table id="server">
    	
    	<tr>
    		<th>Name</th>
    		<th>Value</th>
    	</tr>
    ';
    
    foreach($_SERVER as $k => $v)
    {
    	
    	echo "
    	<tr>
    		<td>$k</td>
    		<td>$v</td>
    	</tr>";
    	
    }
    
    echo '
    	</table>
    ';
    
    ?>
    
    It's all well and good saying "I didn't learn anything", but you should be looking for ways to make your scripts more concise, easier to read and if possible more efficient.
     
  4. Craig321

    Capodecina

    Joined: 2 May 2004

    Posts: 19,974

    I made it complicated on purpose, as I said to practice using functions, define etc.

    I wouldn't normally use that code, that's just silly.
     
  5. hendrix

    Wise Guy

    Joined: 4 Aug 2004

    Posts: 1,889

    Location: Grenoble, France

    Have a go at setting yourself a project of some sort. Have a bash at Pool/Snooker Tournement or something like that. Having a bigger idea sometimes help collect everything together.

    My 2p :)
     
  6. Craig321

    Capodecina

    Joined: 2 May 2004

    Posts: 19,974

    hendrix, not sure what you mean?

    I thought maybe writing a forum might be a good idea, I'd start by doing the simple parts like new topic, reply to thread etc. And then I'd go onto the extras.
     
  7. hendrix

    Wise Guy

    Joined: 4 Aug 2004

    Posts: 1,889

    Location: Grenoble, France

    One of my biggest problems is trying to explain things :o

    Having a project, like for example Forum is what i was trying to get at. Something that you have to combine lots of different things together.
     
  8. Craig321

    Capodecina

    Joined: 2 May 2004

    Posts: 19,974

    Ah, OK. Just wasn't sure what you meant by "Have a bash at Pool/Snooker Tournement or something like that."

    I did start a message board a while ago, I got stuck on replies though, it wouldn't list all the replies to the thread for some reason. I'll probably start it again and see if I can do it this time :)

    Craig.