>> DOUG LLOYD: So we've done a lot of work in C,
and C is a really cool language because it gives you
the ability to dive really low level into your programs.
We get to do things as really minute as manipulating
individual bytes of memory.
Recall that pointers really allow us that flexibility.
>> But do we always need to have that fine-grain level of detail
in our programs?
Probably not, right?
And if we're going to have a trade-off between being
able to do really, really minute things and really, really
big things that we don't have to think about, we don't have to implement
these really big ideas if they're already built in for us,
generally for building big programs or big projects,
we're probably going to err on the side of having more language stuff built in
for us, instead of having the low-level stuff.
And that's where PHP really comes in.
>> Now, one of the reasons that we teach PHP in CS50
is that it's heavily inspired by C. And in fact, in my opinion,
there are really two progenitor languages that
are very common nowadays.
C and LISP.
And they're progenitor languages because every other modern programming
language that has developed since then is inspired
by one or the other syntactically.
PHP is very similar syntactically to C, whereas languages like Scheme,
for example, which you may have heard of,
is heavily inspired by a language called LISP, which is an older language.
>> So the reason we teach PHP in CS50 is that,
by knowing C as fundamentally as you do at this point,
picking up PHP, which gives you the ability to do much higher level things
than C does, isn't that much of a hurdle,
because you already have the basic idea of the syntax.
C's been around for almost 45 years at this point.
PHP's been around for about 20 years.
And in that 25 years in between, programmers
determined that they would much rather have higher level abilities,
and the mistakes and struggles of the 20 years in between
led to PHP and other modern languages.
>> PHP's a great choice of language for software
that allow-- for software that-- where you
need to do things that in C are actually complicated.
So for example, working with strings in C
is very complicated, because as we know, strings in C
are really just arrays of characters.
It's not a built-in data type.
Or perhaps more fundamentally, something we didn't even cover in C,
what if you need to do some computer networking?
All right?
>> C has the ability to do it, but it's so arcane and so difficult to actually do.
Wouldn't it be nice if the language had a built-in, easy way
to implement networking?
And PHP is a language that makes that, or facilitates that, quite a bit more.
As I said, PHP is very heavily inspired by C. The syntax is very similar.
And so it should hopefully make the transition from one to the other
a little bit softer than some other languages might be.
>> To start writing PHP, just open up a file with the .php file extension.
Technically this isn't actually required,
but if you want things like syntax highlighting in IDE, so that type
names, or variable names, functions, you know, the keywords of the language
are highlighted in a specific color, you generally
want to name your files with a particular file extension.
So we've named our file with a .php extension, but then also with PHP,
all the PHP code we write in that file has to be enclosed in these PHP
delimiters that we see here on the screen.
Angle bracket ?php to start.
Then we write all of our PHP code that we want in between.
And then ?
angle bracket to close.
>> If we don't do this, then what's going to happen?
It's not going to crash.
It's not going to really ruin our program.
But it's not going to have the effect that we want.
What's going to happen, really, is that when we try and run this program,
everything not between those delimiters is going to be printed out verbatim.
It's not going to actually execute the code,
it's going to just print it out verbatim.
>> Now why is the case?
So C is what's known as a compiled language.
You're probably familiar with the step of making your programs,
turning the .c files and .h files into a single executable with make,
in particular using Clang as our compiler.
PHP, though, doesn't have this equivalent.
PHP is what's called an interpreted language.
And what does that mean?
>> Well, it means we don't have to convert our source code to zeros and ones
beforehand.
Rather, there's a program, which is also called
PHP, that understands PHP and can sort of make it on the fly.
That's not really exactly accurate, but it's a pretty good analogy
of what's happening.
It's interpreting those zeroes and ones on the fly.
And so if it doesn't know how to process something,
if it doesn't know how to process PHP, you probably
wanted to put that text in there, right?
>> You probably wanted to put the code in there, even if it's not between PHP--
the PHP delimiters.
But-- so it's not going to delete it for you,
it's just going to basically discard it.
So it's going to print it out to the screen.
>> This seems like it's a bad thing, but actually it's
going to be a really good thing, as we'll
see when we talk about PHP web development,
because it means we can intersperse PHP and HTML.
We can use them together to create a more dynamic web page.
But more on that in the video on PHP web development.
>> So what is the syntax of PHP?
That's what this video is all about.
Let's talk about it.
>> So to start out, variables.
PHP variables exist.
There are just two big differences from C.
The first is that there's no type specifier.
We don't have to say int, char, float, all that stuff.
We don't have to do that anymore.
PHP is a modern language.
It can figure out what you're trying to do or make a best
guess as to what you're trying to do.
So that's pretty nice.
>> The other thing is that all variable names have to start with a dollar sign.
That's just something to get used to.
It's a little weird, because it's so that PHP can understand
what's a variable and what's not.
So every variable name starts with a dollar sign.
So in C we might say something like this, int x = 54.
We don't have to do that anymore in PHP.
We can just say $x = 54.
And we could say, for example, in C, if we had pound-included the CS50 .h
header file, we could say string phrase = "This is CS50."
We don't have to do that in PHP, though.
We can just say $phrase = "This is CS50."
And in fact, string is now a built-in data type in PHP,
or rather PHP understands what a string is.
It's separate from an array of characters like it is in C.
All your favorite conditional statements from C
are still available for you to use.
So no big transition there.
We can say-- we can have if statements like this.
if $y 43, or $z = 15.
So that's pretty straightforward.
We can have if and else.
We can have if and else if.
>> And notice something pretty nice here, and this
is sort of one of those advantages of PHP versus C, notice
what function we're not using here?
We're using == to compare a variable, $name, to a string.
We couldn't do that in C, right?
We had to use a function called StrComp or StrEndComp or any
of its related cousins.
>> And so already we see these advantages.
We don't have to do something as silly or perhaps unintuitive
as call a function called StrComp if I just want to test
whether a value equals a string.
I could just use equals equals, like I could do anything else.
So there's an advantage.
>> Sometimes, by the way, you might see else if as one word, elseif.
And that's OK in PHP as well.
So sometimes you might see that.
It's not a typo.
PHP actually understands elseif.
I don't know why they decided to implement that,
but as we've seen many times throughout our videos so far,
we programmers love it if we can do things quickly,
so getting rid of that space is apparently a big advantage.
>> So that's if and elseif.
We also have the ternary operator, recall question mark colon,
for really short form if else or conditional branching.
And apparently, in this, what we're trying to do here
is assign the variable $letter either true or false,
depending on whether $var is an alphabetic character.
So this is pretty similar to isalpha that we're familiar with from C.
This is sort of the equivalent in PHP.
The function is apparently called ctype_alpha,
but that's how we do it in PHP.
So all this is going to be is, if $var is a letter, $letter is true.
If $var is not a letter, $letter is false.
>> We also have switch statements still.
We recall those from C as well.
At the very top there, that's how we do something like get int or get string.
So PHP has that built in.
We don't need the CS50 library anymore.
We can just use the function readline.
What that's going to do is print out the message, "Your state,
please," and then blinking prompt waiting for the user to input
some information.
Now notice what else we can do with switch.
If you've used it before, you may recall that switch
is limited pretty much to integers and characters, but now we can use strings.
And in fact, the switch statement in PHP is quite a bit more flexible
than its cousin from C.
Loops.
Just like conditionals, all of your old favorites are still there.
We have while loops that count from 1 to 100 in this case.
We have do while loops that count from 1 to 100,
and we have for loops that count from 1 to 100.
So no big leap there.
The syntax is pretty much exactly the same,
except now we're using dollar sign variable instead
of declaring integer variables or something like that for our counters.
>> Here's where things get a lot better than C, though.
Arrays.
So recall when we were talking about C, in order
for us to grow and shrink sets of information,
we needed to sort of default to this idea of a linked list,
because C arrays were fixed in size.
We couldn't shrink them.
We couldn't grow them.
We had to reallocate memory and do all this madness
or use linked lists, which take up quite a bit more space.
But in PHP, arrays are not fixed in size anymore.
They can grow and they can shrink.
So again, these 20 years that existed between the first release of C
and the first release PHP, we decided that, you know,
it would be really great if we could do this.
And so we implemented this.
>> So PHP arrays are not fixed in size, and because PHP doesn't really
have programmer front-facing notions of types,
we can mix data types in our arrays, too.
So we don't even have to use all integers or all floating points,
we can have a mix of all different kinds in a single array.
Declaring an array is pretty straightforward.
It's just like any other variable.
$nums = array (1, 2, 3, 4), array being a function
that's built into PHP that will create an array for you.
This creates an array of four values, numbers in this case, called $nums.
And there's more than one way to do it.
And we're going to see this a lot in PHP.
PHP has been developed by many different people and grows and grows and grows.
There's usually not just two or three ways to do something in PHP,
there's usually like 10 or 20.
Here's just another common way to declare an array.
$nums= square bracket 1, 2, 3, 4.
So this is sort of similar to C's angle br-- curly brace notation, rather. $--
or it would be int nums square brackets equals curly brace 1, 2, 3, 4.
In PHP it's $nums = square brackets 1, 2, 3, 4.
But both of these examples here give me an array of four in this case integers.
>> What if I want to tack something on now?
Well I can just say $nums 4, which again, we're still counting from 0 here
in PHP, would be the fifth element of the array.
I can just say that.
I'm not going to suffer a seg fault, because my array is just
going to grow to accommodate that.
That's pretty nice, right?
And in fact, I don't even need to specify where I want to put it.
I can just say this and just tack it right on to the end,
or I could even just say $nums 20 or 1,000.
It doesn't really matter.
It's still just going to tack it right on to the end.
>> So I can grow, and as-- we're not going to cover it in here,
but I can splice or strip elements out of the array as well,
and the array will shrink to accommodate that now missing or empty space.
There's another way to tack something onto an array,
which is a function called array_push.
So again, just this idea of being able to do things many different ways.
So we've seen three different ways now to tack another element onto an array.
So this adds another element to the end of the $nums array.
And we can mix up our data types.
So I could have an array of not 1, 2, 3, 4, but 1, true, 3, 4,
where true is a Boolean, and then if I want to tack on another element
to that array, perhaps a string, the string "five," I could do that.
And now my array would be 1, true, 3, 4, five.
The word five, not the integer 5.
So a lot of flexibility there.
>> The flexibility gets even better, though,
because PHP has support for something called an associative array.
And we sort of vaguely talked about associative arrays in C
in the context of hash tables, because what associative arrays are really all
about are making key value pair mappings.
And in this case, the keys-- if we're familiar with arrays from C,
the keys are index numbers.
0, 1, 2, 3.
And the values are what we find that array 0, array 1, array 2, and so on.
So the keys are indexes, and the values are
what is in that array location, specified by that index.
>> But in PHP, we don't have to do this notion of array 0, array 1, array 2
anymore.
We can now use actual words to map keys to values.
And so I could say something like this.
I could create an array using the square bracket syntax as follows.
$pizzas = square bracket "cheese" and then
this sort of double arrow notation, 8.99, "pepperoni," arrow 10.99-- 9.99,
and so on.
And so what's going on here?
What am I actually doing?
I'm creating key value pair mappings.
So instead of saying, for example, pizzas 0, pieces 1, pizzas 2,
I can now say pizzas cheese, pizzas pepperoni,
and refer to the values associated with them.
>> So here are our keys in green.
Cheese, pepperoni, vegetable, buffalo chicken.
Here is the arrow that makes this key value pair mapping.
And then here are the values at that array location.
So it's like saying array 0 equals 8.99.
The key is 0.
The value is 8.99.
I can now say array cheese, or in this case pizzas cheese, cheese is the key,
and what I find at pizzas cheese is 8.99.
That's the value that I find there.
>> So I can say things like. $pizza cheese = 7.99.
Say I'm having a sale.
I want dis-- I want to drop the price of the cheese pizza.
Or I can use the vegetable pizza as part of a condition,
or I can add a new element to my array, just like I could do previously.
I can add a new element to this associative array with the key "bacon"
and the value 13.49.
>> But this sort of introduces a problem, if you think about it for a second.
How would we iterate through this array?
Right?
In C, we would just have a for loop, typically, that
would run from 0 to the size of the array minus 1.
The array has n elements in at, the valid indexes are 0 to n minus 1.
So we could use a for loop to step through every single element.
>> But that's not really the case anymore, right?
Now where we have key value pair mappings where the keys are words,
how do we iterate over all of the words?
Well, fortunately, PHP has a way to deal with this too,
and so we'll jump back to loops for a second
to introduce a fourth kind of loop that exists in PHP called a foreach loop.
And what a foreach loop does is it's basically the same idea.
You can use it for any kind of array.
But it's basically the same idea as a for loop,
except instead of using index numbers, you just
have this weird syntax where you call every single element
a name for the purposes of this loop.
>> So in this case, foreach($array as $key).
Basically, as that comment notes, inside of that foreach loop,
it's going to go over every single element of $array, which is typically
going to be an associative array, but can really be any kind of array
that you want in PHP.
And every time that in a for loop you might
have said $array square brackets $i, you could just say $key.
So that $key becomes an alias for every index of your PHP associative array,
and so you can use it like that.
>> So for example, we've now got our pizzas array.
I've kind of tucked it into the corner there
so we can use it to do a quick example.
If I say foreach($pizzas as $pizza), well, what's happening?
Well, I'm going to iterate through every single element of the array $pizzas,
and in so doing, I'm going to call every element, when I'm inside
of the body of that for loop, $pizza.
>> So that's sort of a stand-in, recall, that $pizza
is a stand-in for saying $pizzas square brackets $i
if we were using a for loop, where we could go from $i = 0 to, in this case,
$i = 3.
If we didn't have key value pairs here, this would be element 0, 1, 2, 3,
and we would use a for loop to go $pizzas 0, $pizzas 1, $pizzas 2,
$pizzas 3.
So now just $pizza is substituting for that individual key.
>> So what is this going to print out?
I'm printing out $pizza.
What am I going to find at-- if I print out $pizzas, $i?
Right?
If I'm going to print out the ith element of pizzas,
what am I going to print?
I'm going to print out the values at that location, right?
Like if we were doing this in the context of C,
we don't usually use our iterator variable, int i = 0, i is less than 3,
i++, to print out 0, 1, 2, 3.
We're printing out array 0, array 1, array 2, array 3.
And so what this prints out is this.
It's the list of prices.
8.99, 9.99, 10.99, 11.99.
>> Now a quick note here.
A foreach loop does not necessarily print out things in order.
It's not guaranteed.
It usually does.
It's usually based on the order in which elements are added to the array,
so just bear that in mind.
It might not be in order.
But a foreach loop will iterate across every single element
of the array in question.
In this case, again, that array is $pizzas.
>> I can change the syntax, though, if I want both the key and the value.
Instead of saying $pizzas as $pizza, I can say this.
And if you look at what I've highlighted in green here,
it looks like a key value pair mapping.
And so if you-- even if you are not entirely sure what it's going to do,
you can probably guess that $topping is going
to be the key in this case and $price is going to be the value.
So I'm substituting now every element of $pizzas as a key value pair,
and now I can refer to the key and the value, which might in handy,
for example, as follows.
>> "A whole"-- this is a lot of printing going on here-- "A whole"
topping "pizza costs $" price, and then I print out a period and a backslash n.
So now, notice again I have access to a key, $topping, and a value, $price.
So can you guess what this is going to print out?
There's a lot of print statements, but there's only one backslash n,
so it's going to print something on an entire-- on a single line of code.
>> If I can refer to the key and the value, then now,
instead of just being able to print out the prices,
I can print out something like this.
"A whole cheese pizza costs $8.99."
And now I'm using all of the keys-- cheese, pepperoni, vegetable,
buffalo chicken-- and the values.
8.99, 9.99, 10.99, 11.99 So that's just a different way
to do a foreach loop that instead of just giving you access to the values,
it just gives you-- it gives you access to the keys and the values.
>> So printing out information.
I've already done it a couple of different ways, you might have noticed.
The two functions we've primarily seen are print and echo.
And for pretty much all intents and purposes, they're exactly the same.
They're-- there's a very subtle difference that's not even worth
getting into, but basically everywhere you can use print you can probably use
echo as well.
>> And that's not the only two.
PHP has a lot of different ways to print things out,
and it also has ways to integrate variables into the middle of string.
So recall from C, do you remember what function
we can use to substitute variables into things we want to print out?
You probably use this function quite a lot.
printf, right?
So this is what we had before inside of the context of our foreach loop.
We had these five separate print statements,
because that was the only way I really knew at the time
how to print out messages.
I didn't know how to integrate the variable $topping into my PHP code.
Well, if I just taken a wild guess, printf, it actually would have worked.
printf is a function that I can use in PHP, just like I can use it in C.
>> And so something like this, printf, again, we're familiar with that.
The first %s is replaced with the value of $topping.
The second %s is replaced with the value of $price.
And so I'm interpellating, which is just a fancy way
of saying I'm sticking the variables into that location.
So I'm plugging in $topping where the red %s is and $price where the blue %s
is, and then I would get the message, "A whole cheese pizza costs $8.99."
>> Not the only way I can do it, though.
Maybe I would want to use this method.
This is actually what's most commonly called variable interpellation.
I can use an echo.
I could use a print too, as we'll see.
But what's happening here?
>> First of all, I have to escape the dollar sign.
Because remember, when we were actually printing out the prices of the pizzas,
I was actually formatting them as monetary figures with a dollar sign.
But we're using dollar signs also to represent variable names in PHP,
and in particular when I'm using this method of the curly
brace variable interpellation method, I need
to escape my dollar sign so it doesn't think I'm talking about a variable.
It's going to actually, literally print a dollar sign.
>> So sort of analogize it to what you see at the end there.
It doesn't actually print backslash n, right?
It prints out a new line character.
This is-- it's not going to print backslash dollar sign,
it's going to print out just a dollar sign character.
Same idea.
Escape sequences, what these things are called.
>> But notice that I am not doing any sort of %s substitutions,
I'm just literally plugging in these variables.
And so in this-- what would happen here is that the value of $topping-- again,
just keeping with what we've been talking about so far-- cheese would get
plugged in there.
And $price would be whatever value is at pizzas, square brackets, cheese,
which was 8.99.
And so this would also print out "A whole cheese pizza costs $8.99."
And like I said, I could use print here instead of echo,
and the functionality be pretty much exactly the same.
It would print out the same thing.
>> There's another way to do it, and this is another advantage
of PHP working with strings.
We can do string concatenation.
We could do this in C, too, using a function called strcat,
but again, we had to call separate functions.
It was this whole mess to do.
We had to pound-include string.h.
It was a production, right?
But now I can just use this dot operator to concatenate strings together.
>> So I'm concatenating "A whole" and then whatever the value of $topping is,
and then another string, " pizza costs $"
and then concatenating whatever the value of $price is,
and then at the very end I'm tacking on period backslash n.
And so this would also print out "A whole"-- again,
if we're talking about the first element of that pizzas array--
"A whole cheese pizza costs $8.99."
Period, backslash n, again, with the $topping and $price substituting
for what we had specified in our foreach loop as the key value pair mapping.
>> PHP can handle functions.
Functions were sort of integral to C, as we saw.
Like variables, we don't need to specify the return type of the function,
because it doesn't really matter.
And we don't specify the data types of any parameters,
because they don't really matter, like we've seen in PHP.
Every function is introduced with the function keyword.
That's how we indicate to PHP that what we're talking about is a function.
>> And we don't have to deal with main at all,
because the interpreter, the PHP interpreter, works from top to bottom,
regardless.
If it sees you can make a function call, it'll
go find the function call, even if it comes later.
But it's going to read from top to bottom, so we don't need to specify,
here's where you start.
You start on line 1 of your PHP and work down from there.
>> So here is how we would create a function called hard_square.
It apparently takes one parameter, which I'm calling $x.
This function is complicated just to illustrate various things.
We still have return values.
I'm using a for loop here.
But it's basically just, what this amounts to is just $x times $x.
What I'm actually doing is just adding x to 0 x times or $x to zero $x times.
But it's effectively exactly the same as multiplying $x times $x.
I can still return a value, in this case $result,
and I've made a function in PHP.
>> Here's how you might use it in context.
So maybe I'm inside of some PHP file.
Notice in blue there that I've used my PHP delimiters,
angle bracket question mark php.
In between those are all of the PHP that I want to write.
So I'm apparently going to get-- I'm going to prompt the user
to give me a number, store that variable, store in that variable $x,
whatever they gave me.
Then I'm going to echo hard_square of that value,
and apparently going to tack on a new line
as well, and then later on I'll define the function hard_square so
that when I make the call to hard_square,
it knows what I'm talking about.
>> Now, I could also do something like this.
This is slightly different.
It's almost exactly the same as what we saw before,
except instead of saying just $x there as the parameter to hard_square,
I'm saying $x = 10.
So this is an example of defensive programming,
guarding your programs against malicious users.
>> This is one way to do some error checking that we didn't really
have as an option in C. We could never specify the default value of something.
We always had to check whether the, for example,
if we made a call to GetString, it was most proper if immediately after we
checked that, we checked whether the string
that the user gave us is not equal to null,
because we don't want to start working with a null string.
>> Here, this is a way to guard against that.
If the user doesn't provide us something somehow, what are we going to do?
Well, we'll just say whatever they didn't provide us,
we're just going to plug in 10 instead.
So if they didn't give us a value, just use 10 by default. And so here,
notice that I'm making a call to hard_square,
but there's no prompt to the user, right?
I'm just making an empty call.
>> But my function hard_square is expecting a parameter.
What is this going to print out?
It's going to print out 100, right?
Because the user didn't give me anything.
And so I'm just going to assume that 10-- 10 is the default value.
And so this would print out 100 on its own line.
>> PHP files do not have to be just a single file.
You can combine multiple files together, just like you can in C. The way
we did that in C was typically to do a #include to get header files pulled in.
We don't do that in PHP.
We do something called require_once.
And then there's this whole thing, what's this __dir__?
That's just a special variable, or special constant,
really, that specifies what your current directory is.
And so it's going to look in your current directory
for a file called cs50.php in this example here,
and it's going to stick that file at the top of your PHP program,
assuming that you put the require once line at the top of your PHP file.
>> So PHP is primarily used, but not exclusively used,
as a language for web-based programming.
That's really how it came to be.
But it is a full language, as we've seen.
We've seen pretty much all the things that it can do that are similar to C,
and it can do a heck of a lot more than that.
>> But because it's a full language and we can do command line programming in it.
We can run command line programs.
All that's required to run a command line program that's written in PHP
is that you have a PHP interpreter.
So it's sort of analogous to having a compiler on your system
if you want to compile your C code to turn it into executable files.
You need to have a PHP interpreter that exists on your system
so that you can interpret PHP files.
>> Assuming you do, and usually this interpreter is called PHP,
and it's usually bundled with most downloads or installations of PHP
that you can get online, and certainly the name of the PHP interpreter
we have in CS50, IDE.
All you do is type php file.
And what your program's going to do is it's
going to run through the interpreter, it's
going to ignore everything that's not in between question mark--
or, angle bracket question mark php, the PHP delimiters, and print it out,
and it will interpret and execute the code inside of your PHP delimiters.
>> So let's pop over to CS50 IDE and have a look at a couple of PHP files,
running a couple of PHP files, in command line interface of CS50 IDE.
So here we are in CS50 IDE, and I've taken the liberty
of opening a file called hello1.php.
And apparently, the contents of this file are just the PHP delimiters there,
and in between, echo("hello, world").
This is a pretty simple PHP program.
I'm just going to scroll down to my terminal window here,
and I'm going to type php hello1.php, hit enter.
Hello, world.
That's probably what we were expecting it to do, right?
>> Let's go up and take another look at a program.
hello2.php.
Pretty much the same thing, not a lot going on here.
This time, though, I'm going to prompt the user to give me their names.
I'm using that readline function again.
$name = readline.
That's the prompt, "What is your name?"
>> Apparently I'm printing it on its own line.
And then, so the line below that will be the prompt
where the user can enter their name.
And then I'm using a little bit of variable interpellation here on line 3
to print out "Hello" and whatever the user types.
So this is analogous to saying, Hello, comma, %s if we were using printf in C.
>> So let's go and interpret this program.
So again, I'll scroll down to my terminal window.
php hello2.php.
What is your name?
Doug.
Hello, Doug.
I also have another file called hello3.php.
I'm going to clear my screen with Control L,
and I'm going to execute that.
What is your name?
Doug.
Hello, Doug.
So the behavior is identical to hello2.php, but why is it hello3.php?
>> Well, here's the difference.
In this case, notice that on line 1 here,
I have something that's not in between the PHP delimiters.
I'm just printing out-- or I just typed, "What is your name?"
When the PHP interpreter sees this, it has no idea how to interpret it as PHP,
and so instead of failing, it's just going to spit it out.
>> So notice on line 3 now, my call to readline, there's no prompt anymore.
I'm just actually going to-- when the PHP interpreter sees this,
it's going to print out "What is your name?"
Then it sees, oh, OK, here's-- everything else is going to be
interpreted as PHP, so that's why this works.
I don't have to necessarily prompt the user to-- inside of readline,
I can just have it outside of the PHP delimiters
and allow the interpreter to just print it out for me.
>> So you don't actually only have to have one set of PHP delimiters
in your program.
You can actually have several of them, opening and closing them as needed.
So let's take a look at a couple of programs
in CS50 IDE where we illustrate this idea of having
multiple sets of delimited PHP.
>> OK, so I've opened a file here called add1.php.
And notice what's happening here.
Just as before, I have a single PHP set of delimiters.
I'm going to print out the message, "Please give me a number."
Then I'm going to read a line and store it in the variable $num1.
Then I'm going to print out again.
Give me a second number.
Read a line from the user, store whatever they typed in in $num2.
Add them together and store that result in a variable called $sum,
and then print out, "The sum of these two numbers
is," and then interpellate there the variable $sum.
So let's just run this through the interpreter
to confirm that this is what we expect.
php add1.php.
Please give me a number, 3.
Please give me a second number, 4.
The sum of these two numbers is 7.
That's 3 plus 4.
OK?
So nothing terribly fancy there.
>> And now let's open up add2.php.
Here, I've got a couple of PHP delimited sets there, right?
Lines 1, 3-- lines 1 and 3 have no PHP delimiters.
So when the interpreter sees them, it's just
going to spit out exactly what I have typed there.
So that's where I'm doing all my prompting.
On lines 2 and 4, we see the very familiar $?php sort of delimiters,
so those two lines are going to execute as PHP.
And then on line 5, I have this weird thing right here, right?
This angle bracket question mark equal sign.
I'll even zoom in a little bit further.
You can see this is what I'm talking about right there, this $?=.
>> It turns out that it's so common that the reason that we open up a set of PHP
delimiters is to print out a value.
And that's all we're going to do.
But there's even shorthand for that.
$?= is PHP shorthand for saying something like $?php echo the sum
of num1 and num2.
So this is just another shorthand for that.
>> So if I run this program, php add2.php.
I'll zoom down a little bit.
Please give me a number, 4.
Please give me a second number.
And since I don't really care about data types in PHP, I can say 4.8.
The sum of these two numbers is 8.8.
That function behaves pretty much exactly the same as we would expect,
as well.
And I have one more opened up here called dice.php.
Try this again.
I have one more here called dice1.php, which also, see,
has that angle bracket question mark equal sign notation in there,
but notice that in this case I'm calling the function rand, which as you
might expect generates a random number.
"You rolled a," and it's going to calculate some random number, mod 6 +
1.
So that'll give me number in the range of 1 to 6.
>> Remember that mod 6 would give me a number in the range of 0 to 5,
but if I'm simulating dice rolls, which is what I'm doing here,
I don't want these dice to go from 0 to 5, I want dice that go from 1 to 6.
And so this is a way to get me in the range of 1 to 6.
I'm doing this twice.
So apparently I am rolling two dice in this program.
>> So I'll clear my screen, and I'll do php dice1.php.
You rolled a 4 and a 2.
And if I run the program again, you rolled a 5 and a 5.
So every time I run the program, I'm getting different numbers,
because every time I do so, it's starting over.
It's going to generate a new set of random numbers for me.
>> So if we're used to running programs from C,
we're used to typing ./ the name of a program, right?
That's how we've done all of our programs in C so far.
We can do this in PHP as well by adding something called
a shebang to the top of our PHP file.
I know it's kind of a silly word.
It's short for hash bang.
That's the first two characters there.
Remember we call exclamation point frequently a bang in computer science.
It also might be for sharp bang.
There's a couple ways to interpret it.
But it's basically a special sort of command that the PHP interpreter
understands as, oh, I want you to execute this program,
which is apparently /user/bin/php, which is actually where the PHP interpreter
specifically lives on our system.
So it's-- what happens here is the interpreter understands, oh,
I'm apparently supposed to use in this program to run this file.
And so it allows you to skip over the step
of having to say php something.php.
There's one other catch here, which is that if we
want our programs to work as expected, we
need to do something called a file permission change.
And we'll go-- and we talk a little bit more about file permission changes
in our video on MVC, but suffice it to say that this is what you need to do
in order to make your .php files executable.
So let's take a look at this as our final example over in CS50 IDE.
>> So here in IDE I have two files in this PHP directory that appear not to be
called .php.
I have a function called add-- I have a file called add3
and a file called dice2.
So let's take a quick look and open up add3.
And as you can see, at the beginning of my file I have this shebang, right?
This hash mark exclamation point.
Now, you'll also maybe notice that for some reason,
I don't have any syntax highlighting anymore,
and this is what I alluded to earlier, which was that if I don't name my file
.php, I don't have the benefit of syntax highlighting anymore.
This file is just called add3.
So that I can run it later on with ./ add3 and not ./ add3.php.
>> So the reason-- it's still fine, it's still valid PHP,
but it's not syntax highlighted, because this file is not called something.php.
That's the only real difference here, plus the shebang.
So let's see what happens when I try and run this program.
./ add3, just like I would with C. Bash.
./ add3 permission denied.
This is what you're going to see if you forget
to use the chmod command to change the permissions of the file.
>> As it turns out, regular PHP files cannot just be executed.
They can be interpreted, but we're doing something a little different here.
We're executing it.
And so I need to add the permission of execution, chmod a+x to add3.
Then I can say ./ add3.
Please give me a number.
5, 6.
The sum of these two numbers is 11.
>> Similarly, I have already chmoded dice2, so I can just type ./ dice2,
you rolled a 1 and a 1, you rolled a 5 and a 4, and so on.
>> So that's pretty much the idea of a PHP syntax, right?
There's a lot to get through, I know.
But hopefully you've seen now that PHP is not really that different from C
and really gives us the ability to take things up a notch or two.
We don't really have to worry too much about-- we don't really
have to worry too much about the low-level details we
had to worry about with C, right?
We can focus on the higher level stuff that PHP
allows us to do and to take for granted that it will work for us.
So it gives us the ability now, transitioning from C to PHP,
to make programs that are a lot more complex and perhaps a lot more robust.
>> So I hope you have fun working with PHP, and I'm Doug Lloyd.
This is CS50.