In this chapter we're going to be learning the basics of writing of PHP code. We'll start by opening the file 0201.php. This is a basic HTML file but we're going to put our first lines of PHP code in here. If we open this in the browser we'll see our simple text in between the body tags, This is my Basic HTML File. So let's see what we can do by entering in our first lines of PHP. First of all we have to declare where that PHP code is so that the web server knows what code to process. You do this by using two declaration tags. The less than bracket, the question mark followed by PHP tells the web server, here's where you can start processing PHP code. To end that you put the question mark and then the greater than bracket. Notice the syntax highlighting has changed, noting that it knows that now we've ended the PHP code. You can output simple text and already start to do some stuff with PHP simply by using the Echo Statement. You type in Echo space quote and then whatever's inside the quotes are strings that are sent directly to the browser. Now I need to also tell the web server not only where the PHP lines of code start to end but I also need to specify when my statements end. Otherwise if I just started another Echo Statement it's not going to know where this one ends and where that one begins. So you need to use the semi-colon which terminates all statements in PHP. So every piece of line of code that you write needs to be terminated by a semi-colon. Let's refresh that in the browser and there we have our first line of PHP. Notice that there's no return characters or white spaces. Well number one PHP ignores white spaces, you can have as much return characters and white spaces you want. And number two even if we did have some return characters and spaces in there remember the browser is interpreting HTML. So any kind of formatting or return characters need to be sent out in the appropriate tags. The other thing that is nice to know about PHP is the fact that you can interject this code anywhere you want this page. So I can add another line of this down in the body. Here's my second line of PHP. Now you can see we have our first and second lines and if we look at the source code on here you can see our first line is way up here and then we can see that our second line is down here. Notice that the return characters and the spaces do show up in the source code. So anything in the quotes will get outputted to the browser but the browser won't interpret them without the tags appropriate for HTML. Another thing that you'll need to know when you're first learning PHP is how to write comments. You can put a comment in PHP code just for signifying different parts of your code or leaving notes for yourself as a developer by using either the pound sign, two forward slashes or you can do multi-line comments by using a forward slash and the asterisk. Now when we refresh our page we'll see that, that code has been blocked out. Lastly I'll give you a special command in PHP. It's called the PHP Info Command and what it does is it outputs all the configuration options and the version of the PHP that's installed on your system. You can output that simply by typing php Info and the open and closing parenthesis. That's because that's a function and you always have to put your open and closing parenthesis after the function. Don't forget to terminate with the semi-colon. Now if we refresh the browser, you'll see that the PHP Info is outputted to the browser. This is great to have if you're ever trying to troubleshoot or trying to see what, how your PHP has been configured and what's been installed with it. So this concludes some of the basics of getting started with PHP.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |