Tag Archives: comparison

If statements in PHP

If statements (conditional statements) are used to make decisions in your programs. An if statement will test a condition and then run a section of code if the condition evaluates to true. There are a number of relational operators and logical operators you can use in if statements to test different conditions which are explained in the video below.

Relational operators

Relational operators are used for comparison such as checking if one variable’s value is greater than another. Eg. is x greater than y? (x>y). Here is a list of relational operators:

Operator Description
< less than
> greater than
<= less than or equal to
>= greater than or equal to
== equal to
=== identical to (equal to AND of the same data type)
!= not equal to
!== not identical to (not equal to AND not of the same data type)

Logical operators

Logical operators are used to combine conditions. Eg. is x greater than both y and also less than z? Here is a list of logical operators:

Operator Description
&& AND
|| OR
and AND (same as &&)
or OR (same as ||)
xor Xor (if either one or the other is true, but not both)
! NOT (eg. !$x returns true if $x is not true)

Watch the video and then scroll down to see the sample code.

Sample if statement PHP code:

<?php
 
 $number = 5;
 
 if($number < 10)
 {
 echo $number , " is less than 10";
 }
 
// comparison operators include < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (equal to), != (not equal to), and === (identical to).
?>

Sample PHP code using AND operator used to test that both criteria are true (the AND operator is two && ampersands):

<?php
 
 $number = 5;
 
 if($number > 1 && $number < 10)
 {
 echo $number , " is greater than 1 and less than 10.";
 }
 
 // logical operators include && (and) and || (or)
?>

Sample PHP code using OR operator to test that at least one criteria is true. (the OR operator is two || bars):

<?php
 
 $number = 5;
 
 if($number < 10 || $number > 20)
 {
 echo $number , " is less than 10 or greater than 20.";
 }
 
 // logical operators include && (and) and || (or)
?>

PHP Manual references:

Operators in C#

Operators

Operators are symbols that are used to perform operations on data. The range of different operations you can perform can usually be placed in these four categories:

  • Arithmetic operators
  • Assignment operators
  • Equality operators
  • Logical operators

You will see how these operators can be used for conditional statements in the next tutorial but for now, you can watch the video below to learn about the different types of operators in the C# programming language.

Arithmetic operators

Arithmetic operators are used for performing standard math operations on variables and are usually only used number variables (although they can be used for other things too).

Operator Name / description
+ Addition – this operator is used to add two numbers together. It can also be used to concatenate (join) two strings together.
Subtraction – this operator is used to subtract one number from another.
* Multiplication – this operator is used to multiply two numbers together.
/ Division – this operator is used to divide one number by another.
% Modulus – this operator is used to divide one number by another but instead of returning the result, it returns the remainder of the division. Eg. 5%2 would return a result of 1.

Brackets can also be used for more complex math operations eg. 5 + (10 * (6 / 3) / 2);

The assignment operators follow standard mathematic order of operations. That means that the math works from left to right. Parenthesis are done first, multiplication and division comes second, and then addition and subtraction come third.

Assignment operators

Assignment operators are used to assign a value to a variable. The most frequently used assignment operator is the equals (=) sign. There are other operators as well that are used to combine multiple operations into one. The syntax of a standard variable assignment looks like this:

<variable name> <assignment operator> <value>;

For example: x = 5;

The table below shows the different assignment operators available in C#.

Operator Description
= The equals sign is used to assign the value on the right side of the equals sign to the variable on the left side of the equals sign.
+= , -= , *= and /= These assignment operators are also used to perform arithmetic operations and assign the result to the variable eg. x *= 5 is the same as saying x = x * 5.
++ and – These assignment operators are called increment and decrement operators and are used to increase or decrease the value of a variable by 5. For example, x++ is the same as saying x = x + 1.

Equality operators

Equality operators are used to compare two values. The result of using an equality operator can either be true or false. The only type of variable that can store the result of an equality operator is a Boolean. The table below describes the equality operators used in C#.

Operator Description
== This operator is used to check if two values are equal eg. x == 5 would return true if x had a value of 5.
> and < The ‘greater than’ and ‘less than’ operators are used to check if values are greater than or less than another value. For example, x > 5 (if the value of x was 3 than it would return false).
>= and <= The ‘greater than or equal to’ and ‘less than or equal to’ operators are similar to the ‘greater than’ and ‘less than’ operators. For examples, 5>=5 would return true because 5 is equal to 5, and 6<=10 would return true because 6 is less than 10.
!= The ‘not equal’ operator is used to check if two values are not the same as each other. For example, x != 10 would return true if the value of x was 9 because 9 is not equal to 10. However, y != 5 would return false if the value of y was 5.

Logical operators

Logical operators are used for complex conditions. The table below describes each logical operator.

Operator Description
&& This is known as the AND operator and is used to check if both values are true in a complex condition.
|| The is known as the OR operator and is used to check if at least one of the values is true when two values are compared. It will return true if either one or both values are true.
! This is known as the NOT operator and will return the opposite of a Boolean value. For example !true; would return false and !false; would return true.