1 00:00:00,000 --> 00:00:00,360 2 00:00:00,360 --> 00:00:02,530 >> SPEAKER: Let's write a program in PHP. 3 00:00:02,530 --> 00:00:05,920 Unlike C, which is a compiled language, PHP is an interpreted 4 00:00:05,920 --> 00:00:09,340 language, which means as soon as we write our source code in PHP, we can 5 00:00:09,340 --> 00:00:12,960 simply run it by passing that source code as input into what's called an 6 00:00:12,960 --> 00:00:16,340 interpreter, a program whose purpose in life is to read your PHP source 7 00:00:16,340 --> 00:00:20,140 code, top to bottom, left to right, and do, line-by-line, whatever it is 8 00:00:20,140 --> 00:00:21,650 you've set it to do. 9 00:00:21,650 --> 00:00:25,450 Here, in a file called hello.php, let me begin as follows. 10 00:00:25,450 --> 00:00:31,140 Open bracket, question mark, php, and then down below, let me close that php 11 00:00:31,140 --> 00:00:33,700 tag with question mark, angle bracket. 12 00:00:33,700 --> 00:00:36,570 >> Now you'll find, on some computers, you can actually get away with just 13 00:00:36,570 --> 00:00:39,710 doing open bracket, question mark, without the trailing php. 14 00:00:39,710 --> 00:00:42,920 But you'll find that your code is more portable and runs on more computers if 15 00:00:42,920 --> 00:00:45,180 you do use this more verbose version. 16 00:00:45,180 --> 00:00:49,760 Now, in-between these tags, let's do something simple, like printf, quote 17 00:00:49,760 --> 00:00:53,230 unquote, "hello world", with a backslash n. 18 00:00:53,230 --> 00:00:55,610 >> Let's now save the file and run this program. 19 00:00:55,610 --> 00:00:58,490 But to run this file, we're going to have to pass it through the PHP 20 00:00:58,490 --> 00:00:59,180 interpreter. 21 00:00:59,180 --> 00:01:02,980 So technically, we're running PHP, and passing as input the source code I 22 00:01:02,980 --> 00:01:03,840 just wrote. 23 00:01:03,840 --> 00:01:10,150 To do this, at my command prompt, I'll do php hello.php. 24 00:01:10,150 --> 00:01:12,120 And there we see, hello world. 25 00:01:12,120 --> 00:01:15,220 >> Of course, to run this program, I had to know that the program was written 26 00:01:15,220 --> 00:01:18,400 in PHP, so that I'd know to run its interpreter. 27 00:01:18,400 --> 00:01:22,290 This certainly is an ideal, especially when, in C, after compiling a program, 28 00:01:22,290 --> 00:01:25,500 a user can just run it without having to know or care that it was once 29 00:01:25,500 --> 00:01:29,850 written in C. But we can achieve that same effect with PHP as well. 30 00:01:29,850 --> 00:01:31,260 >> Let's go back to my source code. 31 00:01:31,260 --> 00:01:34,970 At the very top of this file, let's add a shebang, a sharp bang or 32 00:01:34,970 --> 00:01:39,610 exclamation point, followed by slash bin slash php, which is simply the 33 00:01:39,610 --> 00:01:44,040 path to the PHP interpreter or program on this particular computer. 34 00:01:44,040 --> 00:01:47,830 Let's now save the file, but also make the file executable 35 00:01:47,830 --> 00:01:49,230 in addition to readable. 36 00:01:49,230 --> 00:01:52,810 It suffices for a file to be readable for a program like PHP to interpret 37 00:01:52,810 --> 00:01:56,820 it, but to actually run a program and look for that shebang at the top of it 38 00:01:56,820 --> 00:02:00,470 to know what interpreter to use to execute the code, we have to make the 39 00:02:00,470 --> 00:02:02,610 file executable as well. 40 00:02:02,610 --> 00:02:09,840 >> To do this, let's type chmod a plus x, for executable, hello.php. 41 00:02:09,840 --> 00:02:13,730 And now, let's run dot slash hello.php. 42 00:02:13,730 --> 00:02:15,400 And again, we see hello world. 43 00:02:15,400 --> 00:02:17,030 Now we can take this one step further. 44 00:02:17,030 --> 00:02:19,850 We don't have to reveal to the world that this program, hello world, is 45 00:02:19,850 --> 00:02:21,140 written in PHP. 46 00:02:21,140 --> 00:02:27,280 We can remove that file extension now as well by specifying mv hello.php, 47 00:02:27,280 --> 00:02:32,750 shall be renamed simply hello, and I can now do dot slash hello, and I 48 00:02:32,750 --> 00:02:34,090 again see hello world. 49 00:02:34,090 --> 00:02:36,080 >> Let's take this one final step. 50 00:02:36,080 --> 00:02:39,810 Suppose that I don't necessarily know the path to PHP on the computer on 51 00:02:39,810 --> 00:02:43,980 which this program might be run, but I want the computer to find it for me. 52 00:02:43,980 --> 00:02:46,670 It turns out that on a lot of computers, there's a program called 53 00:02:46,670 --> 00:02:50,820 env for environment, E-N-V, that can figure that out for us. 54 00:02:50,820 --> 00:02:57,020 So let's go back to the shebang and change it to be simply env space php. 55 00:02:57,020 --> 00:03:00,290 This new shebang will instruct the computer to check its environment for 56 00:03:00,290 --> 00:03:04,170 the program, PHP, and if it's there, use that to interpret this program. 57 00:03:04,170 --> 00:03:06,582