php for beginners
PHP is a coding language where instructions are executed on a server, and then the final output is generated in html that is then sent to the web browser.
At first maybe this sounds confusing. But remember, your web browser only understands one language and that’s html. But remember that html is really limited in what it can do, so when we want to get fancy and do things that require a whole lot more smarts, we need to choose a “smarter” coding language like php. So, when we code in php, it will do a bunch of stuff on the server – but ultimately we want it to render some output in the form of a web page, and the only way for it to do that is to generate html to send to the browser.
Because php runs on the server and not in the browser, it is referred to as a “server side” coding language.
PHP is commonly used in conjunction with a database language, so that it can store data. Most commonly on Unix systems that coding language is mysql. We’ll discuss mysql in a separate section. For now we’re going to give you some basics of working with php.
With html, the first file to load for a given website was index.html – in php, it is index.php
Notice that php files are named with .php at the end so the server knows they are php files.
Because php and html can be mixed together within the same file, we need to tell the server where to being interpreting things as php and where to stop. So, all php code needs to begin with <?php and end with ?>
//In almost all coding language, there is also some requirement at the beginning of the file so the server knows what kind of file it is working with and knows what to do with the file and where the code begins and ends. In php, it is required
If you read through our html section, we began by showing you how to have it do nothing –
Well, that’s just as easy in php –
That code will do absolutely nothing.
So, let’s make it print something, and also add a comment –
<?php // This is a php comment echo "CodingForBeginners.com is my favorite website"; ?>
From this tiny bit of code, there are quite a few nifty things to observe.
Nifty thing #1 – the echo command will print to the screen whatever follows in quotes. But what’s that semicolon doing after the closing quotes?
Nifty thing #2 – every line of php code (other than comments) needs to end with a semi-colon (there are some other exceptions which you’ll learn later)
Nifty thing #3 – Can you spot the format for commenting? That’s right – if you want just a single line of comments, you start it out with two forward slashes followed by your comments. There are other options for commenting, but this will get you started
Why do we add comments anyhow? There are many reasons. Usually the most important reason is to help you understand your own code when you come back to it later on. Maybe you’re writing some really fancy code that does something really complex and you might need to write it over many days or weeks or even months. The comments will help you to understand and remember all of the important details of your own code.
But it’s not just for you. PHP was initially developed as a very open language. PHP itself is open-source. So, later on if somebody else needs to troubleshoot something with your code, or if somebody else wants to use your code and alter it in some way or extend its use, the comments will help them to understand what you’ve done. I’ve tried to figure out the code of others, usually to troubleshoot client websites, and believe me – it’s way easier when there are comments in there.
PHP is a very flexible and powerful coding language. Let’s take a look at some examples of what php can do.
Conditional logic operations – (decision) –
Database manipulation with MySql
User input, forms, e-commerce