# Sanjib Sinha

Members

97

1. ## To test a prime number

Thanks IThinkMyBrainHurtsI guessed something like this. But why should I go for such complexity? Please consider this code to test the primality: function is_prime(\$num) { for(\$i=2; \$i<= (int)sqrt(\$num); \$i++){ if(\$num % \$i == 0) { return false; } } return true; } if(is_prime(119)){ echo 'Prime'; } else { echo 'Not Prime'; } //output not prime
2. ## To test a prime number

I had searched web and found some of them very interesting and few of them seem complex like this one in wiki: function isPrime(\$n) { if (\$n <= 3) { return \$n > 1; } else if (\$n % 2 === 0 || \$n % 3 === 0) { return false; } else { for (\$i = 5; \$i * \$i <= \$n; \$i += 6) { if (\$n % \$i === 0 || \$n % (\$i + 2) === 0) { return false; } } return true; } } Specially this part: for (\$i = 5; \$i * \$i <= \$n; \$i += 6) { if (\$n % \$i === 0 || \$n % (\$i + 2) === 0) { return false; } } It started from 5. It is okay. But the logic flow seems pretty complex. Can anyone translate in simple English actually what is happening?
3. ## To test a prime number

I have written a function to check whether any number is prime or not. Is it okay? or I can better it by any means? function isPrime(\$num){ \$number = array(); for (\$i=2; \$i <= \$num; \$i++){ if((\$num%2)==0){ continue; } if ((\$num%\$i)==0){ break; } \$number[]=\$i; } /* * how I back calculate to make it successful foreach (\$number as \$key => \$value) { echo "\$key = \$value <br>"; } echo count(\$number); */ if (count(\$number)== (\$num-2)) { echo 'it is prime'; } else { echo 'not prime'; } } isPrime(101112345909);
4. ## OOPHP - instantiatiing a class from within another class

To learn it more please google 'type hinting'. I think it will help a lot.

Please check whether the root path is correctly defined or not. I think there is some problem.
6. ## Help with classes - Can not get it to work

AS you said: It works if I remove the class.. just not WITH the class.. I think you would better write the class again. Keeping in mind, every class should have single responsibility. It is also not a good practice to hit the database directly from your class where you are displaying data so you can have it encapsulated. And finally as NotionCommotion said create an instance. Best Wishes.

It can be done. The site will be role based. Admin from his dashboard can control everything. You need to create a proper database table structure for doing this.

9. ## Framework Suggestion

Yes, I am agreed with Adam. To start with and getting acquainted with the MVC approach, CodeIgniter is fine. Hopefully it will catch up with the advancement taking place in the PHP world; but, till then you can start with CI.
10. ## Wordpress and PHP

Tons of Basic WordPress video tutorials are there in you tube. For the beginners I think, video tutorials make lasting impacts primarily. Please try it before you go down writing your own themes and plugins.
×

• #### Activity

• Chat
×
• Create New...