-1

The error is probably something stupid, but I can't find the mistake to save my life.

I have this piece of code in my program:

$validate = (
  $num_rows > 1 || 
  $num_rows == 0);

if ($validate) {
  $data['code'] = 2;
} else {
  $data['code'] = 1;
  while ($a = $resultados->fetch_assoc()) {
    $data['response'] = $a;
  }
}

I 'believe' its written correctly, however when ran, I'm getting the error 500 and the logs read:

Syntax error: Unexpected '$num_rows' (T_VARIABLE) on line 21...

Line 21 is the second validation for $num_rows.

Any suggestions?

Thanks in advance!

  • 3
    can you write what you want to do with this code cause it is impossible to figure it out – Omis Brown Sep 04 '17 at 15:20
  • 2
    There are no syntax errors in the code you've posted, can you add a few lines before and after? – iainn Sep 04 '17 at 15:20
  • `$validate = ($num_rows > 1||$num_rows == 0);` Try this, sometimes the space between `||` and the second validation make a strange syntax error – Mcsky Sep 04 '17 at 15:24
  • this is db-related; you've no code to support it and what @OmisBrown said; is just that: *impossible* to answer. – Funk Forty Niner Sep 04 '17 at 15:27
  • @iainn maybe a duplicate, but the answer Mcsky gave me is not listed in that article, and that's what did the trick. – Lalo Santos Sep 04 '17 at 15:27
  • @Mcsky please post it as an answer so I can accept it. – Lalo Santos Sep 04 '17 at 15:28
  • what you're wanting to do here is use a conditional statement or ternary operator; what you have now doesn't really do anything. – Funk Forty Niner Sep 04 '17 at 15:28
  • 1
    @LaloSantos There's no difference at all between your code and that comment. *sometimes the space between || and the second validation make a strange syntax error* - that just isn't true. But at least it's working. – iainn Sep 04 '17 at 15:29
  • I have to agree with @iainn ^ that space shouldn't be an issue. – Funk Forty Niner Sep 04 '17 at 15:30
  • I posted an answer, thank guys :) – Mcsky Sep 04 '17 at 15:31
  • I edited the question to show more context, I don't disagree with you that the space should have nothing to do with it, but for some reason it worked with that modification. – Lalo Santos Sep 04 '17 at 15:31
  • Aside: Perhaps the condition would make more sense as `$num_rows != 1`? Assuming it can never be negative. And since you only seem to be fetching the result if there's one row, you don't need the `while` loop either. – deceze Sep 04 '17 at 15:36
  • Please give us your IDE version :) – Mcsky Sep 04 '17 at 15:40
  • Please can you just tell us what you want to do ? something like : I want to test the variable if it is grather than 1 or equals to 0 or something like that – Omis Brown Sep 04 '17 at 15:41
  • @Mcsky I'm using Atom. And yes, re-writing the condition also solved the problem. But I didn't think it should fail even with how it was originally written. The condensed version of what I need is the one specified by you: $validate = $num_rows >= 0; – Lalo Santos Sep 04 '17 at 16:08

2 Answers2

1

I think you just forgot to put a semicolon at the end of the code line that comes before your line starts with $validate = ...

Would be awesome to see the lines above this code snippet.

Link
  • 29
  • 5
-1
$validate = ($num_rows > 1||$num_rows == 0); 

Try this, sometimes the space between || and the second condition make a strange syntax error. I'm curious, which IDE/text editor are you using.

I think it isn't a PHP core problem, but it come from something else, please give us more context :)

By the way, you could replace your condition by

$validate = $num_rows >= 0;

it's exactly the same and you avoid your problem

Mcsky
  • 1,426
  • 1
  • 10
  • 20
  • Can you show an example where this happens? Because if so, it's a bug in PHP's core, and should be reported as such. – iainn Sep 04 '17 at 15:32
  • Nop I can't, maybe it isn't a php problem, perhaps an IDE. – Mcsky Sep 04 '17 at 15:36
  • I'm not using and IDE, I use ATOM to build the code, and then run it on php. I had actually had this issue before, but ended re-writing my condition to prevent it. – Lalo Santos Sep 04 '17 at 16:06
  • I had this issue on sublime text and PHP Storm. It seem to be it isn't an editor problem, very strange – Mcsky Sep 05 '17 at 07:08