Basic PHP Syntax
with
Don Schechter
PHP is a server side scripting language that, very often along with databases created with SQL, can create dynamic webpages. Learn basic PHP syntax in this demonstration.
Transcript: Basic PHP Syntax
Hi! I'm Don Schechter for About.com Computing. Today I will show you the basics of PHP. PHP is a server side scripting language that, very often along with databases created with SQL, can create dynamic webpages.Start a PHP File
The very first step when coding with PHP is to start with a blank file. I am using notepad. I will go to file save and call it test.php. Make sure that you add the extension .php at the end.Create a Tag for PHP Code
Now we need to create a tag letting the server know that there is php code. So type a bracket, question mark,then php. Following this will be our code.At the end of the code, we need to add another question mark and a closing bracket to state that the php statement is complete. Anything that is outside of this tag will be read as HTML. If you want, you can leave out the php text in the tag and just start with a bracket and question mark.
Add a Comment to the PHP
Comments are always a good idea when you want to explain what a function or piece of code does without it being displayed. To create a comment, you can type two slashes and then a comment on one line.Alternatively, you can write your comment after the number sign. If you have a large block of text you can type a (IS THIS BACK?) slash followed by an asterisk. Then write your block of text and end it with another asterisk and slash.
Output Text in PHP
Now, let's output some text. All we need to do is add the word echo followed by our text in quotes. I will write Hello World. If you want to add another line of text, just add a semicolon after the quote and, on the next line I will type echo again and, in quotes, Hello Again.Upload the PHP File
I will save this file and upload it via FTP to my web server. If I open up my Web browser and type in the URL where that file is saved, we will see our text. Notice that there is no line break between the two statements. If I go back to my code I can add an echo in between them. In quotes I will add an html break tag. Once I save and upload the file again, I can hit refresh and there is the text on the second line.Variables in PHP
If you have used variables in other languages, then PHP will seem familiar. Just type a dollar sign and make up a name for your variable. I will create $text1. To give this a value I'll insert an equals sign and then Hello World in quotes again. I can create another variable $number that is an integer. I can write a number, 312 and don't need to put it in quotes.There is a lot more you can do with PHP but this should help give you a feel for what to expect when working with PHP. Thanks for watching. For more information, visit us online at computing.about.com.
