我的IF语句正在改变PHP中的变量

I am fairly new to the whole programming thing, so forgive me if this is a stupid question. It seems odd that I haven't run into it before.

I am trying to make an order form for a cake. You fill out the form, submit it, and it will then display the order in a new window, where you then hit "submit," and upload it to the Database. I have a series of If Statements to check for errors in the form before submitting it. Here is a simplified version of the code. Writing means any writing you want on the cake, Name is your name, and cake is what type of cake you want (the default is "None").

try {
$name = trim($params->name);
$cake = trim($params->cake);
$writing = trim($params->writing);
if (strlen($name) < 3){
    throw new Exception("Please enter Your name.");
    }
if ($cake = "None") {
        throw new Exception("Please select a Cake"
     }
if ($cake = "Caramel Apple Pie" or $cake = "Pumpkin Pie" or $cake = "Eggnog Pie" and strlen($writing) > 1) {
    throw new Exception("We are sorry, but you can't write on any of our specialty pies.");
    }

} catch(Exception $x) {
    $error = $x->getmessage();
    }

So what is happening is that when I go and hit submit the first time, the correct cake type comes up, but when you submit it the second time, the error comes up saying that I have "None" selected. All the other values are there and remain the same. I think the problem is that the first "IF" statement (Where it says "If($cake = "None")) is automatically changing $cake to "None" because I have tried commenting just that statement out, and it will then change the cake to be "Caramel Apple Pie," which is in the top of the next IF statement.

Anyone know why it is doing this? And how to fix it?

You are using a = instead of a ==

  • = is an assignment operator, ie it changes our variable value. Using it inside an if-statement results in setting the variable and checking the result.
  • == is a comparison operation
  • === is also a comparison operator, it additionally checks whether the types are equal.

See the PHP assignment operator and comparison operator manuals for more information

Additional notes:

  • the line throw new Exception("Please select a Cake" is missing ); as well.
  • using exceptions to write a message in $error is a bit overkill in your example situation! For example, just use $error = "Please enter Your name.".

The problem is that you are using an assignment operator. Change your code to this:

if($cake == "None")

= Operator:

In problem languages = represents an assignment. You are assigning a value to a particular variable.

I.E.
$truck = "Ford";
//$truck has now been assigned the value of "Ford"

== Operator:

The == is known as an equality. This checks to see if the values and or variables you are comparing are equal.

I.E.
$truck = "Ford";
if($truck == "Ford") //returns true

= is assignment. == is a test for equality.

Some programmers write their equality tests like this to catch this error:

if ( 'foo' == $x ) 

because if you have this

if ( 'foo' = $x )

that's a syntax error because you can't assign $x to a string literal.