Shoutbox

PHP Help - Printable Version

-Shoutbox (https://shoutbox.menthix.net)
+-- Forum: MsgHelp Archive (/forumdisplay.php?fid=58)
+--- Forum: Skype & Technology (/forumdisplay.php?fid=9)
+---- Forum: Tech Talk (/forumdisplay.php?fid=17)
+----- Thread: PHP Help (/showthread.php?tid=55104)

PHP Help by Ezra on 01-20-2006 at 05:11 PM

Does anyone know what's wrong with this?

code:
class bin2dec{

    function main($invoer){
        if(isset($invoer) && $invoer != ""){
            $this->substr($invoer);
        }else{
            die("ERROR 0x001");
        }
    }
    function substr($invoer){
        for($i=0; $i<strlen($invoer); $i++){
            $uitvoer[] = substr($invoer,$i,1);
        }
        $this->calculate($uitvoer);
    }
    function calculate($invoer){
        $pw=count($invoer) - 1;
        for($i=0; $i<count($invoer); $i++){
            $uitvoer[] = $invoer[$i] * pow(2, $pw);
            $pw--;
        }
        $this->addall($uitvoer);
    }
    function addall($invoer){
        $uitvoer=0;
        for($i=0; $i<count($invoer); $i++){
            $uitvoer = $invoer[$i] + $uitvoer;
        }
        $this->returnit($uitvoer);
    }
    function returnit($invoer){
        return $invoer;
        //die("Success ofzow");
    }
}

That return on the end doesn't work :(

If I use that die it dies normally...

MyBB wrecks the indents :(

RE: PHP Help by Plik on 01-20-2006 at 05:24 PM

its because of this:

code:
$this->returnit($uitvoer);<< This is where the function is called, hence whatever the function returns will be returned here, but as there is no varible storing the returned value then it is forgoten.
}
function returnit($invoer){I dont understand what you are attempting todo with this function :-/
return $invoer;
//die("Success ofzow");
}

Basically return, returns the value as the result of the function where there must be some way of outputing or storing the value otherwise it will be forgoten.
RE: PHP Help by Ezra on 01-20-2006 at 05:30 PM

So, how do I return the last value to where I called main in my php script?


RE: PHP Help by Plik on 01-20-2006 at 05:36 PM

I guess you could return the result up through the functions, like this:

code:
class bin2dec{

function main($invoer){
if(isset($invoer) && $invoer != ""){
return $this->substr($invoer);
}else{
die("ERROR 0x001");
}
}
function substr($invoer){
for($i=0; $i<strlen($invoer); $i++){
$uitvoer[] = substr($invoer,$i,1);
}
return $this->calculate($uitvoer);
}
function calculate($invoer){
$pw=count($invoer) - 1;
for($i=0; $i<count($invoer); $i++){
$uitvoer[] = $invoer[$i] * pow(2, $pw);
$pw--;
}
return $this->addall($uitvoer);
}
function addall($invoer){
$uitvoer=0;
for($i=0; $i<count($invoer); $i++){
$uitvoer = $invoer[$i] + $uitvoer;
}
return $uitvoer;
}
}

But it would make more sense to just have all the code in the one main function.
RE: PHP Help by Ezra on 01-20-2006 at 05:41 PM

quote:
Originally posted by Plik
But it would make more sense to just have all the code in the one main function.

I splitted it this way, cause now every function has a different thing to do, one splits the string, the other calculates the decimal representation, and the other one adds everything together.

Later I will add code to convert to other systems like hexadecimal and back

And thanks for your help :)
RE: PHP Help by segosa on 01-20-2006 at 08:53 PM

PHP has a built in substr() function so why do you have your own?


RE: PHP Help by J-Thread on 01-20-2006 at 10:23 PM

quote:
Originally posted by Ezra
code:
...
        if(isset($invoer) && $invoer != ""){
...


Why not:

code:
...
        if(!empty($invoer)) {
...

Try to learn as much php functions as possible, most of the things you want are already build in...
RE: PHP Help by Ezra on 01-20-2006 at 10:31 PM

quote:
Originally posted by segosa
PHP has a built in substr() function so why do you have your own?

I used php's substr function, but I wanted to put all the characters in the array, so I used a loop to loop trough all the characters

quote:
Originally posted by J-Thread
Try to learn as much php functions as possible, most of the things you want are already build in...


I know php can already convert but I had to make a convertor for school.

Actually it was a project for VB.NET :P, but because I'm better with php I tried it with that, and after that I tried to make it OOP :)

I added decimal 2 binary btw, but now my ajax script doesn't work anymore :(