在if语句PHP中传递变量

I have a bunch of if statements and they have different filters based on info passed.

Later in the code i also need to re-use that filter in an IF statement, here is the variable

$over5hours = "$workextra->id > 767 && $workextra->id <=1301";

Now this is my if statement

if($userid==$workextra->ownerId && $workextra->hoursDone >= 5 && $over5hours)

But that over5hours variable only displays this: >767 && <1301 instead of writing the whole string in the IF statement.

Other than converting that variable to an array is there an easier way to do this, i dont want to have to write 2x the IF statements as it will bloat my already terrible newbie code. Any suggestions?

Assuming I understand your goal, you can simply remove the quotes around the value of $over5hours.

$over5hours = $workextra->id > 767 && $workextra->id <=1301;

I could be misunderstanding your goal, however.

You cannot interpolate statements like that. ... && $over5hours does not evaluate $over5hours as an expression; it's just a string and is the same as $anyRandomString == true. The best you can do is evaluate the expression into a boolean value first and use that in the if statement:

$over5hours = $workextra->id > 767 && $workextra->id <=1301;
if ($userid == $workextra->ownerId && $workextra->hoursDone >= 5 && $over5hours)

If you use single quotes instead of double quotes, the variables will not be calculated.

Also, if the value for $workextra->id has not changed, you could simply leave out the quotation marks.

You need to use single quotes or escape the $ in order to get it to print. but besides that, you will need to use eval() to get this code to run the way you want. But it is bad practice to use eval(). You should probably find another approach.