0

I'm having some problems with a Return statement in PHP. The thing is that, no matter what happend inside my function I always get a false value out of the function. I really think that is because of the Return statement because I try to use it in other functions and I don't get a diferent value.

public function valid_token () { if (!isset($_SESSION['token']) || $this->token != $_SESSION['token']) { $this->errors[] = "Formulario incorrecto"; } return count($this->errors)? 0 : 1; } 

Out of this function I always get a false value (0). The same happends when i call:

public function valid_data () { if (empty($this->sectorName) || empty($this->sectorInfo) || empty($this->sectorCat)) { $this->errors [] = "Datos incorrectos"; } return count($this->errors)? 0 : 1; } 

Of course, I call both functions when I have already sent the form and have set the token.

0

2 Answers 2

1

Because you are just counting not checking,Try this

return count($this->errors) > 0 ? 0 : 1; 
Sign up to request clarification or add additional context in comments.

3 Comments

I even think count is lengthy.
You're wrong. count() returns a number. 0 evaluates to false, and any number different from 0 evaluates to true. And since count() doesn't return negative numbers, your check is unnecessary.
See this: ideone.com/BXnYiB I'm sure there are another reasons.
0

More simply,

return !$this->errors; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.