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