Html Assignment 2 Operations

PHP 5 Operators

❮ PreviousNext ❯


PHP Operators

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

OperatorNameExampleResultShow it
+Addition$x + $ySum of $x and $yShow it »
-Subtraction$x - $yDifference of $x and $yShow it »
*Multiplication$x * $yProduct of $x and $yShow it »
/Division$x / $yQuotient of $x and $yShow it »
%Modulus$x % $yRemainder of $x divided by $yShow it »
**Exponentiation$x ** $yResult of raising $x to the $y'th power (Introduced in PHP 5.6) 

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.



PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

OperatorNameExampleResultShow it
==Equal$x == $yReturns true if $x is equal to $yShow it »
===Identical$x === $yReturns true if $x is equal to $y, and they are of the same typeShow it »
!=Not equal$x != $yReturns true if $x is not equal to $yShow it »
<>Not equal$x <> $yReturns true if $x is not equal to $yShow it »
!==Not identical$x !== $yReturns true if $x is not equal to $y, or they are not of the same typeShow it »
>Greater than$x > $yReturns true if $x is greater than $yShow it »
<Less than$x < $yReturns true if $x is less than $yShow it »
>=Greater than or equal to$x >= $yReturns true if $x is greater than or equal to $yShow it »
<=Less than or equal to$x <= $yReturns true if $x is less than or equal to $yShow it »

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

OperatorName DescriptionShow it
++$xPre-incrementIncrements $x by one, then returns $xShow it »
$x++Post-incrementReturns $x, then increments $x by oneShow it »
--$xPre-decrementDecrements $x by one, then returns $xShow it »
$x--Post-decrementReturns $x, then decrements $x by oneShow it »

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.


PHP String Operators

PHP has two operators that are specially designed for strings.

OperatorNameExampleResultShow it
.Concatenation$txt1 . $txt2Concatenation of $txt1 and $txt2Show it »
.=Concatenation assignment$txt1 .= $txt2Appends $txt2 to $txt1Show it »

PHP Array Operators

The PHP array operators are used to compare arrays.

OperatorNameExampleResultShow it
+Union$x + $yUnion of $x and $yShow it »
==Equality$x == $yReturns true if $x and $y have the same key/value pairsShow it »
===Identity$x === $yReturns true if $x and $y have the same key/value pairs in the same order and of the same typesShow it »
!=Inequality$x != $yReturns true if $x is not equal to $yShow it »
<>Inequality$x <> $yReturns true if $x is not equal to $yShow it »
!==Non-identity$x !== $yReturns true if $x is not identical to $yShow it »

❮ PreviousNext ❯

JavaScript Operators Reference

❮ PreviousNext ❯


JavaScript operators are used to assign values, compare values, perform arithmetic operations, and more.


JavaScript Arithmetic Operators

Arithmetic operators are used to perform arithmetic between variables and/or values.

Given that y = 5, the table below explains the arithmetic operators:

For a tutorial about arithmetic operators, read our JavaScript Arithmetic Tutorial.


JavaScript Assignment Operators

Assignment operators are used to assign values to JavaScript variables.

Given that x = 10 and y = 5, the table below explains the assignment operators:

For a tutorial about assignment operators, read our JavaScript Assignment Tutorial.



JavaScript String Operators

The + operator, and the += operator can also be used to concatenate (add) strings.

Given that text1 = "Good ", text2 = "Morning", and text3 = "", the table below explains the operators:

OperatorExampletext1text2text3Try it
+text3 = text1 + text2 "Good ""Morning" "Good Morning"Try it »
+=text1 += text2 "Good Morning""Morning"""Try it »

Comparison Operators

Comparison operators are used in logical statements to determine equality or difference between variables or values.

Given that x = 5, the table below explains the comparison operators:

For a tutorial about comparison operators, read our JavaScript Comparisons Tutorial.


Conditional (Ternary) Operator

The conditional operator assigns a value to a variable based on a condition.

SyntaxExampleTry it
variablename = (condition) ? value1:value2 voteable = (age < 18) ? "Too young":"Old enough";Try it »

Example explained: If the variable "age" is a value below 18, the value of the variable "voteable" will be "Too young", otherwise the value of voteable will be "Old enough".


Logical Operators

Logical operators are used to determine the logic between variables or values.

Given that x = 6 and y = 3, the table below explains the logical operators:


JavaScript Bitwise Operators

Bit operators work on 32 bits numbers. Any numeric operand in the operation is converted into a 32 bit number. The result is converted back to a JavaScript number.

OperatorDescriptionExampleSame asResultDecimal
&ANDx = 5 & 10101 & 00010001 1
|ORx = 5 | 10101 | 00010101 5
~NOTx = ~ 5 ~01011010 10
^XORx = 5 ^ 10101 ^ 00010100 4
<<Left shiftx = 5 << 10101 << 11010 10
>>Right shiftx = 5 >> 10101 >> 10010  2

The examples above uses 4 bits unsigned examples. But JavaScript uses 32-bit signed numbers.

Because of this, in JavaScript, ~ 5 will not return 10. It will return -6.

~00000000000000000000000000000101 will return 11111111111111111111111111111010


The typeof Operator

The typeof operator returns the type of a variable, object, function or expression:

Example

typeof "John"                 // Returns string
typeof 3.14                   // Returns number
typeof NaN                    // Returns number
typeof false                  // Returns boolean
typeof [1, 2, 3, 4]           // Returns object
typeof {name:'John', age:34}  // Returns object
typeof new Date()             // Returns object
typeof function () {}         // Returns function
typeof myCar                  // Returns undefined (if myCar is not declared)
typeof null                   // Returns object

Try it Yourself »

Please observe:

  • The data type of NaN is number
  • The data type of an array is object
  • The data type of a date is object
  • The data type of null is object
  • The data type of an undefined variable is undefined

You cannot use typeof to define if a JavaScript object is an array (or a date).


The delete Operator

The delete operator deletes a property from an object:

Example

var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
delete person.age;   // or delete person["age"];

Try it Yourself »

The delete operator deletes both the value of the property and the property itself.

After deletion, the property cannot be used before it is added back again.

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

Note: The delete operator should not be used on predefined JavaScript object properties. It can crash your application.


The in Operator

The in operator returns true if the specified property is in the specified object, otherwise false:

Example

// Arrays
var cars = ["Saab", "Volvo", "BMW"];
"Saab" in cars          // Returns false (specify the index number instead of value)
0 in cars               // Returns true
1 in cars               // Returns true
4 in cars               // Returns false (does not exist)
"length" in cars        // Returns true  (length is an Array property)

// Objects
var person = {firstName:"John", lastName:"Doe", age:50};
"firstName" in person   // Returns true
"age" in person         // Returns true

// Predefined objects
"PI" in Math            // Returns true
"NaN" in Number         // Returns true
"length" in String      // Returns true

Try it Yourself »

The instanceof Operator

The instanceof operator returns true if the specified object is an instance of the specified object:

Example

var cars = ["Saab", "Volvo", "BMW"];

cars instanceof Array;          // Returns true
cars instanceof Object;         // Returns true
cars instanceof String;         // Returns false
cars instanceof Number;         // Returns false

Try it Yourself »

The void Operator

The void operator evaluates an expression and returns undefined. This operator is often used to obtain the undefined primitive value, using "void(0)" (useful when evaluating an expression without using the return value).

Example

<a href="javascript:void(0);">
  Useless link
</a>

<a href="javascript:void(document.body.style.backgroundColor='red');">
  Click me to change the background color of body to red
</a>

Try it Yourself »

❮ PreviousNext ❯

0 thoughts on “Html Assignment 2 Operations

Leave a Reply

Your email address will not be published. Required fields are marked *