Operators are used to perform various operations on variables and values in this lesson we'll do an
overview of several operators.
It's important to note that p p divides operators into the following groups arithmetic operators assignment
operators comparison operators increment in decrement operators logical operators sting string operators
and array operators because of the amount of operators that can be found across these groups will go
over a few examples in each group.
The file used in this lesson is located in your Downloads folder.
You can use it for future reference and also go through each operator.
If you require further information
we'll start with arithmetic.
Operators
the first session in our HP file provides an example of the different arithmetic operators available
including addition subtraction multiplication division in modulus we can see the output of each echo
statement in our web browser.
So for example we've set variable X to equal 50 in variable Y to equal 20.
The first example here the first echo statement uses the addition operator to add the value of variable
X with Y and the output is 70.
The second example subtracts x minus y in our output is 30 so it's fairly straightforward
going down the file.
The next operator we have here is the assignment operator
an assignment operator is used to set the value of the Left operant to the expression on the right
in this equation.
We have a variable X and we use a single equal sine to assign the value twenty to X so if we were to
create an echo statement and output the value of X Well it would be 20.
So we've set the right expression we've set that value to X and that is known as an assignment operator
it assigns the value over comparison operators are used to compare two values numbers or strings in
the first example we've set the value of x to three hundred and y to three hundred with quotes yes or
no quotes and quotes
we've used the VAR dum function to compare X to Y using a double equals sine the double equal sine is
going to check if the value on the left is equal to the value on the right and because three hundred
is equal to three hundred our output is true the next operator is three equal signs so the variable
x and y are set to the same values again three hundred and three hundred with quotes for Y
three equals sine signs will check to see if X is identical to Y and we know it's not identical because
Y has quotes surrounding it so the output is false in our web browser because they are not identical
you can go through the remainder of the comparison operators to see the result they're fairly straightforward
in the comments explain what each one does
increment operators are used to increment or decrement the value of a variable it can take a look at
the second example we can see that the initial value of x is set to 50 using the plus plus X in this
case.
So if our variable was named anything else we replace x with the variable name we can increment the
value of this variable by one and you can see on the right hand side 50 has been incremented by 1 4
result of fifty one
in the next example we can see that we've decrement it the value of fifty so X is again equal to fifty
and using the minus minus variable operator we have decremental id buy one from 50 to 49
logical operators are used to combine conditional statements so in the first example you're we have
two variables x and y axis set to two hundred and Y is set to three hundred.
We then use an end operator to check if X is equal to two hundred and y is equal to three hundred.
We'll be working a lot more with if statements later on in this course.
But in a nutshell we've instructed the script to output the word true.
If both these conditions are met and in this case the conditions are met.
So in our web browser we get an output of true
in the second example
all of the everything is more or less the same.
The difference is we use the or operator.
So if X is equal to two hundred or Y is equal to ninety will output the word true since one of these
conditions are met X is equal to two hundred we get an output of true
and that's how the logical operators work.
There's a few others you can go through as well.
Next we have string operators and these are specifically to be used with strings.
The first example here allows us to attach one string variable to another in this case our web browser
will output.
Hello and world on the same line.
It's attached the value of X with Y using the period there.
And so you can see the output here.
Hello world.
On the same line now we have array operators as well array operators allow us to compare and manipulate
Aris
in the first example.
We've created a union of two arrays so we have a rate x and a re y rate x has its own set of values
blue and red an array Y has its values pink and brown and using the plus sign we've we've recruited
a union of the x ray in the y Ray so we can see in the output that all the colors of both arrays are
listed
and you can go through the other array operators as well some of these operators will be using in different
sections of this course.
So again there's a lot here.
It's a fairly long script and it's more for reference purposes and you can definitely go through it
and see what each one does all the outputs will be in your web browser when you preview the file.