Go Back   SitePoint Forums > Forum Index > Program Your Site > PHP
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Jul 10, 2002, 17:53   #1
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Talking Anyone for PHP Tennis?

This came up a while back but reckon it's time to give it a try. Start with some simple rules and see what happens.

1. First person serves with a script from the Zend Code gallery

2. Each return must;

a. Make the previous code more efficient / minimalistic

b. Add one piece of functionality to the code that wasn't there before.

c. Not add / delete more than 3 lines to the previous code and must not change more than 6 lines overall (including adds / deletes).

Other Stuff If you can't meet the requires of 2. or you post code with a bug in it, you lose! If you're caught editing your post, you also lose (ed: I just lost - was updating the rules honest!). You don't have to comment code and if the origional from Zend has comments, you can delete them to save lines. You may not use includes or require or reference any external PHP source (e.g. dB using eval() - all code most be part of a single script. Finally, this is not allowed;

PHP Code:

echo ( "Hello " ); echo ( "World!" ); 

...only one closing semicolon per line. Oh and one last thing - you can't have more than 80 characters on a single line - make sure your editor can tell you.

If that sounds fair enough, I'll start with the first serve, with Source viewer

PHP Code:

<? 

function source($source)
{  
  
clearstatcache();
  if(!
file_exists($source))
  {
    return
1;
  }

  
$src =fopen($source,"r");
  
$test = fread ($src, filesize($source));
  
$test = htmlentities($test);
  
$test = str_replace(" ","&nbsp;",$test);
  
$test = nl2br($test);
  echo
$test;
  
fclose($src);
  return
0;
}

source($source);
?>

Last edited by HarryF; Jul 10, 2002 at 18:20..
HarryF is offline   Reply With Quote
Old Jul 10, 2002, 18:28   #2
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Okay... I'll bite.

PHP Code:

<?php

function source($source)
{
  
clearstatcache();
  if(!
file_exists($source))
  {
    return
1;
  }
  if(
$src=fopen($source,'r')){
    
$data = fread($src, filesize($source));
    
$data = nl2br(str_replace(' ','&nbsp;',htmlentities($data)));
    
fclose($src);
  }else{
    die(
'unable to open source file');
  }
  echo
$data;
  return
0;
}
source($source);
?>
randem is offline   Reply With Quote
Old Jul 10, 2002, 18:31   #3
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Re: Anyone for PHP Tennis?

Quote:
Originally posted by HarryF
If you...post code with a bug in it, you lose!
I assume that means a fatal error, yeah? Considering the term bug can describe any unplanned result from the code.

And if we find security holes, should we try to license the code to Microsoft? :-)
randem is offline   Reply With Quote
Old Jul 10, 2002, 18:43   #4
okrogius
SitePoint Guru
 
okrogius's Avatar
 
Join Date: Mar 2002
Location: US
Posts: 633
PHP Code:

<?php

function source($source)
{
  
clearstatcache();
  if(!
file_exists($source)) {
    return
1;
  }
  if(
$src=fopen($source,'r')){
    
$data = fread($src, filesize($source));
    
$data = nl2br(str_replace(' ','&nbsp;',htmlentities($data)));
    
fclose($src);
  }else{
    die(
'unable to open source file');
  }
  echo
$data;
  return
0;
}
source($source);
?>
okrogius is offline   Reply With Quote
Old Jul 10, 2002, 18:44   #5
okrogius
SitePoint Guru
 
okrogius's Avatar
 
Join Date: Mar 2002
Location: US
Posts: 633
Err stupid me forgot one thing. Well sicne we can't edit by rules I'd thouht I'll post the updated version.

PHP Code:

<?php

function source($source)
{
  
clearstatcache();
  if(!
file_exists($source)) return 1;
  if(
$src=fopen($source,'r')){
    
$data = fread($src, filesize($source));
    
$data = nl2br(str_replace(' ','&nbsp;',htmlentities($data)));
    
fclose($src);
  }else{
    die(
'unable to open source file');
  }
  echo
$data;
  return
0;
}
source($source);
?>
okrogius is offline   Reply With Quote
Old Jul 10, 2002, 18:45   #6
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Alright!

Quote:
I assume that means a fatal error, yeah? Considering the term bug can describe any unplanned result from the code.
Exactly - just fatal errors. (guess this game we may have to adjust the rules as we go - not sure how well this is gonna work. Also need to get myself a diff tool to check I'm in bounds (lines changed)... think I just made it here...

PHP Code:

<?php

function source($source)
{
  
clearstatcache();
  if(!
file_exists($source))
    return
1;
  if(
$src=fopen($source,'r')){
    
$data = fread($src, filesize($source));
    
$data = highlight_string ( addslashes ( $data ), TRUE );
    
fclose($src);
  }else{
    die(
'unable to open source file');
  }
  return (
$data );
}
echo (
source("excel.php"));
?>
HarryF is offline   Reply With Quote
Old Jul 10, 2002, 18:47   #7
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Aaarrgggh!

PHP Code:

echo ( source("excel.php")); 

You gonna allow me that? - tested it on a script on my machine and forgot to switch it back.
HarryF is offline   Reply With Quote
Old Jul 10, 2002, 18:49   #8
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Wait a second! OK - no problem - was thinking this would be one on one so my response is to randem.

We could of course start playing via CVS

Anyway - I got a catch some sleep so may be you want to take over Codename49?
HarryF is offline   Reply With Quote
Old Jul 10, 2002, 19:04   #9
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Volley !

PHP Code:

<?php

class PHPFile{
  function
get_source($source)
  {
    
clearstatcache();
    if(!
file_exists($source))
      return
1;
    if(
$src=fopen($source,'r')){
      
$data = fread($src, filesize($source));
      
$data = highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
    }else{
      die(
'unable to open source file');
    }
    return (
$data );
  }
}
$file = new PHPFile;
echo
$file->get_source('excel.php');
?>
randem is offline   Reply With Quote
Old Jul 11, 2002, 01:16   #10
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Ouch! Sly use of a class. HarryF's gonna have to streach to return that one...
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 01:21   #11
platinum
+
 
platinum's Avatar
 
Join Date: Jun 2001
Location: Adelaide, Australia
Posts: 6,509
how about posting some links to a working page
__________________
gavinbenda.com.au // // netforge.com.au
platinum is offline   Reply With Quote
Old Jul 11, 2002, 02:06   #12
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Good point: http://www.pinkgoblin.com/test/tennis.php is this;

PHP Code:

<?php

class PHPFile{
  function
get_source($source)
  {
    
clearstatcache();
    if( ( !
file_exists($source) || !$src=fopen($source,'r') ) )
      return
0;
    else{
      
$data = fread($src, filesize($source));
      
$data = highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
      
$data .= "<p>Last modified: ".date('d-M-Y H:i:s',filemtime($source ));
      return
$data;
    }

  }
}
$file = new PHPFile;
echo
$file->get_source($source);
?>
Back hands with improved error reporting but almost blows it, clipping the net with a non fatal error warning: "Warning: Unexpected character in input: '\' (ASCII=92) state=1 in tennis.php on line 10"
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 02:29   #13
whofarted
SitePoint Addict
 
whofarted's Avatar
 
Join Date: Aug 2001
Location: lost, If you find me please return me to St.Louis
Posts: 396
in that link all I see is this:

<html>
<head>
<title> IP Check </title>
</head>
<body>
<h2>IP Check</h2>
<?php

if ( ISSET ( $_SERVER[
Warning: Unexpected character in input: '\' (ASCII=92) state=1 in /home/goblin/public_html/test/tennis.php on line 10
'HTTP_X_FORWARDED_FOR\'] ) ) {
echo ( \"<p>HTTP_X_FORWARDED_FOR: \" . $_SERVER[\'HTTP_X_FORWARDED_FOR\'] . \" Hostname: \" . gethostbyaddr($_SERVER[\'HTTP_X_FORWARDED_FOR\']) );
} else {
echo ( \"<p>HTTP_X_FORWARDED_FOR not set\" );
}
if ( ISSET ( $_SERVER[\'HTTP_CLIENT_IP\'] ) ) {
echo ( \"<p>HTTP_CLIENT_IP: \" . $_SERVER[\'HTTP_CLIENT_IP\'] . \" Hostname: \" . gethostbyaddr($_SERVER[\'HTTP_CLIENT_IP\']) );
} else {
echo ( \"<p>HTTP_CLIENT_IP not set\" );
}
if ( ISSET ( $_SERVER[\'REMOTE_ADDR\'] ) ) {
echo ( \"<p>REMOTE_ADDR: \" . $_SERVER[\'REMOTE_ADDR\'] . \" Hostname: \" . gethostbyaddr($_SERVER[\'REMOTE_ADDR\']) );
} else {
echo ( \"<p>REMOTE_ADDR not set\" );
}

?>
</body>
</html>
Last modified: 28-Jun-2002 05:43:42
__________________
You smell something?
whofarted is offline   Reply With Quote
Old Jul 11, 2002, 02:36   #14
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
That is what you're supposed to see. This script reads another PHP file and displays the source code. It's like the "Show Source" links you find here for example.
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 05:49   #15
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
PHP Code:

<?php

class PHPFile{
  function
get_source($source)
  {
    
clearstatcache();
    if(!
file_exists($source))
      return
1;
    if(
$src=fopen($source,'r')){
      
$data = fread($src, filesize($source));
      
$data = highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
    }else{
      die(
'unable to open source file');
    }
    return
stripslashes($data);
  }
}
$file = new PHPFile;
echo
$file->get_source('excel.php');
?>
You might wanna test that, I'm not quite sure if it'll work.
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 11, 2002, 05:50   #16
randem
morphine for a wooden leg
 
randem's Avatar
 
Join Date: Jun 2002
Location: .chicago.il.us
Posts: 957
Sorry, can't post a link. I'm at work, and putting this on our server wouldn't go over too well. :-)
PHP Code:

<?php

class PHPFile{
  function
PHPFile( $filename ){
      
$this->filename = $filename;
  }
  function
get_source()
  {
$source = $this->filename;
    
clearstatcache();
    if( ( !
file_exists($source) || !$src=fopen($source,'r') ) )
      return
0;
    else{
      
$data = fread($src, filesize($source));
      
$data = @highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
      
$data .= "<p>Last modified: ".date('d-M-Y H:i:s',filemtime($source ));
      return
$data;
    }
  }
}
$file = new PHPFile( $_SERVER['SCRIPT_FILENAME'] );
echo
$file->get_source();
?>
randem is offline   Reply With Quote
Old Jul 11, 2002, 06:04   #17
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
PHP Code:

$file = new PHPFile( $_SERVER['SCRIPT_FILENAME'] ); 

You sly dog! Slipping a big of back spin on it! Hmmmmm...

To all the "spectators" out there; this is an expirmental game derived from the Photoshop Tennis forum (wierd that is too). If it looks good, may be we can request a "Server-side Tennis" Forum (no doubt those ASP coders will want to get in on the action).
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 06:05   #18
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,312
yay! we've finally got a Tennis match going around here... never really figured how that worked at the Graphic design forums so i'm gonna read this thread out again...

i'm game... just let me get my racket
redemption is offline   Reply With Quote
Old Jul 11, 2002, 06:26   #19
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
Seems to work ok as well. The "no more than 6 lines changed limit" seems to work. Phew - getting tough now - got a further optimisation in mind and an extra piece of functionality, but try make them both work by only changing six lines aint happening yet.
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 06:31   #20
someonewhois
SitePoint Wizard
silver trophy
 
someonewhois's Avatar
 
Join Date: Jan 2002
Location: Canada
Posts: 6,399
Ok, since you posted 1 minute after me, I think I'll modify mine what I said before, to teh way yours is, if that makes any sense!

PHP Code:

<?php

class PHPFile{
  function
PHPFile( $filename ){
      
$this->filename = $filename;
  }
  function
get_source()
  {
$source = $this->filename;
    
clearstatcache();
    if( ( !
file_exists($source) || !$src=fopen($source,'r') ) )
      return
0;
    else{
      
$data = fread($src, filesize($source));
      
$data = @highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
      
$data .= "<p>Last modified: ".date('d-M-Y H:i:s',filemtime($source ));
      return
stripslashes($data);
    }
  }
}
$file = new PHPFile( $_SERVER['SCRIPT_FILENAME'] );
echo
$file->get_source();
?>

People dont' want those slashes in teh soource!

Harry, maybe you can upload this one?
I wan tto see it.
__________________
- Nathan
someonewhois is offline   Reply With Quote
Old Jul 11, 2002, 06:39   #21
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,312
one thing i don't get is how putting things in a class would make the code more efficient or minimalistic... but since we went the OO way, i'll jump in and make a return!

PHP Code:

<?php

class PHPFile{
  var
$filename;
  var
$errmsg;
  function
PHPFile( $filename ){
      
$this->filename = $filename;
      if (!
file_exists($filename))
      {
        
$this->$errmsg = "'$filename' does not exist!";
      }
  }
  function
get_source()
  {
    
clearstatcache();
    if( !
$src=fopen($this->filename,'r') )
      return
0;
    else{
      
$data = fread($src, filesize($this->filename));
      
$data = @highlight_string( addslashes ( $data ), TRUE );
      
fclose($src);
      
$data .= "<p>Last modified: ".date('d-M-Y H:i:s',filemtime($this->filename));
      return
$data;
    }
  }
}
$file = new PHPFile( $_SERVER['SCRIPT_FILENAME'] );
$source_data = $file->get_source();
echo
$source_data;
?>
i added the class properties (since we're going the OO way, we might as well do it right)... i wanted to make further use of the $errmsg property but didn't cos of the rules... (hint: i wanted to test for it's existence everytime i did a get source)

actually i don't see how 'Add one piece of functionality to the code that wasn't there before.' and 'Make the previous code more efficient / minimalistic' can co-exist in the ruls

- Joel
redemption is offline   Reply With Quote
Old Jul 11, 2002, 06:45   #22
redemption
SitePoint Wizard
silver trophy
 
redemption's Avatar
 
Join Date: Sep 2001
Location: Singapore
Posts: 5,312
oops someonewhois posted while i was writing...

and i seem to have broken the 6 lines limit... discount that for the fact that i added 2 properties?
redemption is offline   Reply With Quote
Old Jul 11, 2002, 07:02   #23
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
What is this? A pitch invasion!?!

Quote:
actually i don't see how 'Add one piece of functionality to the code that wasn't there before.' and 'Make the previous code more efficient / minimalistic' can co-exist in the rules
So far me and random have made it work but it definately needs more defining - minimilistic needs the idea of re-usability added to it. By the minimilistic rule you're out I'm afraid - we haven't stated anywhere that you have to use good coding practices, like declaring properties.

And this;

PHP Code:

echo $source_data; 

Definate no no.

Nice angle of more error reporting though - could constitute extra functionality I guess.

OK - you carry on - I'm gonna go hunting for an Open Source "diff" tool (checks the difference between two files) to act as the referee.
HarryF is offline   Reply With Quote
Old Jul 11, 2002, 07:08   #24
samsm
SitePoint Wizard
 
samsm's Avatar
 
Join Date: Nov 2001
Location: Atlanta, GA, USA
Posts: 5,024
Wow, this isn't a tennis match, it's like a giant PHP Battle Royale with new people charging in from the locker room every few minutes! Look out for the folding chairs!

Seriously, this is great... a fun way to get more efficient and creative code... I'm enjoying spectating.
__________________
Using your unpaid time to add free content to SitePoint Pty Ltd's portfolio?
samsm is offline   Reply With Quote
Old Jul 11, 2002, 07:52   #25
HarryF
SitePoint Wizard
gold trophysilver trophy
 
Join Date: Nov 2000
Location: Switzerland
Posts: 2,898
"Just when they thought he was down, HarryF storms back with that killer forehand!"

Check this out!

Refinement - those irritating slashes now gone (**cough** thanks someonewhois) and bam - line numbering, with a devilishly constructed for loop. Bring it on randem!

PHP Code:

<?php

class PHPFile{
  function
PHPFile( $filename ){
      
$this->filename = $filename;
  }
  function
get_source()
  {
$source = $this->filename;
    
clearstatcache();
    if( ( !
file_exists($source) || !$src=fopen($source,'r') ) )
      return
0;
    else{
      
$data = fread($src, filesize($source));
      
$data=stripslashes(@highlight_string(addslashes($data),TRUE));
      
fclose($src);
      
$data=explode("<eol>",str_replace("<br />","<br /><eol>",$data));
      for(
$i=1;$i<=count($data)-1;$output.=$i." ".$data[$i-1],$i++);
      
$data=$output."<p>Modified: ".date('d-M-Y H:i:s',filemtime($source ));
      return
$data;
    }
  }
}
$file = new PHPFile( $_SERVER['SCRIPT_FILENAME'] );
echo
$file->get_source();
?>
Out of hype mode for a moment: starting to see something that can be a way to make games come to an end. With the 80 char limit per line plus not more than 6 lines changed, if we set a limit to the maximum and minimum number of totals lines the script can have, you can basically fill up each line to 80 chars, with some long var names for example, and eventually force the other player to break one of the limits.

We should also set some rules on indents and use of curly braces perhaps. But then again perhaps not - if you want to indent a line by 10 characters for example, that's fine - you force the other player to waste one line of their changes in reducing the indent.
HarryF is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

 
Forum Jump


All times are GMT -7. The time now is 09:23.


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved