PHP Basics

Variables

Maximiliano Firtman

Maximiliano Firtman

Independent Consultant
PHP Basics

Check out a free preview of the full PHP Basics course

The "Variables" Lesson is part of the full, PHP Basics course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano demonstrates how to create variables in PHP. Variables are prefixed with a dollar sign and are dynamically typed, meaning you can change the type assigned to the variable at any time. The concept of "variable variables" is introduced. They allow the name of a variable to be stored in a variable and a value assigned with a double dollar sign.

Preview
Close

Transcript from the "Variables" Lesson

[00:00:00]
>> Maximiliano Firtman: So, you've seen before variables, so I didn't spoil a few variables, and that's one of the most recognizable moments of php. Variables start with a dollar sign. So we know something is a variable because It has a dollar sign prefix. So there is no bar key, no let key, okay?

[00:00:27]
You just say, my name is Max, that's all. And by the way, semicolons are mandatory. Sorry about that. For some of you, depends on where you're coming from. My brain is already connected with that. So it's not a problem for me, but I know that if you're coming from other languages, Swift, Kotlin, or things like that.

[00:00:51]
But yeah, you have to add semicolons. The only exception to that semicolons is when you're closing the tile. In case you have multiple tags, if you're closing the tag, the last line doesn't need to have a semicolon because the closing tag is also ending the sentence, okay? But it's the only exception for the rule.

[00:01:16]
That also means that you will most of the time don't use the rule, because you, your brain needs to see everywhere. So, yeah. So yeah semicolons, variables, you can add us. You can create variables in the fly. I don't need to declare the variable beforehand. So I don't need to say, this is a variable I will have later.

[00:01:37]
No, you just use the variable. Okay, and the variables can be of different types, of course. I know I can have a year, 2025. That's a number, the internal number, the internal string. We'll get into strings in a second. We have of course, Boolean, like is finished, and I can say true, I can say false.

[00:02:00]
No one asks Yet but for example of course i can do this what about if I do this? Will it work so if I run this so if I say php test php warning undefined variable name with capital N. So php for variable names is case sensitive. So it's not the same if it's capital N or lower case n, which is a rule that probably you are expecting depends on where you're coming.

[00:02:33]
But what you are not expecting is this. You can use true in capital case in lower case, in title case. There are some constants such as true, false, and null that are case insensitive. So you can do some weird things like this and it works. So Variable names are case sensitive, but you will find some pieces of the language where it's case insensitive.

[00:03:09]
So again if something is null, this is null you can use null capital letters. That's will be good for your eyes if you're coming from C++, for example, or you can use null lowercase if you're coming from Javascript, Java, it's the same value. I'm not talking about different values, right?

[00:03:34]
It's just case insensitive. So it means it's just the same value, which also means that when you are opening a php code from another developer, it might not be using the same rules, the same design guidelines as you're used to, okay? The good news that we have this year, or from this year, is that if you need to continue working on a project that you didn't create, you can use AI to convert that code into your own design guidelines.

[00:04:04]
You can ask ChatGPT or Gemini, hey, I have this php code that is using capital letters for true, false, and null. Can you change all of that to lowercase? And it will do that, okay? So it's not a big deal these days. But yeah, you have different ways to do the same thing.

[00:04:24]
By the way, you are realizing that I'm not defining data types. I'm just creating a variable, setting a value. Can I change the data types? By the way, comments, that's one comment, let's go one by one. Can I do this? It is a string initially, and then I'm changing it to number.

[00:04:46]
Can I do that? Well, first I didn't save the file, so let me save the file. Run this again. I mean, I log it in error, so it works. If I do another echo, Cosine without parentheses at one point. So I know how to be fat first. You don't like it, but it's not so bad.

[00:05:07]
So I get a new max and the four and you can see I don't have any new space, a new line. Do we have an echo line or something like that? No, not really. Something that we can do is we can echo a new line like that, okay?

[00:05:26]
Backward end is the new line character, because we are actually sending a string to the output, okay? So you can see that in terms of data types. This is dynamic typing. So it can change the type of a variable. It's not going to complain, similar to JavaScript, actually, or Python, similar to a lot of scripting languages.

[00:05:51]
It has a dynamic nature. Anyway, give me some minutes, because I will show you that there are ways to narrow the type on some specific situations, but not for normal variables, okay? Also, you can see that I'm just writing code, global code. There is no class here, no function yet, but I don't need to create a class or a function or a main function.

[00:06:16]
No, it's just going to execute my PHP code line by line from the. The top to the bottom. So I think that the first paradigm that you need to get is that the goal of a PHP script is to send outputs to the console. Right now is the console.

[00:06:35]
Remember, we still don't have any web server here. Where is the web server? Did we set up any web server? No, we're just writing a script. Script, okay? Ae will get into the web server part, but so far, it just a script because we are just learning the basic syntax of php.

[00:06:50]
It's just a text, okay, that's all. So that's why you can use this for a scripting, for something simple to use your terminal. I'm not saying this is a normal situation. But at least for playing with basic php, it's good enough. So variables, name, the name should start with the dollar sign.

[00:07:14]
Of course then it's a normal identifier, so don't use spacy, things like that in this identifier. I think at this point you know that. There are weird things, things that we can do. For example, I'm not saying you will do that or we will do that now. There is something known as variable variable.

[00:07:33]
What? That's a name, variable variable. So we can create the variable variable. What? Well, it's something like this. You can create A variable with a name for example it can be course. And then we can say variable variable equals to php Fundamentals.
>> Maximiliano Firtman: I know this is gonna be really weird for you.

[00:08:01]
So what is that? Okay, what's going on? Variable, variable. Well also, I call it variable, which is adding another variable thing. In this situation it can be called anything. So it can be,, let's say B name, variable name. So just forget about the variable. It can be any name.

[00:08:18]
So now I can echo course. Now you will get what's. What's going on? So what's going on here?I'm creating a course variable, but the name of the variable is inside the string. Okay, so the name of the variable, the variable is actually the I have two variables here.

[00:08:42]
Name, and course. But the definition of the course variable, the name of the variable is coming from another variable. That's called a variable variable. I mean, it's weird. Yeah, it's weird. But it also makes the language really flexible, because you can be downloading variable names from a database.

[00:09:03]
Which also adds a lot of security risk, probably if you don't know what you're doing. But you can create variables based on, it can be an array or something like that. That's called variable variable. Do you use it on a daily basis? No. Not really. Okay, not really.

[00:09:22]
But some Important things that I wanna show you about the language and variables.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now