How does PHP Work?
PHP is a strange animal. It’s quite close to HTML. If you are totally unfamiliar with what HTML can do, here’s a very basic example:
Let’s say you have a webpage with the words, “My name is Fred” on it.
If you were to write that in HTML, your code would look exactly like this:
Not very difficult, huh? Well if you wanted to make the word “Fred” bold, then in HTML it would look like this:
What you’re seeing around the word “Fred” are two tags. An opening tag, and a closing tag. The opening tag is basically saying “Everything after me is BOLD!” and the closing tag is saying, “Everything after me WON’T BE BOLD!”
PHP is similar to using HTML tags like the above example in that you can interject PHP into existing HTML. It’s also a programming language.
PHP always executes on the server, which means that it has nothing to do with your computer while you’re sitting there surfing the net. Some of you might be familiar with clicking on “View” then “Source” when you’re looking at a web page in order to see the HTML that makes it happen. Well, you’ll never see any PHP there because it’s already been executed and only the end result is sent to you.
Let’s say that “Fred” was stored in a PHP variable (a variable is just something that holds a value that you give it – think of it as a container). That variable is “$Name”.
So if your code said:
Then any time we use “$Name” in our source code, you’re going to see “Fred” on your browser instead.
So all we need to do is find out a way to write code that basically says:
Incidentally with PHP you would do it like this:
Looks funny, huh? Well the “<?” and “?>” are opening and closing tags for PHP code. It tells the server to run that PHP code before it continues, and the “echo” places the value of “$Name” in that place. The result would be this appearing on the webpage:
My name is Fred!
We could fill the $Name variable with anything we wanted – “Bob”, “Joe”, or even “Gorgonzola”. In this case, PHP is only spitting that value back out.
I know what you’re thinking …
“Why not just type ‘My name is Fred!’”?
That’s a great question, and one I kept asking until I realized how cool PHP can be. Think of it this way. Let’s say we have a list of 5,000 names that we have to make web pages for.
Can we make 5,000 web pages and upload them? Absolutely. But it might be easier to make one web page and let it show us whatever we want. PHP allows you to do this because it is dynamic.
If I create a page with the words, “My name is Fred!” on it, I know that the page will never change. If I use PHP instead, then I can make the page say whatever I want in place of “Fred”. So instead of 5,000 pages, I only need one page that has a little bit of code to dynamically change the name. This saves me a TON of work!
©2014 Blue Moose Technology, LLC
Article Publishing and Re-Print Guidelines
- The article text, resource box, URLs copyright information and these guidelines must be left intact and unchanged.
- When re-published online, all links must be live hyperlinks.
- These articles may not be distributed in any manner that does not comply with federal communication guidelines. Be Good!
- These articles must not be password protected or limited to membership or paid-only viewing. Keep 'em free, folks!
You can easily link to this article!
If you'd like to link to this article from your own website, copy and paste the following HTML code onto your page:
On your page, the code will appear like this: