Subtitle
Subtitle
programming language is the key words. And they're reserved words, key words,
these are the words that you can't use for any other thing. Meaning if you say the
word class,
or clone, or do, or else, PHP is going to say I
know what you mean by that. That has a very specific meaning to me and
I'm going to ignore, you might treat that as a variable, etc,
etc, etc, but that is reserved for PHP. And so you don't get to do
what you want with that. It just means that you
have to avoid these. Most of these, like case maybe,
or as, class, end, some of these you might mistakenly
use as a variable name, or. So just avoid these, and
sometimes you'll get a syntax error, so these are just the basic things. Probably
one of the weirdest things
when I go back and forth between PHP and another language, and
I love programming in multiple languages, is the fact that in PHP all
variables start with a dollar sign. And that's weird,
it's kind of a throwback to Perl. Followed by a letter or underscore. So after the
dollar sign it's the same
rules as to most variables, letters, numbers and underscores after starting
with a letter or an underscore. We tend to not use underscores unless
it's sort of something internal so that we don't collide. If I'm building a library
and I have
a variable, I might stick an underscore in the front of it that I don't
want anyone else to see. Not that they can't see it, it's just
that you hope that there's no collisions. And so the weirdest thing about this is
that if you leave the dollar sign off, it may or may not be a syntax error. And so
you've got to discipline yourself,
when I'm coming from JavaScript or another language, I will start typing
it and leave the dollar sign off and then I cause myself bugs. There's a little bit
of weirdness when you
leave the dollar sign off, because there's a syntax where you can have something
like x with no dollar sign and it's a predefined constant
that assumes to be 0. This may give you a non-fatal error or
it may just silently turn that into 0. It's not going to turn into 2, right? It's
not x = 2, that's not 2 there. That's a non-existent predefined
constant that might turn into 0, and give you an non-fatal error. On the other
hand, if you put it on
the left side of this assignment statement it's going to actually
give you a parse error. So all you mean is just
put the dollar signs in. But PHP is a little weird
when you make syntax errors. Sometimes earlier versions
of PHP would do things, like this is an array that
is a key value array. It maps x to value Hello. And you could put x in square
brackets
to kind of look that thing up, but PHP in early versions
allowed that without the quotes. JavaScript kind of has
a similar kind of idea there, that you can do it with or without quotes and so
it would sort of stick quotes in here. These days it'll stick
those quotes in there and it'll give you a non-fatal error. So again,
it's just kind of letting you know that sometimes if you don't use dollar
signs it has weird behaviour. So strings are a little bit different
than in other programming languages. And part of why I focus so much on
differences is that I assume at this point you already know one programming
language,
hopefully it's Python. And so I like you am going back and
forth between programming languages, Python, JavaScript, PHP, etc. And so I'm
trying to highlight
the things that are different. So strings in PHP are awesome,
I actually like them. I wish other languages would do the kind
of things that PHP does, but they don't, which sometimes gets me in trouble when
I go from PHP to another language. So you can use single quotes or
double quotes and they function a little bit differently. Backslash is used as the
escape character. That really is kind of a C inspired idea. Strings can span
multiple lines. They can just go and
they can go round, round round. You can start one and then stop it,
I love that, I absolutely love that. And in double-quoted strings,
variables are expanded and variables have dollar signs in them. This is kind of one
of the weird
things about why there's dollar signs in variables. And a thing that I absolutely
love but unfortunately it's not the same
in any other languages. "." is concatenation which
I think is awesome and "+" is a mathematical operator. Where in most languages,
even though
I love PHP, in most languages plus is the concatenation operator for strings,
so we'll talk about that in a little bit. So here is the use of
a double-quoted string. So double quotes and
single quotes are different. So I think of double quotes
in PHP as smart strings, you can do more things in them. So, for example, a new
line works
in double-quoted PHP string. You can have embedded do lines. You start the string
here and
it comes all the way down to here. And there is a \n here, and
a \n that PHP automatically adds. I think that's supernatural, very natural,
I wish more languages did it. Other languages you have to work a lot
harder to have a multi-line string and so this is quite natural. You can have a new
line in
the middle of a string, and so the newline will cause the text to go
back to the beginning of the next line. And here's kind of the weird thing
that you learn to love after a while, it's both wonderful and dangerous,
so you gotta be careful about it. If inside of a double-quoted string
you see a variable, $expand. It actually puts the 12 in there, so it wipes this out
,and reads the 12 that's
in expand and puts the 12 in there. That's cool, it's also dangerous. So we'll
learn about
when you don't use this. You certainly don't use this when
you're producing SQL statements. But actually if you know what you're doing
and you're careful about it, this is really a way to write really nicely, easy
to read code that makes a lot of sense. So single quotes I think of as
sort of not such smart strings. And so single quotes start and
stop strings [SOUND]. You can also have embedded new lines. So this one starts here
and stops there. And there's newlines, \n. You can put double quotes
in a single-quoted string just as a normal thing. If you want to put a single quote
in,
you have to backslash it to escape it. And this is something I don't like, this is
probably my least favorite
feature of single-quoted strings. And that is newlines, \n doesn't work
inside of a single-quoted string. I'll sometimes do something where
I do a single-quoted string and I'll concatenate it with a double-quoted
sting, just so I can put a newline in. It's like aah,
why do I have to do that, sad face. But just remember that wasn't my idea, but
if you put newlines in a single-quoted string, a \n in a single-quoted string,
it doesn't expand. And the whole trick of dollar signs
don't expand, and that actually is good. I mean, I like that, so you know when
you're using a single-quoted string, dollar signs are not going to expand. You know
when you're going to
use the double-quoted string, our dollar sign variables
are going to expand. Comments in PHP are truly awesome. You can use comment style
that came from
C++ which is // to the end of the line. You can use the comment style
from Perl and shell scripting, which is # to the end of the line. And you can use
the comment style from
the C line, which is start of comment, which is two characters,
that goes all the way to the end comment. And so this is really nice,
we use this for documenting code. So if you write little API documentation
at the beginning of some code that you're writing that's really nice. This is C++,
this is the C language and this is like Perl and
Shell language comments. So they just made it, hey, why not take
all the existing comment styles on the planet and use them all,
which is really quite nice. because again, I go back and forth between
PHP and lots of other languages, and so there we go. The output,
there's more than one way to do it. echo is a language construct,
so you can say like echo $x or can be treated like a function, echo \n. You can
have more than one parameter,
$x, "\n". And it does not add a space here, it
just concatenates these things together. And then there's a print statement, it's
actually a function that happens to not require the parentheses if you
don't have the parentheses. The difference between print,
they do the exact same thing. The difference is, is echo can have more than one and
print can only have one parameter. For whatever reason, I tend to use
echo all the time and not print. I think echo came from the Bash shell and
then print came from Perl, I think. Certainly looks like it
also came from Python. Yeah, could be from Python. Perl and Python use print, I
think. So a PHP philosophy is like hey,
if you want to do it, you can do it. We'll let you kind of pretend
you're in more than one language. So up next, we'll talk about operators and
expressions.