1. 8
    Create & name variables in PHP
    2m 25s

Create & name variables in PHP

Mark Shust
InstructorMark Shust
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 2 years ago

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.

[1:52] Whichever format you use is completely up to you. Some prefer snake case because it's generally easier to read, while others prefer camel case because it more closely aligns with how JavaScript variables are named.

[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.

egghead
egghead
~ 4 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today