is there an example at all where i can do a sum in php 1 +3 = answer?
| SitePoint Sponsor |




is there an example at all where i can do a sum in php 1 +3 = answer?
Animated Chatrooms - www.121chatrooms.net
$result = $number1 + $number2;
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
Uh oh... Remember this thread?
http://www.sitepoint.com/forums/showthread.php?t=580793
How about....
PHP Code:if(1 + 3 == 4){
echo "answer";
}
Guido - Community Team Advisor
Do you know where the (database) error is? Add it to the list!
Thinking Web: Voices of the Community
Blog - Free Flash Slideshow Widget
Last edited by -T-; Feb 16, 2009 at 05:30. Reason: typo
chrome is a wrapper that combines a browser with spyware
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
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![]()
Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
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
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)





Replace x and y with the numbers you wish to add. You can also change the size by editing $size in the source code.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
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 ## ## #### ## ## ## #### ## #### #### ## #### #### #### ## #### ## ## #### ## ## ## #### ## ## ## ## ## ## ## ## ## ## ## ###### ###### ## ## ###### ###### ###### ## ## ######



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;
I wanna see the genetic mutation method again lol
...or maybe some shell help:
with the much simpler and easy to read, single line alternative using the backtick operator:PHP Code:exec('echo $((3 + 1))', $result);
echo array_pop($result);
PHP Code:echo trim(array_pop( explode("\n", trim(`echo $((3 + 1))`) ) ) );
echo $x + $y
Bookmarks