payment by upi: sinhamit@icici or payment by bank account name: amit kumar sinha, account number: 2646728782 IFSC code: KKBK0005660 SWIFT: KKBKINBB

Please support if you like my work by payment through upi: sinhamit@icici or payment by bank

account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB


Operators   in Category: PHP   by amit

🕙 Posted on 2023-05-27 at 14:05:34     Read in Hindi ...


Operators

    In previous page, you have seen some simple arithematic operation on floating point numbers, and introduction to other kinds of operators. You have also seen . (concate­nation) and .= (concate­nation assignment) operators in More about Strings topic. You have already knew about different data-types, and their function­alities in previous pages.

Let's dive deep into Various Kinds of Operators.

    You should have some basic knowledge of + (addition, i.e. 21 + 2 = 23), - (subtraction, i.e. 21 - 2 = 19), * (multiplication, i.e. 21 × 2 = 42), / (division, i.e. 21 / 2 = 10.5), % (remainder, i.e. 21 % 2 = 1 ), and ** (exponent, 21 ** 2 = 441). But, results in following examples will be different, because the COMPUTER MEMORY (RAM) stores each variable in specific location. When multiple operations are applied on one variable name, that is, $var2 in following example, the value of $var2 is updated every time.

Assignment Operators

    All Assignment Operators place the value of an expression (or a literal) to a variable/constant name, from right to left. It means that the left operand gets set to the value of the expression on the right. Anything after any of assignment operators, is an expression. PHP is an expression-oriented language, and almost everything is an EXPRESSION. You have already seen simple assignment operator in previous page, when a literal value is assigned to a variable name.

<?php $my_var = 34; // simple assignment operator ?>

<?php $new_var = $my_var; echo $new_var; // Outputs 34 ?>

<?php $other_var = "$my_var"; echo $other_var; // Outputs 34 ?>

    In above examples, $my_var is assigned with a literal value of 34. 34 is an expression, which evaluates to itself, and then assigned to $my_var. Also, $new_var and $other_var variables have been assigned with $my_var, either directly or within a pair of double quotes. Do not use a pair of single quotes! In second and third examples of above codes, $my_var is first evaluated, and then the value is assigned to respective variable names. In each line of above examples, ; (semi-colon) terminator is used, and thus, these are statements. In following example also, $var1 and $var2 are assigned with two literal values, 2 and 21, respectively.

<?php $var1 = 2; $var2 = 21; // Two variables of any integer value can be declared. ?>

<?php $var2 += $var1; // $var2 = $var2 + $var1;
  echo $var2; // 21 + 2 = 23 ?>
<br />

<?php $var2 -= $var1; // $var2 = $var2 - $var1;
  echo $var2; // Now, value of $var2 is 23, and therefore 23 - 2 = 21 ?>
<br />

<?php $var2 *= $var1; // $var2 = $var2 * $var1;
  echo $var2; // Now, value of $var2 is 21, and therefore 21 × 2 = 42 ?>
<br />

<?php $var2 /= $var1; // $var2 = $var2 / $var1;
  echo $var2; // Now, value of $var2 is 42, and therefore 42/2 = 21 ?>
<br />

<?php $var2 %= $var1; // $var2 = $var2 % $var1;
  echo $var2; // Now, value of $var2 is 21, and therefore 21 % 2 = 1 ?>
<br />

<?php $var2 **= $var1; // $var2 = $var2 ** $var1;
  echo $var2; // Now, value of $var2 is 1, and therefore 1 ** 2 = 1 ?>
<br />

If you are confused with above examples, consider following simple explanation:

<?php
  $a = 21 + 2; echo $a; // Outputs 23
  echo '<br />';
  $b = 21 - 2; echo $b; // Outputs 19
  echo '<br />';
  $c = 21 * 2; echo $c; // Outputs 42
  echo '<br />';
  $d = 21 / 2; echo $d; // Outputs 10.5
  echo '<br />';
  $e = 21 % 2; echo $e; // Outputs 1
  echo '<br />';
  $f = 21 ** 2; echo $f; // Outputs 441

  echo '<br />';   $x = 21;
  $x += 2;   echo $x; // Outputs 23
  echo '<br />';
  $x -= 2;   echo $x; // Outputs 21
  echo '<br />';
  $x *= 2;   echo $x; // Outputs 42
  echo '<br />';
  $x /= 2;   echo $x; // Outputs 21
  echo '<br />';
  $x %= 2;   echo $x; // Outputs 1
  echo '<br />';
  $x **= 2;   echo $x; // Outputs 1
  echo '<br />';
  $y += $x; echo $y; // Outputs ERROR as shown below:
?>


Warning: Undefined variable $y in C:\xampp\htdocs\php_project\phptest.php on line 27
1

    In above example, you can see that $a, $b, $c, $d, $e and $f are assigned with simple arithematic expressions, and the outputs are expected as we have thought. But, in practical world, you have to get data from users outside CMS (Content Management System) or outside the WEBSITE. You have to calculate age of a person (e.g. 18 years and above to have a legal contract), multiplying price with number of items, etc.

    Therefore, the variable $x has been first assigned with expression 21, and thereafter it is added with 2. Now, value of the variable $x is 23. Then, $x is subtracted from 2, and now its value is 21, and so on... In the last line of above example, you can see that $y has directly been applied with addition assignment operator, with value from $x, which outputs ERROR. You should note that only = (simple assignment) operator assigns literal value to a newly defined variable/constant name on left side of the statement. For other assignement operators, you have to first declare a variable name with some value, either 0 (zero), null, or "" (empty string), etc.

    In next page, you will learn about operator precedence and associativity of various OPERATORS. Operator Precedence decides which operator should be prefered over other operators when many operators are present in one expression. Associativity decides from which direction (left to right, or right to left) operators should be applied, when two or more operators have same level of precedence in one expression.

<?php $abc = $mno = 24;
  echo $abc, '<br />', $mno; // both variables output 24 each. ?>

    You may find the above code in some other programmer's scripts. This is not incorrect, but should be avoided, and you should not be confused when you may see this. Assignment operator has right associativity, which means that first literal value 24 is assigned to $mno, and then it is evaluated and assigned to variable name, $abc.


Leave a Comment: