Go to GoReading for breaking news, videos, and the latest top stories in world news, business, politics, health and pop culture.

The PHP Equals Function

104 14

    Assignment

    • The equal sign functions as an assignment operator in PHP. PHP computes the value to the right of the equal sign and assigns that value to the variable on the left of the equal sign. For example:

      $x = 100;

      $name = "John";

      $result = max($a, $b);

      The variable "x" is assigned the value 100, the variable "name" is assigned the value "John" and the variable "result" is assigned the larger of the value in the variable "a" and the value in the variable "b."

    Assignment and Operation

    • Combine the equal sign with an arithmetic operator, such as the operators used for addition, subtraction, multiplication, division and modulus, to perform a calculation during assignment automatically. For example, the statement:

      $x += 20;

      adds 20 to the current value of the variable "x."

      This also works for string variables. For example, the statement:

      $first_name .= ' ' + $last_name;

      adds a space and the value in the "last_name" variable to the end of the "first_name" string.

    Comparison

    • Use two equal signs to make comparisons between values or variables. For example, the statement:

      if ($x == $y) echo "Equal";

      will compare the values contained in the variables "x" and "y". Create a "not equal to" expression by using the exclamation point ("not") operator with one equal sign. For example:

      if ($name != "Tom") echo "You are not Tom!";

      Be careful not to use one equal sign when making a comparison, because that comparison will be true unless the value is zero. For example, the statement:

      if ($x = $y) echo "This is true unless y is zero";

      assigns the value in the variable "y" to the variable "x" and then evaluates that value as either true or false.

    Identical Comparison

    • Use three equal signs to make an identical comparison. This is especially helpful when evaluating expressions for "true" or "false," because zero evaluates to "false" in PHP and any non-zero number evaluates to "true." By using three equal signs, only the value "false" will evaluate to "false." For example:

      $x = false;

      $y = 0;

      if ($x === false) echo "X is false";

      if ($y == false) echo "Y is false using two equal signs";

      if ($y === false) echo "Y is not false using three equal signs";

Source...

Leave A Reply

Your email address will not be published.