php-variable

Basic syntax of PHP: Variables and constants. How to use them properly?

The basic syntax of variables in PHP.

We’re going to go through some of the basic syntax of PHP. So, PHP has a whole bunch of keywords, a lot of these are kind of taken from C, probably the weirdest thing when I write PHP that I, when I switch back and forth between PHP and other languages, is the fact that the variable names start with dollar signs, and you’ll find this is annoying for a while, but you get used to it.

The lack of a variable with a dollar sign can be kind of a weird thing, cuz in different places, like a string that looks like this in PHP sometimes can be sort of misinterpreted, but they all have to start with dollar signs, followed by underscores and letters and numbers, just like any other language, but just understand that they’ve got to have dollar signs. And it turns out that I actually like it when I get used to it because we’re increasingly mixing PHP and JavaScript, in JavaScript, you can put dollar signs in the variable names, but we tend to avoid that. So that then I could kind of be looking at a line of code and immediately know with dollar sign variables, that’s the PHP code that I’m working in and the JavaScript code, cuz JavaScript is kinda similar to PHP in case matters on variable names.

Constants in PHP

Like I said, there are certain situations that if you omit the dollar signs, sometimes it freaks out more than others, and so, in this case, it doesn’t freak out, and in this case, it does freak out. And it has to do with the fact that there are these predefined constants that you use this defined statement to define, so y or x in this case with no dollar sign could be a predefined constant. A predefined constant could participate in an expression, and that ends up seeming like zero with no error, and then it, but it’s certainly not, you can’t do an assignment statement to a predefined constant. And so, just understand, and this is kind of a PHP-ism, when you forget the dollar sign, it may or may not blow up on you and so you should mentally have a background thing in your mind like, wait a sec, what am I doing here?

Look for the dollar signs, again, this is another weird thing depending on how your PHP is configured. In early, early versions of PHP this syntax was allowed where this string maps to that in an array and it actually sort of adds the double quotes here. Not all PHPs do it cuz you can compile PHP in different ways, so it’s just, don’t omit the dollar sign, that’s the message here, don’t omit the dollar sign. So there’s a lot of things in PHP that I actually love and wish were in other languages, but not likely, all these other languages have their worlds and they love them and whatever, so string literals can be single quotes or double quotes. The backslash is used as an escape character, that’s pretty typical among C based languages. Strings can span multiple lines, you just start it, enter, enter, enter and then stop it, I love that.

The weirdness is is that double coded strings are quite different than single coded strings. In a language like Python, or JavaScript, they tend to be the same, and we have reasons to use them. And concatenation, these are different, and PHP is the only language that does this, concatenation is the dot operator, not the plus operator. Most other languages, and historically, it had to do with the fact that other languages that do object orientation use the thought in object orientation notation, but PHP existed without OO, object orientation, and so it used the dot operator for concatenation, and I love the dot operator for concatenation, but no other language is ever going to adopt it, and so it’s quite frustrating even though I think it’s a super elegant choice that PHP made.

So, and I like the double quoted strings, but you got to be, it’s like, you got to, you gotta be responsible when you use the quick and dirty dollar sign substitution in double quoted strings, it’s, again, with great power comes great responsibility. So here are some examples of double quotes, and so the fact that we have a double quote, I mean, there’s an echo state, which does the printing, a new line at the end, just like most C based languages, and Python is also like this.

The new lines are rather explicit, and you’ve got to manage the notion that the, we come to the end of the line and we go to the beginning of the next line. You can embed new lines in a multi-line string, so this one starts here and ends there, and there’s a new line there, and a new line there. You can put a new line in the middle, it’s the same, and this is probably the weirdest part that people don’t understand and don’t get so well, and that is in dollars, in double quoted strings, when there’s a dollar sign, it looks for variable with that name and out comes the 12, so, this actually substitutes and puts the 12 in here.

And so, that’s cool, I tend to only use it for debugging, it’s kind of dangerous because you’ve got to be careful not to introduce bad HTML into your output. So, that’s double quoted the strings, and so, single quoted strings are quite different, single quoted strings are really simple. They don’t do any substitution and the backslash doesn’t work quite the same, right? So here’s a single quoted string, you can have embedded newlines that have newlines in them, and so that part works. And you can, one of the reasons I tend to use single quoted strings is so I can use a double quote as a character, that’s kind of nice.

Strings explained in depth

You can put a single quote into a single quoted string by a backslash single quote, but a lot of the other backslash characters don’t expand, and that’s actually kind of frustrating. If I could change one thing, I would want the newline, [SOUND] newline, to expand, but it doesn’t, it actually prints out backslash n, and it doesn’t do variable expansion, and that is a good feature of PHP strings, that it doesn’t do variable expansion because that can get you in some trouble. You can kind of end up in a mistake where you didn’t mean for an expansion to happen, so I’m actually, when I’m writing code, I tend to use single quotes whenever I can, and only use double quotes when I have an intention to use double quotes.

So, single quotes is my preference, and then I use double quotes. And so, if you look at some of my code, you’ll see I will have a single quote blah, blah, blah, blah, blah, concatenate with double quote blah, blah, blah, and so, I mean, I use single quotes and double quotes very consciously in PHP. PHP is one of my favorite languages for comments, and that’s because they just took all of the comment styles and made them work. And so, there is a comment style with C++ where you say slash, slash, and it’s a comment to the end of the line.

There is a block comment that comes really out of C, that is slash star, as much as you want, the star slash, it’s a multi line comment. And then the shell-style comment, which really comes from PEARL, says anything after the pound sign is a comment. And so, whatever language you come from, you can use the commenting syntax of it. I tend to, I definitely use this kind of comment and that kind of comment all over the place. We use this kind of comment for the built in documentation, and that’s really nice. And I tend not to use the pound sign, I just want to think of PHP like C or C++. PHP, yeah, PHP is actually getting like C++ in the later versions.

So I already mentioned that echo is the way we print, but there’s also this thing called print, and it has to do to with the fact that shell scripts say echo, and PEARL said print, and so both of them worked. They have subtle differences, echo is part of the language, it’s a language construct, and print is actually a function, which only takes one parameter, but with parenthesis that are optional.

So I, in all my code, I don’t quite know why I chose to do this, maybe it’s because I don’t like PEARL, but I don’t use print at all. But you’re gonna look at code, and people might wanna use it, that’s just my choice, I tend to use echo for everything. It’s shell script and print is from PEARL, neither of them are from C, if there was something from C, I would probably prefer that one. And so, up next, we’re going to talk about the operators and expressions and how we perform calculations in PHP.