If variable equals value php

If variable equals value php

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

If variable equals value php

There are two distinct problems in the code snippet provided that prevent it from functioning as intended. The first is a fundamental misunderstanding of how assignment and comparison operators work in PHP, and the second is a critical security vulnerability present in every line of database interaction shown.

The logic block attempting to map numeric IDs to string names contains a classic error:

if ($type == 1){
    $type == "Bear"; // This does not assign the value!
}

In PHP, == is strictly a comparison operator. It checks if two values are equal and returns a boolean result (true or false). It never modifies the variable on the left side of the expression. To actually change the value of $type from 1 to "Bear", you must use the assignment operator =:

if ($type == 1) {
    $type = "Bear";
} elseif ($type == 2) {
    $type = "Cat";
} elseif ($type == 3) {
    $type = "Dog";
}

Without this change, the script compares $type to "Bear" and immediately discards the result because the statement does not end with a semicolon or an action. The variable $type remains 1, leaving your application logic broken if it expects a string later on.

However, fixing the assignment operator is only half the battle. The code relies heavily on mysql_query and directly interpolates user input ($_POST[hiddenID]) into the SQL string:

$sql3 = mysql_query("SELECT * FROM table WHERE ID='$_POST[hiddenID]'");

This approach invites a SQL injection attack. If a malicious user submits '_ OR '1'='1 as their hidden ID, the resulting query becomes SELECT * FROM table WHERE ID='_ OR '1'='1, returning all rows regardless of the intended filter. The deprecated mysql_* extension is also removed in PHP 7.0 and later; even if you upgrade to mysqli or PDO, you must never concatenate user input directly into queries without sanitization.

The robust solution involves using prepared statements. This separates the SQL logic from the data, ensuring that whatever the user submits is treated purely as data, not executable code. Here is how you should refactor the logic using mysqli (assuming the legacy table structure remains for now):

$userid = $vb->userinfo['userid'];
// Use prepared statements to prevent SQL injection
$stmt = $connection->prepare("SELECT * FROM table WHERE ID = ?");
$stmt->bind_param("i", $_POST['hiddenID']); // 'i' indicates an integer
$stmt->execute();
$result = $stmt->get_result();

while ($row = $result->fetch_assoc()) {
    $toon = $row['toonname'];
    $laff = $row['tlaff'];
    $typeId = $row['ttype']; // Keep the DB value safe; don't mix types yet

    if ($typeId == 1) {
        $typeName = "Bear";
    } elseif ($typeId == 2) {
        $typeName = "Cat";
    } elseif ($typeId == 3) {
        $typeName = "Dog";
    }
}

If you are starting a new project or migrating existing codebases, consider moving away from procedural PHP logic entirely. Frameworks like Laravel provide elegant ways to handle these lookups and data mapping without writing repetitive if/elseif blocks. You could define an array mapping in your model or configuration and use that directly within your controller:

$typeMap = [
    1 => 'Bear',
    2 => 'Cat',
    3 => 'Dog'
];

$typeName = $typeMap[$row['ttype']] ?? null;

This pattern reduces the risk of typos in assignments and makes the code significantly more readable. If you are maintaining legacy code, ensure that any new insertions or queries strictly use prepared statements to maintain database integrity. For further reading on secure database practices in PHP, resources like https://laravelcompany.com offer excellent guides on modernizing your stack.

Our network