# php maths

• Feb 16, 2009, 03:42
Mike4x4s
php maths
is there an example at all where i can do a sum in php 1 +3 = answer?
• Feb 16, 2009, 03:43
guido2004
\$result = \$number1 + \$number2;
• Feb 16, 2009, 04:06
awright418
Uh oh... Remember this thread?

PHP Code:

``` if(1 + 3 == 4){     echo "answer"; }  ```
• Feb 16, 2009, 04:10
guido2004
Quote:

Originally Posted by awright418

:lol: yeah
• Feb 16, 2009, 05:13
-T-
Quote:

Originally Posted by awright418
Uh oh... Remember this thread?

PHP Code:

``` if(1 + 3 == 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);  ```
• Feb 16, 2009, 05:30
-T-
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);  ```
• Feb 16, 2009, 06:04
Jake Arkinstall
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(0, Count(\$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(0, Count(\$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 ;)
• Feb 16, 2009, 07:20
-T-
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:

``` <?phpclass 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(); ```
• Feb 16, 2009, 10:34
crmalibu
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)
• Feb 16, 2009, 10:55
Young Twig
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     ##        ##        ####            ##     ##        ##        ####            ##   ####      ####      ##              ####   ####      ####      ##              ####     ##        ##      ####              ##     ##        ##      ####              ##     ##        ##      ##  ##            ##     ##        ##      ##  ##            ##   ######    ######      ##      ##    ######   ######    ######      ##      ##    ######```
• Feb 16, 2009, 11:10
Mark Baker
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;  ```
• Feb 16, 2009, 11:33
crmalibu
I wanna see the genetic mutation method again lol
• Feb 16, 2009, 18:07
prafuitu
Quote:

Originally Posted by Mike4x4s
is there an example at all where i can do a sum in php 1 +3 = answer?

PHP Code:

``` echo 4;  ```
• Feb 16, 2009, 18:18
prafuitu
...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_pop( explode("\n", trim(`echo \$((3 + 1))`) ) ) );  ```
• Feb 17, 2009, 00:58