PHP变量在if语句中被意外覆盖的问题求助
Got it, let's sort out this issue you're facing! The problem is exactly what you suspected: in your if statement, you're using a single equals sign (=), which is an assignment operator instead of a comparison operator.
What's Happening Now
When you write if ($table = "1"), you're actually setting the value of $table to "1" every time this line runs. The result of an assignment expression is the value being assigned, so this evaluates to "1"—which PHP treats as a truthy value, making the if block always execute, no matter what the original $_POST['origintable'] value was.
The Fix
You need to use a comparison operator instead. There are two common options in PHP:
==: Loose equality (checks if values are the same, allowing type conversion)===: Strict equality (checks both value and type match, which is safer to avoid unexpected type-related bugs)
Here's the corrected code:
<?php print_r($_POST); $table = $_POST['origintable']; $database = $_POST['origindatabase']; // Use === for strict comparison (recommended) if ($table === "1") { echo "<br> Database name: <b>$database</b> Table name:<b>$table</b>...."; } ?>
Why Strict Equality is Better
If you use ==, PHP might do implicit type conversion (e.g., the number 1 would match the string "1"). Using === ensures that only when $table is exactly the string "1" (same value and same type) will the condition pass, which is usually what you want when checking form input values.
内容的提问来源于stack exchange,提问作者PythonNoobie

