>> SPEAKER: Let's write a program in PHP.
Unlike C, which is a compiled language, PHP is an interpreted
language, which means as soon as we write our source code in PHP, we can
simply run it by passing that source code as input into what's called an
interpreter, a program whose purpose in life is to read your PHP source
code, top to bottom, left to right, and do, line-by-line, whatever it is
you've set it to do.
Here, in a file called hello.php, let me begin as follows.
Open bracket, question mark, php, and then down below, let me close that php
tag with question mark, angle bracket.
>> Now you'll find, on some computers, you can actually get away with just
doing open bracket, question mark, without the trailing php.
But you'll find that your code is more portable and runs on more computers if
you do use this more verbose version.
Now, in-between these tags, let's do something simple, like printf, quote
unquote, "hello world", with a backslash n.
>> Let's now save the file and run this program.
But to run this file, we're going to have to pass it through the PHP
interpreter.
So technically, we're running PHP, and passing as input the source code I
just wrote.
To do this, at my command prompt, I'll do php hello.php.
And there we see, hello world.
>> Of course, to run this program, I had to know that the program was written
in PHP, so that I'd know to run its interpreter.
This certainly is an ideal, especially when, in C, after compiling a program,
a user can just run it without having to know or care that it was once
written in C. But we can achieve that same effect with PHP as well.
>> Let's go back to my source code.
At the very top of this file, let's add a shebang, a sharp bang or
exclamation point, followed by slash bin slash php, which is simply the
path to the PHP interpreter or program on this particular computer.
Let's now save the file, but also make the file executable
in addition to readable.
It suffices for a file to be readable for a program like PHP to interpret
it, but to actually run a program and look for that shebang at the top of it
to know what interpreter to use to execute the code, we have to make the
file executable as well.
>> To do this, let's type chmod a plus x, for executable, hello.php.
And now, let's run dot slash hello.php.
And again, we see hello world.
Now we can take this one step further.
We don't have to reveal to the world that this program, hello world, is
written in PHP.
We can remove that file extension now as well by specifying mv hello.php,
shall be renamed simply hello, and I can now do dot slash hello, and I
again see hello world.
>> Let's take this one final step.
Suppose that I don't necessarily know the path to PHP on the computer on
which this program might be run, but I want the computer to find it for me.
It turns out that on a lot of computers, there's a program called
env for environment, E-N-V, that can figure that out for us.
So let's go back to the shebang and change it to be simply env space php.
This new shebang will instruct the computer to check its environment for
the program, PHP, and if it's there, use that to interpret this program.