SitePoint Sponsor

User Tag List

Results 1 to 15 of 15

Thread: php maths

  1. #1
    SitePoint Guru
    Join Date
    Sep 2007
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php maths

    is there an example at all where i can do a sum in php 1 +3 = answer?
    Animated Chatrooms - www.121chatrooms.net

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    $result = $number1 + $number2;

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uh oh... Remember this thread?

    http://www.sitepoint.com/forums/showthread.php?t=580793

    How about....

    PHP Code:
    if(== 4){
        echo 
    "answer";


  4. #4
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,491
    Mentioned
    161 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by awright418 View Post
    yeah

  5. #5
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by awright418 View Post
    Uh oh... Remember this thread?

    http://www.sitepoint.com/forums/showthread.php?t=580793

    How about....

    PHP Code:
    if(== 4){
        echo 
    "answer";

    Heh, I'm up for some fun

    PHP Code:
    function sum($arg1$arg2)
    {
      
    $x 0;
      for ( 
    $i 0$i $arg1$i++)
      {
        
    $x++;
      }
     
      for ( 
    $i 0$i $arg2$i++ )
      {
        
    $x++;
      }
      return 
    $x;
    }

    echo 
    sum(4,6); 
    Last edited by -T-; Feb 16, 2009 at 05:30. Reason: typo
    chrome is a wrapper that combines a browser with spyware

  6. #6
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or make it handle a variable amount of numbers
    PHP Code:
    function sum()
    {
      
    $args function_get_args();
      
    $sum 0;

      foreach(
    $args AS $arg)
      {
        if( 
    is_numeric($arg
        {
          for(
    $i 0$i $arg$i++ )
          {
            
    $sum++;
          }
        }
      }
      
      return 
    $sum;
    }

    echo 
    sum(4,6,6,7,4,3); 
    chrome is a wrapper that combines a browser with spyware

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ooh, let the contraptions begin:
    PHP Code:
    <?php
    class Count{
        protected 
    $Raw = array();
        function 
    AddNumber($Number){
            for(
    $i 0$i $Number$i++){
                
    $this->Raw[] = 1;
            }
        }
        function 
    GetRaw($Recursion 0){
            
    $Count = -1;
            foreach(
    range(0Count($this->Raw)) as $Number){
                
    $Count -= $Number 1;
                
    $Count += $Number;
            }
            if(
    $Recursion == 1000) return $Count;
            return 
    $this->GetRaw($Recursion 1);
        }
        function 
    GetSum(){
            return 
    $this->GetRaw();
            
    $Count 0;
            foreach(
    range(0Count($this->GetRaw())) as $Number){
                
    $Count $Number;
            }
            return 
    $Count;
        }
    }
    $Count = new Count();
    $Count->AddNumber(4);
    $Count->AddNumber(40);
    $Count->AddNumber(400);
    $Count->AddNumber(4000);
    echo 
    $Count->GetSum();
    Off Topic:

    @OP, don't take that code seriously
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This one uses ASCII to iterate

    Can handle numbers up to 1000 characters long, though, it is a bit of a memory hog

    PHP Code:
    <?php

    class Sum
    {
        private 
    $asciiArray;
        
        public function 
    Sum()
        {
            for( 
    $i 0$i 1000$i++)
            {
                
    $this->asciiArray[$i] = 48;
            }
            
        }
        
        public function 
    addNumber($number)
        {
            if ( 
    is_numeric($number) == false) return false;
            
            if( 
    strlen($number) == 1)
            {
                for(
    $i 0$i $number$i++)
                {
                    
    $this->recursiveIterator(0);
                }
            }
            else
            {
                
    $splitNumber str_split($number);
                
                
    $revSplitNumber array_reverse($splitNumber);
                
                for(
    $i 0$i count($revSplitNumber); $i++)
                {
                    if(
    $revSplitNumber[$i] > 0)
                    {
                        for(
    $x 0$x $revSplitNumber[$i]; $x++)
                        {
                            
    $this->recursiveIterator($i);
                        }
                    }
                }
            }
            
            
            
        }
        
        public function 
    printSum()
        {
            
    $print array_reverse($this->asciiArray);
            
            
    $hitNumber false;
            
            foreach( 
    $print AS $ascii )
            {
                
    $number chr($ascii);
                if(
    $hitNumber == false && $number 0)
                {
                    
    $hitNumber true;
                }
                if(
    $hitNumber == true)
                {
                    echo 
    $number;
                }
                
            }
        }
        
        private function 
    recursiveIterator($x)
        {
            
            
    $this->asciiArray[$x]++;
            
            if(
    $this->asciiArray[$x] > 57)
            {
                
    $this->asciiArray[$x] = 48;
                
    $this->recursciveIterator($x 1);
            }
        }
    }

    $Sum = new Sum();
    $Sum->addNumber(1);
    $Sum->addNumber(2);
    $Sum->addNumber(3);
    $Sum->addNumber(100000);
    $Sum->printSum();
    chrome is a wrapper that combines a browser with spyware

  9. #9
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you mean let the user submit a string that contains "1 + 3" and you provide the answer? Like

    <input name="equation" value="1 + 3">

    Like how google does it when you type 1 + 3 into thier search engine? (try it)

  10. #10
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    php -r "$size=2;$px=array('1'=>array(0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,1,0),'2'=>array(0,0,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,0),'3'=>array(0,1,1,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1,1,1,0),'4'=>array(0,1,0,1,0,0,1,0,1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,1,0),'5'=>array(0,1,1,1,0,0,1,0,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0),'6'=>array(0,0,1,1,0,0,1,0,0,0,0,1,1,0,0,0,1,0,1,0,0,0,1,0,0),'7'=>array(0,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0),'8'=>array(0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0),'9'=>array(0,0,1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1,1,0,0),'0'=>array(0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,1,0,0),'-'=>array(0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0),'.'=>array(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0));$sum=strval(floatval($argv[1])+floatval($argv[2]));for($r=0,$rr=0;$r<5*$size;$r++,$rr=floor($r/$size)){for($d=0;$d<strlen($sum);$d++){$a=$px[$sum[$d]];for($x=count($a)/5,$i=$rr*$x;$i<$x+$rr*$x;$i++){echo str_repeat(($a[$i]===1)?'#':' ',$size);}}echo \"\n\";}" x y
    Replace x and y with the numbers you wish to add. You can also change the size by editing $size in the source code.

    Example:
    Code:
    > php -r "$size=2;$px=array('1'=>array(0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,0,0,1,0,0,0,1,1,1,0),'2'=>array(0,0,1,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,0,0,0,1,1,1,0),'3'=>array(0,1,1,1,0,0,0,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1,1,1,0),'4'=>array(0,1,0,1,0,0,1,0,1,0,0,1,1,1,0,0,0,0,1,0,0,0,0,1,0),'5'=>array(0,1,1,1,0,0,1,0,0,0,0,1,1,0,0,0,0,0,1,0,0,1,1,0,0),'6'=>array(0,0,1,1,0,0,1,0,0,0,0,1,1,0,0,0,1,0,1,0,0,0,1,0,0),'7'=>array(0,1,1,1,0,0,0,0,1,0,0,0,1,0,0,0,0,1,0,0,0,1,0,0,0),'8'=>array(0,0,1,0,0,0,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,1,0,0),'9'=>array(0,0,1,0,0,0,1,0,1,0,0,0,1,1,0,0,0,0,1,0,0,1,1,0,0),'0'=>array(0,0,1,0,0,0,1,0,1,0,0,1,0,1,0,0,1,0,1,0,0,0,1,0,0),'-'=>array(0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0),'.'=>array(0,0,0,0,0,0,0,0,0,0,0,0,0,1,0));$sum=strval(floatval($argv[1])+floatval($argv[2]));for($r=0,$rr=0;$r<5*$size;$r++,$rr=floor($r/$size)){for($d=0;$d<strlen($sum);$d++){$a=$px[$sum[$d]];for($x=count($a)/5,$i=$rr*$x;$i<$x+$rr*$x;$i++){echo str_repeat(($a[$i]===1)?'#':' ',$size);}}echo \"\n\";}" 95 21.1
        ##        ##        ####            ##
        ##        ##        ####            ##
      ####      ####      ##              ####
      ####      ####      ##              ####
        ##        ##      ####              ##
        ##        ##      ####              ##
        ##        ##      ##  ##            ##
        ##        ##      ##  ##            ##
      ######    ######      ##      ##    ######
      ######    ######      ##      ##    ######

  11. #11
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or using multiplication to calculate the result of an addition:
    PHP Code:
    $number1 23;

    $number2 19;

    $answer log(exp($number1) * exp($number2));

    echo 
    $number1.' + '.$number2.' = '.$answer
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  12. #12
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wanna see the genetic mutation method again lol

  13. #13
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mike4x4s View Post
    is there an example at all where i can do a sum in php 1 +3 = answer?
    PHP Code:
    echo 4

  14. #14
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...or maybe some shell help:
    PHP Code:
    exec('echo $((3 + 1))'$result);
    echo 
    array_pop($result); 
    with the much simpler and easy to read, single line alternative using the backtick operator:

    PHP Code:
    echo trim(array_popexplode("\n"trim(`echo $((3 + 1))`) ) ) ); 

  15. #15
    Non-Member adstiger's Avatar
    Join Date
    Nov 2008
    Location
    Canada
    Posts
    348
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo $x + $y


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
  •