Learn how to create variables in PHP, and the different naming conventions to use when naming variables.
Instructor: [0:00] Every programming language has the concept of variables, and they're used any time you wish to store data and then reference it somewhere later on in your code. [0:09] PHP variables always start with a dollar sign. Let's create a new variable for a fictitious blog we will be creating.
[0:16] We'll do that by opening up another PHP tag at the start of our script, and then we will create a $title variable. Let's set it equal to "My Blog."
[0:30] We can put all of this code on one line, but usually when I create variables, there is additional code that will eventually make its way into this script, so I always use multiple lines when defining variables.
[0:43] Remember that we need to use a semicolon here, because we are not within a short tag and our script isn't completely PHP. Let's change this "Hello world" to the reference of our variable, $title. When we save this file and refresh the page, we will see "My Blog" outputted.
[1:04] Note that the name of this variable, $title is completely arbitrary. It can be whatever we wish, but variables cannot start with a number. We will see that PhpStorm complains that it is invalid. It also can't contain any odd characters or other characters within the variable.
[1:24] A good IDE will throw some sort of flag or error warning so you know this code cannot be compiled before you go ahead and save it.
[1:33] If you would like your variable to contain more than one word, for example, "my title," you can either use snake case, which is all lowercase and words separated with an underscore, or you can also use camel case, with the first word lowercase and all subsequent words uppercase.
[2:05] I personally prefer camel case, but whichever you choose, just pick a format and stick with it so you don't drive your co-workers or other people dealing with your code crazy.
[2:16] For now, I will just set this variable back to $title because it's the most straightforward name at the moment for this variable.