What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » PHP Help

PHP Help
Author: Message:
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. PHP Help
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 :(

This post was edited on 01-20-2006 at 05:12 PM by Ezra.
[Image: 1-0.png]
             
01-20-2006 05:11 PM
Profile PM Web Find Quote Report
Plik
Veteran Member
*****

Avatar

Posts: 1489
Reputation: 46
34 / Male / –
Joined: Jun 2004
RE: PHP Help
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.
01-20-2006 05:24 PM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP Help
So, how do I return the last value to where I called main in my php script?
[Image: 1-0.png]
             
01-20-2006 05:30 PM
Profile PM Web Find Quote Report
Plik
Veteran Member
*****

Avatar

Posts: 1489
Reputation: 46
34 / Male / –
Joined: Jun 2004
RE: PHP Help
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.

This post was edited on 01-20-2006 at 05:36 PM by Plik.
01-20-2006 05:36 PM
Profile PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP Help
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 :)

This post was edited on 01-20-2006 at 05:44 PM by Ezra.
[Image: 1-0.png]
             
01-20-2006 05:41 PM
Profile PM Web Find Quote Report
segosa
Community's Choice
*****


Posts: 1407
Reputation: 92
Joined: Feb 2003
RE: PHP Help
PHP has a built in substr() function so why do you have your own?
The previous sentence is false. The following sentence is true.
01-20-2006 08:53 PM
Profile PM Find Quote Report
J-Thread
Full Member
***

Avatar

Posts: 467
Reputation: 8
– / Male / –
Joined: Jul 2004
RE: PHP Help
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...
01-20-2006 10:23 PM
Profile E-Mail PM Find Quote Report
Ezra
Veteran Member
*****

Avatar
Forgiveness is between them and God

Posts: 1960
Reputation: 31
37 / Male / Flag
Joined: Mar 2003
O.P. RE: PHP Help
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 :(


This post was edited on 01-20-2006 at 10:34 PM by Ezra.
[Image: 1-0.png]
             
01-20-2006 10:31 PM
Profile PM Web Find Quote Report
« Next Oldest Return to Top Next Newest »


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On