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"; }
  }
9
  • 2
    specify which error did you get. Commented Mar 31, 2010 at 9:27
  • 1
    Care to post here what error you've got? Commented Mar 31, 2010 at 9: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"; } Commented Mar 31, 2010 at 9:31
  • 3
    You've just changed the question. That's really not helpful. Commented Mar 31, 2010 at 9:32
  • 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 >=. Commented Mar 31, 2010 at 9:35

10 Answers 10

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

1 Comment

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

Comments

2

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

Comments

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.

3 Comments

You probably mean switch(true) { /* rest of your code goes here */ }, though, right?
ah right tnx, wouldve been a nice bug for later if range ever hits "0" :)
wow, I developed in PHP for years but i never saw this use of the switch statement, it's coool ! thx ;)
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.

Comments

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?

Comments

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

Comments

0

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

Comments

-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"; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.