Join Kevin Skoglund for an in-depth discussion in this video What is PHP?, part of PHP Essential Training.
- We are going to start out by talking about what is PHP. PHP is a server-side scripting language. Now, you may have thought that PHP was a programming language. Well, technically speaking, it's not. So, how is a scripting language different from a programming language? The distinction between them is largely artificial. The lines can get a bit blurry. But we can do a general comparison. A script only runs in response to an event. It also usually runs a set of instructions by working down the page from the start to the end. It has low or no user interaction after that initial event.
So PHP script does not run until a web page is requested. Then it launches, follows its instructions from top to bottom, and then quits until another action launches the script again. On the other hand, a program runs even when not responding to events. It continues to run until wait for interaction, whether that interaction comes from a user making choices, or from other programs or input. A program also jumps around its instructions a lot more, so there's often not a clear start and end point. And, it often involves lots of user interaction. Photoshop is a good example of an application.
After you launch it, it keeps running, waiting for more interactions or for you to tell it to quit. The tasks that it performs are not a linear set of instructions. It jumps around based on the task you want to do at that particular moment. But, as I said, the lines get blurry. As scripts get more complex, they start to resemble programs. In the simplest programs, they're basically just scripts. So, you could say it's a distinction without a difference. But, we still call PHP a scripting language. Now, what does it mean when we say PHP is server-side? When we talk about server-side and it's opposite, client-side, what we're talking about is where the code does its work.
Because PHP runs on a web server, that means it generally can't run on its own. We'll need to have a running web server in order to use PHP. PHP code does not need to be compiled. It's executed by the web server exactly as it's written. Other programming languages, such as C or Java, require the code to be compiled, or translated into another form before it can be used. We'll be able to just write our PHP, put it where our web server can find it, and then we can load up the web page and see the results. PHP is designed for use with HTML. It can be embedded in our HTML, and we can use it to generate HTML.
In the end, PHP is going to return HTML to the browser. PHP code is going to be our input and web pages are going to be our output. Now, if you've been working with HTML, you're already familiar with having .htm or .html at the end of your filenames. PHP is going to work exactly the same, but we're going to put php at the end. The PHP is going to tell the web server that this file contains PHP code that needs to be executed. PHP is going to provide more flexibility than HTML does on its own. HTML pages are static by their nature, so all visitors to a web page see that same page all the time.
PHP let's us create dynamic pages. Page content can change based on conditions, such as interactions with the user, or data stored in the database. You can think of PHP as turbo-charging your HTML. PHP syntax is going to be very similar to C, Java and Perl. The small details are going to vary quite a bit, but the structure of logical expressions and loops, those kinds of things will be familiar to anyone with programming experience in one of these languages.
- What is PHP?
- Embedding PHP code on a page
- Inserting code comments
- Variables, strings, arrays, and Booleans
- If, else, and elseif statements
- While and for loops
- User-defined functions
- Function arguments and return values
- Debugging and troubleshooting