PHP is a "loosely-typed" language, so it automatically assigns data types at the time of creation. In this lesson, we'll also touch base on how to determine which type of variable PHP assigns.
Mark Shust: [0:00] Note that we didn't assign a data type when we created our title variable. This is because PHP is considered a "loosely-typed" language. In most other languages, you would need to define title as a string at the place the variable assignment takes place. [0:15] Let's create a new <p> tag. Within it, we will use a short echo tag. We will display the result of a gettype, which is a built-in PHP function. Let's pass in the title variable as an argument to this function. When we save and refresh the page, we will see that it is the type of string.
[0:39] Let's create another new variable named numPosts. I generally really dislike using abbreviations in my code, but num is very common and simple variable names make your code a bit easier to read. Let's set it equal to 10 and let's output this within a new <h2> tag. In a short echo tag, you will output numPosts and then go ahead and close up the tag.
[1:06] When we save it and refresh, we will of course get 10. Let's again output the type of this variable. I'll just copy and paste this <p> tag and then pass in numPosts. When we refresh, we will see it is of type integer.
[1:23] Note how we didn't strictly define a data type when creating either of these variables. Based on the value of the variable at the time they are created, PHP infers the data type that it should be and automatically assigns it for us. That's super cool.
[1:39] I'm going to go on a little tangent about the gettype function because I didn't really talk about it. Note that gettype is actually a built-in PHP function and not a language construct. I'm going to expand this PhpStorm window just a little bit.
[1:57] Let's Command-click in to this gettype function. On Windows or Linux, it's Control-click. This will take us to the place that this function is defined. We can read the signature of the function to find out exactly what types of variables we can pass in and what is returned.
[2:17] If we scroll up, we will see a doc block with some additional info. This includes the description of the function and a link to additional data on php.net website. If we Command-click or Control-click on Linux or Windows, we will get sent to full documentation of the function on PHP's documentation website.
[2:41] This is a bit easier to read than the dock block and contains all of the additional info related to this function along with a change log of when this function was potentially introduced or changed and examples of how this function is used along with related functions.
[2:58] There are thousands of built-in functions in PHP, so this documentation is extremely helpful. Don't be concerned about memorizing all of these functions though. I've been programming in PHP for over 20 years, and I probably only know a fraction of them.
[3:13] You will slowly become familiar with more functions as you need them and use them, and as you continue to work more with the language. We'll touch base on quite a few more built-in PHP functions later in this course.
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
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!