2

I keep getting an error with the following bit of code. It is probably some small thing but I don't see what is wrong.

while($row = mysql_fetch_array($result))
  {
   $varp = $row['ustk_retail'];
   if ($varp<80000) { $o1 = 1; }
   if (($varp=>80000) && ($varp<100000)) { $o2 = "1"; }
   if (($varp=>100000) && ($varp<120000)) { $o3 = "1"; }
   if (($varp=>120000) && ($varp<140000)) { $o4 = "1"; }
   if (($varp=>140000) && ($varp<160000)) { $o5 = "1"; }
   if (($varp=>160000) && ($varp<180000)) { $o6 = "1"; }
   if (($varp=>180000) && ($varp<200000)) { $o7 = "1"; }
   if (($varp=>200000) && ($varp<220000)) { $o8 = "1"; }
   if (($varp=>220000) && ($varp<240000)) { $o9 = "1"; }
   if (($varp=>240000) && ($varp<260000)) { $o10 = "1"; }
   if (($varp=>260000) && ($varp<280000)) { $o11 = "1"; }
   if (($varp=>280000) && ($varp<300000)) { $o12 = "1"; }
   if ($varp>=300000) { $o13 = "1"; }
  }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    specify which error did you get. – zerkms Mar 31 '10 at 09:27
  • 1
    Care to post here **what** error you've got? – Your Common Sense Mar 31 '10 at 09:27
  • I get a parse error - Parse error: parse error in C:\wamp\www\wp\wp-content\themes\canvas\includes\usedprice.php on line 35 Line 35 is - if (($varp=>80000) && ($varp<100000)) { $o2 = "1"; } –  Mar 31 '10 at 09:31
  • 3
    You've just changed the question. That's really not helpful. – Skilldrick Mar 31 '10 at 09:32
  • Parse error: syntax error, unexpected T_DOUBLE_ARROW. It's >= not => – Gordon Mar 31 '10 at 09:33
  • 3
    Ok, posing a question and then changing it completely is not helpful. Maybe you could first learn PHP syntax and then start asking questions. You could start by replacing every => into >=. – wimvds Mar 31 '10 at 09:35
  • Sorry - it was the =>. Thank you for the help. –  Mar 31 '10 at 09:43
  • You are right. It's small but hard to find mistake. Good question. See an opdate to my post. Hope it helps – Your Common Sense Mar 31 '10 at 09:53
  • Just a comment about performance.. you'd be better off using "if {} elseif{} .." .. as only one condition will ever match, there isn't any point trying every condition every time – cEz Mar 31 '10 at 10:19

10 Answers10

27

Running php -l (lint) on your code I get a

Parse error: syntax error, unexpected T_DOUBLE_ARROW

The T_DOUBLE_ARROW token is what PHP expects when assigning array values to array keys.

When comparing for Greater than or equal to the PHP Parser expects T_IS_GREATER_OR_EQUAL, meaning you have to use >= instead of =>.

See

Gordon
  • 312,688
  • 75
  • 539
  • 559
  • 2
    +1 and even when you are not familiar with all the tokens, `T_DOUBLE_ARROW` is a quite descriptive name. Sad that there are people who need to split their code into several lines in order to understand that. (It would be quite understandable if they got `unexpected T_PAAMAYIM_NEKUDOTAYIM` though...) – NikiC Oct 18 '10 at 19:22
11

Greater than or equal to is >= sign, not =>

Update:
You are right. It's small but hard to find mistake.
It took me to split whole line into pieces to see where the problem is:

<?php
if 
(
$varp
=>
80000
)

So, it says parse error on line 5 and I had to doublecheck this operator.
Of course, at first I separated the problem line from the rest of the code to be certain.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 11
    Actually, if you know what the T_DOUBLE_ARROW mentioned in the error message refers to, the error is obvious and easy to find. See the linked list of parser tokens in my answer. – Gordon Mar 31 '10 at 10:49
3

You have an expression error.

$varp=>220000 // is not a valid php expression

=> operator is used to assign values in arrays like:

$x = array( 'foo' => 'bar');

>= is the comparation assigment greater than or equal
Elzo Valugi
  • 27,240
  • 15
  • 95
  • 114
2

This is more readable and compact way to do the same:

$ranges = range(300000, 80000, -20000);

$index = 1;

$varp = 220001;

foreach ($ranges as $i => $range) {
    if ($varp >= $range) {
        $index = 13 - $i;
        break;
    }
}

${'o' . $index} = 1;

Anyway - I think you're doing something wrong with using variable name of result.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
zerkms
  • 249,484
  • 69
  • 436
  • 539
2

The answer has already been given but thought this was neat enough to share:

PHP accepts boolean expressions in it's switch statement.

switch(TRUE) {
    case $range <= 10:  echo "range below or equal to 10"; break;
    case $range <= 20:  echo "range above 10 below or equal to 20"; break;
    case $range <= 30:  echo "range above 20 below or equal to 30"; break;
    default: echo "high range";
}

In my opinion this generates the cleanest most readable code.

Gordon
  • 312,688
  • 75
  • 539
  • 559
Les
  • 2,316
  • 16
  • 17
2

You have made a mistake in the if conditions. The greater than Equal to sign is >= and not =>.

Joy
  • 1,609
  • 3
  • 16
  • 28
1

You probably want to change ($varp=300000) to ($varp==300000) and it might help to enclose the full if-statement inside (), like this

if($varp80000 && $varp100000 && $varp120000 && $varp140000 && $varp160000 && $varp180000 && $varp200000 && $varp220000 && $varp240000 && $varp260000 && $varp280000 && $varp==300000) { $o13 = "1"; }

On another note, where to these strange $varp#### variables come from?

Htbaa
  • 2,319
  • 18
  • 28
1

Not sure whether the code you've posted has gotten messed up somehow, but it looks like you're missing "==" in some of the if conditions. Also, as Skilldrick pointed out, the whole if condition should be in parentheses

Gary
  • 926
  • 3
  • 12
  • 24
0

"Greater than or equal to is >= NOT =>. You use => for arrays for keys/values.

ggfan
  • 2,472
  • 8
  • 36
  • 50
-1

Add one more bracket around the conditions in if....

if ( ($varp80000) && ($varp100000) && ($varp120000) && ($varp140000) && ($varp160000) && ($varp180000) && ($varp200000) && ($varp220000) && ($varp240000) && ($varp260000) && ($varp280000) && ($varp=300000) ) { $o13 = "1"; }

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
raj
  • 3,769
  • 4
  • 25
  • 43