Frage

I'm pretty new to PHP, especially object oriented php, and I'm working on some simple code, and I'm not quite sure what's wrong with what I've written so far. I'm sure it's something simple but I've beat my head against this wall for a little bit, figured I'd ask here.

class primes
{
$TestValues = array(0, 1, 2, 3, 4);
function IsPrime($number)
{

    //if number is a number, perform the rest of the tests, else, return -1 (error)
    if(is_numeric($number))
    {

        //if number is less than 0, return -1 (error)
        if($number < 0)
            return -1;
        //if number is 0, then return 0 (false, not prime)
        if($number == 0)
            return 0;
        //if number is greater than 1024, return -1 (error)
        if($number > 1024)
            return -1;
        //if number is 1, return 0 (false, not prime)
        if($number == 1)
            return 0;
        //if number is 2, return 1 (true, is prime)
        if($number == 2)
            return 1;
        //if number mod 2 is 0, then it is even, and no even number is prime except 2, which is handled above. so return 0 (false, not prime)
        if($number % 2 == 0)
            return 0;

        //if number has passed all previous tests, mod it by all odd numbers from 3 to its square root rounded up.
        for($i = 3; $i <= ceil(sqrt($number)); $i = $i +2)
        {
            //if any numbers mod 3 to its square root equal 0, return 0, (false, not prime)
            if($number % $i == 0)
                return 0;
        }
        //if the number has passed all above requirements, then it is a prime number below 1024.
        return 1;
    else
    {
        return -1;
    }
    }
}

function TestIsPrime()
{
    foreach($TestValues as $value)
        IsPrime($value);
        if(IsPrime() == 0)
            echo($value . "=> Is not Prime");
        elseif(IsPrime() == 1)   
            echo($value . "=> Is Prime");
        elseif(IsPrime() == -1)
            echo($value . "=> Is an Error");

}

 function main()
 {
     TestIsPrime();

  }

}
main();

I'm getting an error saying I don't place my array where it is currently. I'm not quite sure how the structure of php code is supposed to work with a class, so I wasn't sure where to put the $TestValues array, so I tried a few places, and none would be accepted. Also, I'm getting an error on the else statement connected to the first if(is_numeric($number)), but I couldn't be sure that the error wasn't caused by another small error I was getting. The last error is that I'm not sure where in this single page of code to call the functions inside the class. Any help would be appreciated. Once again, I'm new to doing anything useful in php, but I'm liking it so far. Thanks,

War es hilfreich?

Lösung

Your code was missing several braces, also you your $TestValues is part of the class, not a global variable.

I fixed the code, however you have to check the math and read the oop tutorial on php's site.

<?php
class Primes {
    public $TestValues = array(0, 1, 2, 3, 4);
    function IsPrime($number) {

        //if number is a number, perform the rest of the tests, else, return -1 (error)
        if(is_numeric($number)) {

            if($number < 0 || $number > 1024)
                return -1;
            if($number === 0 || $number === 1 || $number % 2 === 0)
                return 0;

            //if number has passed all previous tests, mod it by all odd numbers from 3 to its square root rounded up.
            for($i = 3; $i <= ceil(sqrt($number)); $i = $i + 2) {
                //if any numbers mod 3 to its square root equal 0, return 0, (false, not prime)
                if($number % $i == 0)
                    return 0;
            }
            //if the number has passed all above requirements, then it is a prime number below 1024.
            return 1;
         } else {
            return -1;
        }
    }

    function test() {
        foreach($this->TestValues as $value) {
            $t = $this->IsPrime($value);
            if($t === 0) {
                echo($value . "=> Is not Prime");
            } elseif($t === 1) {   
                echo($value . "=> Is Prime");
            } elseif(IsPrime() == -1) {
                echo($value . "=> Is an Error");
            }
            echo "\n";
        }
    }
}

function main() {
    $prime = new Primes();
    $prime->test();
}

main();

Andere Tipps

You have some syntax errors in your code. I made some modification to your code now it is working :

<?php

// Check the number to be prime or not
function IsPrime($number) 
{
    // If the variable is not numeric, negative or greater than 1024, exit
    if (!is_numeric($number) || $number < 0 || $number > 1024)
    {
    return -1;
    }
    // Perform the tests
    switch($number)
    {
    //if number is 0 or 1, then return 0 (false, not prime)
        case 0 :
        case 1 :
            return 0;
        break;
    //if number is 2, return 1 (true, is prime)
        case 2 :
            return 1;
        break;
    }

    //if number mod 2 is 0, then it is even, and no even number is prime except 2, return 0 (false, not prime)
    if($number % 2 == 0) 
    { 
    return 0;
    }

    //if number has passed all previous tests, mod it by all odd numbers from 3 to its square root rounded up.
    for($i = 3; $i <= ceil(sqrt($number)); $i = $i +2)
    {
        if($number % $i == 0) 
        {
            return 0;
        }
    }

    //if the number has passed all above requirements, then it is a prime number below 1024.
    return 1;
} // End of isprime function

// create an array of numbers
$testvalues = range(-2, 100);

foreach($testvalues as $value) 
{
    switch(isprime($value))
    {
        case 0 :
            echo("<p style='color:gainsboro;'>" . $value . "=> Is not Prime</p>");
            break;
        case 1 :
            echo("<p style='color:green;'>" . $value . "=> Is Prime</p>");
            break;
        case -1 :
            echo("<p style='color:red;'>" . $value . "=> Is an Error</p>");
            break;
    }
}
?>

Notes :

  • I think you don't need a class here. It is a simple program and using classes will make it a bit complex and php is known for its simplicity. So keep it simple.

  • In some cases switch is better and more readable than if, so use it if possible http://php.net/manual/en/control-structures.switch.php

  • Every language has it preferred syntax. In php it is common not to use capitals in naming variable (except for classes) unlike other languages

  • there is no main() in php

Hope this helps, excuse my english and correct me if I am wrong

The reason why u getting error for else statement is because of the closing braces } of your if statement, notice that you have a closing brace after your else condition,instead of doing that, you have to close if statement first then do else statement which in your case else means if is not numeric then do sth, also you have so many if statements which looks a little bit redundant. what you could do is to put them in same condition like if($number ==0 || $number == 2 || $number % 2 ==0 ) return 0 elseif // do sth

The reason why you can't get $Testvalues is that you need to pass the array to your function then you can use it, where did u call your TestIsPrime function? when you call it, do sth like TestIsPrime($TestValues);

btw, it's better not to use Capital letter for variable...

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top