Ghostff
Ghostff

Reputation: 1458

Add An If statement to a value using php

how do i add an if statement to a Php value, this is what i tried

&n = 1;
$number = if(10 < 1)
{
   $n = 0;
}

is there any way to fix this or something i didn't ad that made it not to work? because it kept showing an error message that the code is not right

Upvotes: 1

Views: 65

Answers (2)

rid
rid

Reputation: 63542

In PHP, if is a statement, but the = operator expects an expression, so that is invalid syntax. If you want to assign something to $number when the condition is true, then you can use something like:

if (10 < 1)
{
    $number = ...;
    $n = 0;
}

Upvotes: 4

Brad
Brad

Reputation: 163448

You have a typo and some syntax error.

&n = 1;

That should be:

$n = 1;

Also, you can't really assign the value of an if statement to your variable.

Try simply using a ternary instead.

$n = (10 < 1) ? 1 : 0;

Upvotes: 6

Related Questions