Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Intro to Workspaces and PHP Code3:53 with Alena Holligan
Workspaces make it even easier then normal to get up and running here at Treehouse. We have supplied you with a web based server and the starting code you'll be working with. We'll use workspaces to write our first program and view it in a browser.
Workshop: Using Treehouse Workspaces
Workshop: Basic Error Handling in PHP
PHP 'echo' and 'print' Statements
echo and print are more or less the same. They are both used to output data to the screen.
The differences are small: echo has no return value while print has a return value of 1 so it can be used in expressions. echo can take multiple parameters (although such usage is rare) while print can take one argument. echo is marginally faster than print, which is why it is most commonly used.
Most of the time you hear of PHP as a language for creating web pages. 0:00 But PHP isn't limited to the web, 0:04 PHP is also very useful as a general scripting language. 0:06 You can write and run scripts directly on your computer without a web browser 0:10 using what's called the command line interface or CLI. 0:15 We'll start our projects using the CLI in the workspaces console before adding them 0:19 to a web page in the last section. 0:24 To follow along in your own workspace, click the launch workspace button. 0:27 If you're looking for 0:31 a little more explanation on workspaces, check the teacher's notes. 0:32 Let's get started. 0:35 When learning a new programming language, 0:38 it's a tradition to start with a simple hello world program. 0:40 So let's start off by creating a new file named Hello. 0:44 We'll give this the extension .php. 0:50 You place php code inside what's called a code block. 0:53 Much like HTML tags, the opening tag is <!--?php, 0:58 and the closing tag is ?-->. 1:05 We then place our code within this code block. 1:09 To display something to the screen we use the echo command, 1:12 followed by whatever we want to display. 1:16 In this case we'll type, Hello World. 1:19 We'll surround this text with quotes and end the line or 1:21 statement with a semicolon. 1:25 A statement in programming can be defined as an instruction to perform a specified 1:28 action, such as setting data, retrieving data, performing a calculation and so on. 1:33 Statements in php almost always need to end with a semicolon. 1:38 Now let's run our script in the console. 1:44 If your console is not visible go to View, Show Console, 1:46 then type php followed by a space, and then the name of the php file hello.php. 1:52 This tells the console we want to use the php command 1:58 line interface to process the file, hello.php. 2:03 This is often called running a script or executing the script. 2:07 Don't worry, scripts aren't hurt when you execute them. 2:12 In the console we see the output, Hello World, but 2:15 not the php tags or the echo command. 2:19 This is because the php command line interface, or cli for short, 2:22 processes the php code and returns only the output. 2:26 What we're talking about php code., let me point out that with few exceptions, 2:33 php does not care about extra whitespace such as spaces, tabs, or blank lines. 2:37 You can have things all smooshed up together, or spread out, however you want. 2:43 But remember, with great power comes great responsibility by following 2:47 best practices and spacing your code clearly, 2:52 you can make your code easier to read for others and for your future self. 2:55 Congratulations! 3:01 You've written your first php script. 3:02 To get the most out of this course, let me provide a few tips. 3:05 If you hit a part of the video where you're struggling to follow along, 3:08 you can always slow down or speed up the videos. 3:12 Also, feel free to watch the videos more than once. 3:15 Furthermore, I've included additional information in the teacher's notes. 3:19 So don't forget to check those out as well. 3:23 If you don't write your code exactly right, php will give an error. 3:26 If you want to learn more about error handling, 3:30 check out the workshop in the teachers note. 3:33 And finally, not only is the php community as a whole fabulous. 3:35 You also have an awesome community of fellow students here at Treehouse 3:40 who can help you with questions that you might have. 3:45 Sometimes it just takes another perspective 3:48 to help you really understand a new concept. 3:50
You need to sign up for Treehouse in order to download course files.Sign up