SitePoint Sponsor

User Tag List

Page 3 of 5 FirstFirst 12345 LastLast
Results 51 to 75 of 114
  1. #51
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    ...no for loop though :P

    PHP Code:
    for($i=print(0);($i+=printf("%d",$i))<=9;); 

  2. #52
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Surely this won't work at all, since you don't pass a beginning or end to check() :P
    Gah!
    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  3. #53
    SitePoint Zealot zalucius's Avatar
    Join Date
    Jul 2007
    Location
    Denmark
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Quote Originally Posted by armchaircritic View Post
    Off Topic:


    Well, you guys should think of having a monthly PHP challenge - a simple problem + the interesting/obscure ways of implementing it... would probably be a popular thing
    http://projecteuler.net/

    Plenty of "simple" problems, with "simple" solutions..

    The question is: "how would you solve the problems?"
    zalucius

  4. #54
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    Lets make use of the open web by using Flickr's API.
    PHP Code:
    for(
        
    $i 0;
        
    $i 10;
        print(
            (string) 
    current(simplexml_load_file(
                
    'http://api.flickr.com/services/rest/?method='.
                
    'flickr.test.echo&api_key=baca0c32fba14084c4b'.
                
    '6e4d536b57bc6&format=rest&num='.$i,
                
    'SimpleXMLElement',
                
    LIBXML_NOCDATA
            
    )->xpath('/rsp/num'))
        ),
        
    $i++
    ); 
    Salathe
    Software Developer and PHP Manual Author.

  5. #55
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Montreal
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    array_map('print'range(0,9)); //Functional i suppose 

  6. #56
    SitePoint Wizard bronze trophy C. Ankerstjerne's Avatar
    Join Date
    Jan 2004
    Location
    The Kingdom of Denmark
    Posts
    2,702
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by b1ind View Post
    PHP Code:
    array_map('print'range(0,9)); //Functional i suppose 
    Functional code is a bad idea. If your processes are transparent and your code light-weight, it will be easy for someone else to know how it works. Mke your code jumbled, and you'll secure your job.

    Christian Ankerstjerne
    <p<strong<abbr/HTML/ 4 teh win</>
    <>In Soviet Russia, website codes you!

  7. #57
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    also, it doesn't use a for loop, so isn't a solution :P

  8. #58
    SitePoint Guru Ize's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    808
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    for (
      print
    '0',$a=range(0,9),$i=count($a),$b,$c=array(); 
      
    $b array_pop($a); 
      
    $c[]=$b,print count($c)
    ); 
    This is a fun thread!

  9. #59
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No ones used PHP's string increment yet...

    PHP Code:
    for( $i='0'$i<='9'; ++$i)
        echo 
    $i

  10. #60
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    OK, if we really want to get down and dirty....
    PHP Code:
    for($i=0$i<=1$i++) {
        echo 
    'the numbers 0-9';

    should answer the question exactly......
    hahahaha I actually didnt get it at first.

    never knew about the range() function either.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  11. #61
    Non-Member
    Join Date
    Nov 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    for($i=0; $i<=10; $i++) {
    echo 'the numbers $i;
    }

  12. #62
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Montreal
    Posts
    77
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stormrider View Post
    also, it doesn't use a for loop, so isn't a solution :P
    PHP Code:
    for(array_map('printf'range(0,9));0;9); 
    In classic php-sense, neither echo, nor print can be used as callbacks because they are statements instead of functions.

    This exercise also taught me how to send apache processor usage to 100% very easily:

    PHP Code:
    for(;;); 

  13. #63
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Can print not be used, because it behaves like a function?

  14. #64
    #titanic {float:none} silver trophy
    molona's Avatar
    Join Date
    Feb 2005
    Location
    from Madrid to Heaven
    Posts
    8,222
    Mentioned
    237 Post(s)
    Tagged
    1 Thread(s)
    Off Topic:

    Actually, I thought about proposing some kind of challenge for programmers but then I decided that I would not bring it forward because I couldn't find a good way to implement it... and you see, a simple question and you get this

    Quote Originally Posted by armchaircritic View Post
    Off Topic:


    Well, you guys should think of having a monthly PHP challenge - a simple problem + the interesting/obscure ways of implementing it... would probably be a popular thing


    You guys are all nuts! I can't believe that a FOR can bring so much creativity

  15. #65
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why making such ridiculous solutions? This is good enough:

    PHP Code:
    for($i=0;$<=9;$i++)
      echo 
    $i
    Or:

    PHP Code:
    for($i=0;$<10;$i++)
      echo 
    $i

  16. #66
    SitePoint Evangelist ferrari_chris's Avatar
    Join Date
    Mar 2008
    Location
    NP, New Zealand
    Posts
    576
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this for using the most lines of needless code?:

    Code PHP:
     
    for ($i = 0; $i < 10; $i = $i + 1) {
     switch ($i) {
      case 0:
       echo "0";
      break;
      case 1:
       echo "1";
      break;
      case 2:
       echo "2";
      break;
      case 3:
       echo "3";
      break;
      case 4:
       echo "4";
      break;
      case 5:
       echo "5";
      break;
      case 6:
       echo "6";
      break;
      case 7:
       echo "7";
      break;
      case 8:
       echo "8";
      break;
      case 9:
       echo "9";
      break;
     }
    }

  17. #67
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by risoknop View Post
    Why making such ridiculous solutions? This is good enough:

    PHP Code:
    for($i=0;$<=9;$i++)
      echo 
    $i
    Or:

    PHP Code:
    for($i=0;$<10;$i++)
      echo 
    $i
    I think you've missed the point somewhat

  18. #68
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe these three need a bit of tweaking before they work perfectly..

    PHP Code:
    $href_self = ...;
    if (!isset(
    $_SESSION['buf'])) {
      
    $_SESSION['buf'] = 0;
    } else {
      
    $buf explode(' '$_SESSION['buf']);
      if (
    count($buf) < 10) {
        
    $buf[] = count($buf);
        
    $_SESSION['buf'] = implode(' '$buf);
        
    header('location: '.$href_self); die;
      } else {
        echo 
    implode(', '$buf);
      }

    PHP Code:
    $dir = ...;
    while(
    $filecount count($files scandir($dir)) < 10) {
      
    file_put_contents('_'.$filecount.'.txt''bla');
    }
    echo 
    implode(',',$files); 
    PHP Code:
    echo 'Please type the numbers from 0-9<br/><input/>'

  19. #69
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My "simple" OOP solution with a couple of design patterns (Factory,Observer).

    PHP Code:
    <?php

        
    interface INumber {
            
            function 
    getNumber();
            
        }
        
    ?>
    PHP Code:
    <?php

        
    interface IObserver {
            
            function 
    onChanged($sender,$args);
            
        }
        
    ?>
    PHP Code:
    <?php

        
    interface IObservable {
            
            function 
    addObserver($observer);
            
        }
        
    ?>
    PHP Code:
    <?php
        
        
    class Number implements INumber {
            
            private 
    $_number;
            
            public function 
    __construct($number){
                
                
    $this->_number $number;
                
            }
            
            public function 
    getNumber(){
                
                return 
    $this->_number;
                
            }    
            
        }
        
    ?>
    PHP Code:
    <?php
        
        
    class NumberFactory {
            
            public static function 
    create($number){
                
                return new 
    Number($number);
                
            }
            
        }
        
    ?>
    PHP Code:
    <?php
        
        
    class Printer implements IObserver {
            
            public function 
    onChanged($sender,$args){
                
                echo 
    $args;
                
            }
            
        }
        
    ?>
    PHP Code:
    <?php
        
        
    class Counter implements IObservable {
            
            private 
    $_observers = array();
            
            private 
    $_from;
            private 
    $_to;
            
            public function 
    __construct($from,$to){
                
                
    $this->_from $from;
                
    $this->_to $to;
                
            }
            
            public function 
    addObserver($observer){
                
                
    $this->_observers[] = $observer;
                
            }
            
            public function 
    count(){
                
                for(
    $i $this->_from$i <= $this->_to$i++){
                    
                    
    $number NumberFactory::create($i);
                    
                    foreach(
    $this->_observers as $observer){
                        
                        
    $observer->onChanged($this,$number->getNumber());
                        
                    }
                    
                }
                
            }
            
        }
        
    ?>
    And finally.... The execution:

    PHP Code:
    <?php
        
        $counter 
    = new Counter(0,9);
        
    $counter->addObserver(new Printer());
        
        
    $counter->count();
        
    ?>
    I was thinking about making Counter a singleton but maybe that is just taking it too far!

  20. #70
    SitePoint Member
    Join Date
    Nov 2007
    Location
    Den Haag, Netherlands
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This function is not very fast, but you don't have to do any increments yourself.

    PHP Code:
    for( $i time();  time() - $i <= 9waittime())) {
        print( 
    time() - $i );
    }

    function 
    wait$now 
    {
        while( 
    time() == $now );

    A variation, without adding/substracting:
    PHP Code:
    for( start();  time() % 11 10;  waittime())) {
        print( 
    time() % 11 );
    }

    function 
    start()
    {
        while( 
    time() % 11 );
    }

    function 
    wait$now 
    {
        while( 
    time() == $now );

    Last edited by harryo; Nov 6, 2008 at 17:45. Reason: Addition

  21. #71
    SitePoint Evangelist bals28mjk's Avatar
    Join Date
    Aug 2007
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You all are RIDICULOUS.
    Code:
    echo "
    	<script type='text/javascript'>
    	onload=function(){
    	var a=[];
    	for(var i=0;i<10;i++)
    	{
    		a[i]=i;
    	}
    	document.body.appendChild(document.createTextNode(a));
    	}
    	
    	</script>
    	";

  22. #72
    SitePoint Evangelist AlienDev's Avatar
    Join Date
    Feb 2007
    Location
    UK
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some replies in this thread are unbelievable! :P

    This is untested, but:

    PHP Code:
    <?php

    $nums 
    = array(01.468643924491.58702339780.246300878109, -1.32086953304, -1.67363858702, -0.4876720424671.146657928911.726755888530.71928244758);

    foreach (
    $nums as $num)
    {
      
    $new rad2deg (asin ($num 100));
      echo 
    $new"\n";
    }
    Me on StackOverflow | Blog & personal website.

    I mostly use: PHP, Java, JavaScript, Android.

  23. #73
    Non-Member DelvarWorld's Avatar
    Join Date
    Jul 2004
    Location
    Baloney
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A page that headers to itself 9 times and outputs the string each time until it's done. Someone write this for me, as I am too busy and important.

  24. #74
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Back to for loop coding , here's a flexible procedural object-oriented pseudofunctional programmatic variation; will work with any pair of mumbers, and will count up or down.
    PHP Code:
     
    for ($start 0$end 9$break false; ; ) {
        if (!
    class_exists('CountUpable')) {
            class 
    CountUpable
            
    {
                private 
    $num;
                function 
    __construct($val)
                {
                    
    $this->num $val;
                }
                public function 
    __get($var)
                {
                    return 
    $this->$var;
                }
                public function 
    __set($var$val)
                {
                    
    $this->$var $val;
                }    
                public function 
    __toString()
                {
                    return (string) 
    $this->num;
                }
            }
            
    $count_up = new CountUpable($start);
        }
        echo 
    $count_up;
        if (
    $start $end) {
            
    $count_up->num += 1;
            if (
    $count_up->num $end) {
                
    $break true;
            }  
        } elseif (
    $start $end) {
            
    $count_up->num -= 1;
            if (
    $count_up->num $end) {
                
    $break true;
            }  
        } else {
            
    $break true;
        }
        if (
    $break) {
            break;
        }

    And some more simple solutions:
    PHP Code:
     
    for ($i = -1$i <= 8; ) {
        while ((
    $n rand(09)) !== ($i 1)) {}
        echo (
    $i $n);
    }
     
    for (
    $n 0$n <= 127$n++) {
        if (
    ctype_digit(chr($n))) {
            echo 
    chr($n);
        }
    }
     
    for (
    $str '0123456789'; ; ) {
        echo 
    $str;
        break;

    Tested, of course.

  25. #75
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DelvarWorld View Post
    A page that headers to itself 9 times and outputs the string each time until it's done. Someone write this for me, as I am too busy and important.
    Good suggestion... Here ya go!

    PHP Code:
    <?php

        
    if(isset($_GET['number'])){
            
            
    $number = (int)$_GET['number'];
            
            echo 
    $number;
            
            if(
    $number 9){
                
                
    $number++;
            
                for(
    $i 0$i 1$i++){
                        
                    
    header('Refresh: 1; url=index.php?number=' $number);
                
                }
                
            }    
            
        } else {
            
            
    header('Refresh: 1; url=index.php?number=0');
            
        }
        
    ?>
    This is too much fun! They should really make this a more common thread or contest. I would love to see how we could Rube Goldberg code a Hello World! or something.

    Edit: Added useless for loop in order to "correctly" answer the OP question.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •