So let's get started by creating a first PHP file inside which we're going to learn about the expressions
and the basics of variables.
You can just call it expressions.
PHP and then as said previously, every PHP file it needs to have this opening PHP tag.
This is a requirement for this file to be actually considered as PHP code.
So the first thing you should learn is how do you output something on the screen.
This is done by using the echo statement, which is followed by a string that should go on the screen.
So I can say welcome to PHP and then make sure that I add a new line using the special character slash
n.
And remember about the semicolon.
Now how do you run this PHP script?
Well, we can just run it in the command line using PHP expression.
PHP.
And here is our output.
So, as we already know in PHP, anything that will return a value is an expression, and in expression
you can just mix raw values, variables, function calls and operators.
So here you've got different examples.
And this page is complex on purpose as that's actually a resource you can download under this video.
Maybe you'd like to print it or just keep it as a reference.
And then with expressions we will also use operators like arithmetic logical comparison and the string
concatenation.
So let's jump back to the code editor and see more examples of expressions in practice.
So in this example here we've directly have put something on the screen.
But sometimes you either have more complex expressions or you just need to do a little more work before
you are ready to output the results.
And in such cases, variables are very helpful.
Let me begin by defining a variable that will be called name and it will contain a string.
Alice.
Now let's take a closer look at variables.
So variables among other things, which we're going to talk about later, are used to store the results
of running.
Expressions.
And variables are defined by using the dollar character followed by the variable name.
Now there are rules for what's the valid variable name.
So as I said it needs to start with a dollar.
Then the second character needs to be a letter or underscore.
And only the third and subsequent characters can be letters, numbers, or an underscore.
You need to keep in mind that variable names are case sensitive, so lowercase variable is not the same
as capitalized variable.
Then there are some conventions on variable naming.
You should either use camel case or snake case.
So here are some examples of valid and invalid variable names.
And also keep in mind not to use PHP reserved words for the variable names.
So here we have assigned the value of Alice string to a name variable.
So this is an assignment expression.
Yes I said expression, which means that this together returns a value.
And we can check that by running the echo statement.
If I rerun this script now we see both welcome to PHP and the text Alice.
So assignment is also an expression and it will return whatever was assigned to a variable.
Okay.
So let's create a more complex expression.
So as we see on this board we have simple expressions like numbers just variables or arithmetic operations.
But also we can create complex expressions mixing all of those together.
So take a look at the operators.
The one that's interesting for us in this case is the concatenation operator.
That's just a dot.
So it would let us put a couple of strings together.
Let's see what can we do with this.
So I can output the text saying hello.
And then I use dot and I put the value of the name variable.
And then also I add an exclamation mark and new line character.
So let's rerun this script.
And there we have it.
Hello, Alice.
So as you can see in this example, I can say that that's the first time when we can see that we are
already doing something useful.
So you can easily imagine that if we have assigned a value to this variable called name, we can also
get an input from the user and then set the value of that variable.
And then once someone enters his or hers name, we can welcome him with this let's say personalized
message.
Okay.
So let's continue with expressions.
This time let's do an arithmetic operation.
So let's calculate something.
First let me create a new variable called pizzas.
And we will have three.
Then let's create another one slices per pizza.
That's typically eight.
Now let's do an arithmetic operation so we can multiply, divide, add or subtract or calculate the
modulo.
So let's calculate the total amount of slices.
This would be multiplying pizzas by the slices per pizza.
And let's echo that.
We can say total pizza slices.
And now we concatenate.
So we use the dot operator and we output the total slices.
And again we concatenate that with the new line character.
And now let me run this.
There we have it.
Total pizza slices 24.
That was expected.
Now at this point let me quickly mention the data types.
So I'm going to go on data types in detail a little bit later.
For now let me just explain This line right here.
So we can see that we have total slices variable which is a result of multiplying three times eight.
Yet we concatenate the string and a number and we get the proper output which is a string.
So what happened here is called the type coercion, which is implicitly casting one of the data type
to the other.
So everything makes sense.
So in case of a string concatenation the sensible output is to produce a string.
That's why this number was implicitly casted to a string in PHP.
But as I said we're gonna talk about data types and typecasting later on okay.
So let's see another example.
Let's define a variable and call that is hungry and initialize it with true.
So this would be of data type boolean which basically can hold one of two values either true or false.
So Boolean values are very commonly used.
They are always the result of for example the comparison expressions.
And in this case we will just set it to true.
And what I would like to do is to output different text depending on whether someone is hungry or not.
So let's always echo hungry with a question mark.
And then we're going to use something called ternary operator.
So it's super simple thing.
First you add an expression on the left side, then a question mark and a colon.
And this would return either something on the left side of the colon.
If this expression is true, or the right side after the colon if this expression is false.
Ternary operator is something you will commonly use, so I'd like to return the text yes, or alternatively
the text.
No.
Let's run this.
Or maybe let's also follow up with a new line character.
Now let's run it and we have it.
Hungary.
Yes.
Now let's change this value to false and rerun the script.
And now the Hungary displays the text.
No.
Let's summarize.
So you've learned.
How can you output something on the screen, how to define a variable.
And then couple of operators like concatenation arithmetic operators or an often used ternary operator
Ethz.
Then we've also heard a little bit about different data types like Boolean numbers strings, and the
type coercion.