If you already know a programming language you will see a lot of the same concepts in PHP. All the logic is still the same, you’ll just have to adjust to a new syntax. If you have never programed before, don’t worry, it’s not nearly as scary as you think!
The first thing you need to know is what PHP actually is. PHP is a scripting language that is often interwoven with HTML. It helps your web page do things that HTML alone can not do. It is a programming language able to perform many tasks, from simple adding to complex Boolean problem solving. From just a few simple concepts strung together you can make a PHP program do whatever your website needs.
PHP is often used in conjunction with MySQL. MySQL is a relational database system. A database is a place where you store data and information, and relational means the the database itself has many layers of information strung together. Information that your PHP program uses is stored in this database. Not all programs use MySQL, but often if you are required to store information you will choose to use it.
Before you start learning PHP it is good to know some HTML basics, since we use HTML often to formatt our PHP output. You will also want to know the basics of using an FTP program, as well as a plain text editor.
Now it’s time to get started... brace yourself you are about to write your firsts lines of PHP code!
Every PHP program you write will start the same way, with <?php This essentially turns PHP “on” in the file. As I mentioned before you can switch from PHP to HTML and back again, but all of the PHP is inside of the <?php tag. The “off” tag is just ?> Let’s have a look at how that will appear in your program:
You may have noticed that inside the PHP I used a // before my comment. In PHP anything with a // before it is ignored. This is handy if you want to leave yourself or another programmer notes about what you are doing. Trust me, you may not think you need it but when you go back to revise code you wrote years ago you’ll be happy to have the reminders! Well, we turned PHP on and off, now let’s actually make it do something! As is tradition, let’s say hello to the world:
Possibly some HTML
<?PHP //some PHP code here ?>
Possibly some more HTML
<?PHP Print “Hello World!”; ?>
A few things you need to notice here. The first is that we used PRINT to make our program say “Hello World!”. Next you will notice that what we are saying is within quotes, and finally you will notice that we end our line with a semicolon.
Remember I told you we could interweave PHP and HTML? Well Let’s try it!
And there you have it, your first PHP program!
<html> <head> <title>My First PHP Program</title> </head> <body> <center> <b> <?PHP Print “Hello World!”; ?> </b> </center> </body> </html>
It’s important to mention that in place of print you can also use echo. They do basically the same thing.
If you want to run your code, make sure you save your file as a .php file. If you have a plain text editor you should be able to choose “save as” and then save it as yourfile.php. Make sure you don’t accidentally end up with yourfile.php.txt, you just want the PHP extension. You also have to make sure you run it somewhere with PHP. If you do not have PHP installed on your computer, upload it to your web server to test it out!
Stay tuned for lesson 2!