
This is the chapter where things are going to get exciting; PHP has 2 default placeholders in which it can store data these are the variable and the constant. The main difference between these 2 is that the value of a variable can be changed during code execution, while the one of a constant will always remain the same. So the main question you should ask yourself during creation of your code (or even better, before you start coding) is; will this value ever change?
Creating a constant value
To create a constant the statement called 'define' is used which takes 2 parameters; the name of the constant and the value of the constant, just take a look at the example below for clarification.
On line 1 we start by telling we are going to use PHP code.
Line 2 is where the magic happens, we fill the constant called MY_CONSTANT_NAME with the value 'The value of my constant'
Line 3 finally shows the content of MY_CONSTANT_NAME by using the echo statement.
Line 4 only has some comment which will be ignored by php, it is only used to let other programmers know what you are doing. After the 2 // could be any text you'd like.
It is best practice to use uppercase names for constants, this makes them easily recognizeable within your code.
Many common uses of constants are website data (web shop name, website owner phone number, website owner name, etc), fixed prices like P&P or transaction costs, but it could ofcourse be just anything that you don't want to change while running your code.
Variables
Variables are pretty similar to constants, with the main difference being that their values can be changed. To name a variable we can use the following piece of code:
| Naming a variable | |
1 2 3 |
<?php $newVariable = 'Hello World'; echo $newVariable; |
In the example above we assign the text 'Hello World' to the variable $newVariable in the first line. In the second line we use the echo command to show on screen what is in our variable.
Now here comes the interesting part, changing a variable:
As we execute the code above we see that we do the same actions twice. On screen this should result in 'Hello WorldGoodbye World' as we echo the content of our variable twice but change it's content before echo-ing it the second time.
This opens up a lot of possibilities which makes PHP so powerfull, in the next chapter we will be talking about operators which you can use to manipulate these variable contents by for example making calculations. But before we go there, there is just one more final piece of understanding needed.
Data types
Variables in PHP can be of different data types, by default PHP tries to guess the best data type based on your input. But for certain tasks understanding of these data types is very impotant. Like making calculations, if you would calculate with the number 15 as a text it would lead to different results as calculating with a numeric number 15.
This article is still in draft mode, the final content will be added in the near future.
Any additions, questions or comments to this article are welcome, post a comment below for the maintainer so he can update the article.