Think about our LAMP framework as a bakery. Customers come in and ask the baker's assistant behind the counter for a particular kind of customized cake. The baker's assistant then takes those orders and hands them to the baker. The baker then goes to the stockroom and gets the ingredients, makes the cake, and gives it to the customer. In the same respect, the user goes to a website and puts in data (which are specific instructions, usernames, passwords, etc.) which is taken via PHP to the server, Apache. Apache then goes to the stockroom, which is SQL, and looks up and takes whatever data it need, and finally returns the request to the user.
As a real example, if a user goes to a webpage and puts in their username and password, PHP will take those values for username and password and hand them off to Apache, which will look them up in an SQL database and check the username (to see if they're actually a signed-up user) and check the password (to see if it's correct) and once that's done Apache will send the user to their logged-in homepage.
So, what's the difference between HTML and PHP? In the case of the bakery, HTML is more of how the bakery looks — it is the structure of the bakery: the counter, the display case, the ordering forms, the doors, and so forth. PHP is the baker's assistant: it isn't part of the architecture of the bakery, but it does the leg-work to bring things to Apache. It's easier to see the difference after you've coded for a bit, but if you're curious for a more concise answer, look here.
Important Note: PHP can be tricky. Make sure every line ends in a semicolon (like MySQL!), and make sure you spell everything right. Because we're doing this in gedit, there is no debugging which means if you run your PHP and it's not correct, you'll just return a blank page. Hence, if you get a blank page after doing your PHP and the page should not be blank, check your code and make sure there are no typos.
Other Important Note: Practice all of the following stuff in the index.html file that you made for the LAMP section above; but you will need to rename the file index.php in order for PHP to work. In general, if you have PHP in an HTML file, you'll want the file extension to be .php.
There are few better places to learn the basics than the w3. To get the most out of this course, you'll want to go through the entire "basic" part (and some of the advanced part, if you'd like!). This sounds like a lot, but because you've already done Python and C the syntax and way to write PHP should feel extremely familiar.
This is a beautiful tutorial that I wish I'd found earlier. Go through it and keep working at it until you completely understand what is going on. This is important. Out of all of the things we will be doing with LAMP, this is one of the more crucial ideas since it is center to much of the web: using PHP, Apache, and MySQL to send, access, process, and return data.
Note: You'll notice that in the beginning that you need to connect using mysqli_connect() with a number of commands; you'll always be using 'localhost' for the first part, you'll use your username for your MySQL program (if you didn't make another user, then the name is just 'root'), your MySQL password for the third part, and, optionally, the database you're working with for the last part. The important thing to note here is that even though you are putting in your MySQL password, it will not show up when users view the source of your website . In fact, users will not see any of the PHP you write in your website code, so don't worry too much about people finding out your MySQL password.
Another Note: If you look at other pages, you'll note that there's a slight difference in code: sometimes you'll see mysql_ instead of mysqli_ in front of things; the mysql_ tag is depreciated, which means it is not a good idea to use the mysql_ tag. For example, in some places you may see mysql_connect(...) instead of mysqli_connect(...); use the one with the "i". For reference, the "i" stands for "improved".
Final Note: Take this part seriously. If you don't understand something, google it. People probably will have had the same problem as you.
|⇐ Back to 4.2||Home||Onwards to 4.4 ⇒|