# Sanjib Sinha

Members

97

## Posts posted by Sanjib Sinha

1. ### To test a prime number

Thanks

IThinkMyBrainHurts

I 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.

What Psycho said, is true. Client Side validation is always dangerous. But Server side also can bring some trouble if you don't take proper guard.

I just thought how it would be to use a very simple 'Validate' class. I used trait and interface so that you could hook more functionality later. First the validate.php code:

```<?php

/*
* .
*/

trait ValidateTrait {
public function checked() {
return "Okay, Your Data has been saved";
}
public function unchecked() {
return "You have not provided proper data.";
}
}

interface ValidateInterface {
public function make(\$value);
}

class Validate implements ValidateInterface {
use ValidateTrait;

public \$_value = array();

public function make(\$value) {

\$this->_value = \$value;

if (strlen(\$value) === 0 || strlen(\$value) < 3 || strlen(\$value) > {
return FALSE;
}
elseif(is_string(\$value) && trim(\$value) === ''){
return FALSE;
}
elseif (is_array(\$value)) {
return FALSE;
}

elseif (preg_match("/\@.\/i", \$value)) {
return FALSE;
}

return TRUE;
}
}

```

Second the form.php:

```<form method="POST" action="action.php" accept-charset="UTF-8">
<input name="_token" type="hidden" value="EMnZhqPbryk7oVPcrjwxuTrlHto">

<p>
<p>

<p>
<input name="email" type="text" value="your email" id="email">
<p>
<input type="submit" value="Register">

</form>
```

and finally the action.php where I put the message of validation:

```<?php

/*
*
*/
require 'validate.php';

if (\$_POST['_token'] === "EMnZhqPbryk7oVPcrjwxuTrlHto"){

\$validate = new Validate();
if (\$validate->make(\$value[0]) && \$validate->make(\$value[1])){
echo \$validate->checked();
}
else {
echo \$validate->unchecked();
}
}
else {

echo 'You Crackers! Go back!';

}
```

You can obviously add many more fields to it as you need. I just checked username and email. username field can not be blank, and between 3 to 8 characters etc.

Best wishes to all.

9. ### Framework Suggestion

Im also looking for a framework,  but Laravel seems too fast moving for me.  I want something with a relatively long lifecycle per release.  I don't have time to relearn stuff every couple of months.

Any suggestions.  I need really lightweight,  shallow learning curve,  keeps out of the way,  but comes with a nice feature set that I can use if I want to.  I find out of the box Auth never does what I want ect so don't want to be forced to use something.

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...