But now you want more.
If any of these are the case-and you may find that an of these are the case-learning PHP and MySQL is a great way to take a giant programming step forward. Even if you’ve never heard of PHP you’ll find it’s the best way to go from building web pages to creating full-fledged web applications that store all sorts of information in databases. This book shows you how to do just that.
What PHP and MySQL Can Do
PHP can handle payment processing on its own and it can connect with services like PayPal and Google Checkout. PHP can store and load images from a database or a file system and give you the ability to log users in and out as well as control what they see throughout your application.
Add in MySQL and you can store your users’ names, addresses, billing data, and even their preferences regarding the color of their own personal landing page. MySQL can store just a few bits of data, a few thousand lines of data or every page access by every user who ever logs into your application.
And, of course, PHP can easily connect to MySQL. PHP can do everything from grabbing a user name based on a user ID to storing the details about financial transactions to actually creating tables and updating their structures, and MySQL can back-end all that work and store that data. Ultimately, this is the stuff of web applications; it’s what a web application is
Obviously, web applications like this aren’t simple. They have a lot of complexity, and that complexity has to be managed and ultimately tamed into a usable, sensible web application that you can maintain and your users can enjoy. That’s what this book is about: building web applications, and doing it with an understanding of what you’re doing, and why you’re doing it.
What Is PHP?
PHP started out as a set of tools for doing simple web-related tasks. It appeared on the Web scene way back in 1994. Initially, PHP did nothing more than just track visits to a particular web page (the online resume of Rasmus Lerdorf-the inventor of PHP). It was then expanded to interact with databases, as well as provide a tool set for online guest books and HTML form processing. The next thing you know, it was hugely popular as an alternative to less web-friendly languages like C.
New versions of PHP started coming out, and an increasing number of web programmers adopted it as their scripting language of choice for web tasks. PHP 3, 4, arrd now 5 are now mainstays on the Web. PHP has become fast while remaining lightweight. And, of course, its ability to easily interact with databases such as MySQL remains one of its most attractive features.
What Is PHP Like?
PHP Is All About the Web
If you came here for web programming, you’re in the right place. Although you can write PHP programs that run from a command line (check out Figure 1-1for an example), that’s not really where it excels. The PHP programs you write run within your website, part and parcel with your HTML forms, web sessions, and browser cookies. For exarnp!e, PHP is great at integrating with your website’s existing authentication system, or letting you create one of your own,
You’ll spend a lot of time not just handing off control to an HTML page, but actually writing the HTML you’re already familiar with right into your PHP scripts. Lots of times, you’ll actually write some PHP and then write some HTML all in the same PHP file, as in the following example:
This script references another script, and then extracts a user’s 10 from the request parameters sent by a web browser. The script uses that 10 to search a database for the rest of the user’s information. Then, it builds the data into a web page that’s created on the fly.
The result? Pages that are both full of HTML and have dynamic content. like Figure 1-2.
If you’ve written some Java scnpt and if you’re checking out this book, that’s probably the case-you know that Java script lets you get away with just about anything. You can occasionally leave out semicolons; you can use brackets, or not; you can use the ve: keyword or not. That sort of looseness is great for getting things working quickly but at the same time, it’s frustrating. It makes finding bugs tricky at times, and working across browsers can be a nightmare.
PHP is not quite as loose as Java script, so it makes you learn a little more structure and tighten up your understanding of what’s going on as your program is constructed and then run. That’s a good thing, because it will end up making you tighten up your Java script skills, too. And, perhaps best of all PHP’s stodgy consistency makes it easier to learn. It gives you firm rules to hang on to rather than lots of “You can do this or this or this ..
50 get ready. There is a lot to learn but everything you learn gives you something on which to build. And PHP, lets you know right away when there’s a problem. four won’t need to pop open an error console or keep an eye out for the tiny yellow warning triangle in Internet Explorer as you do with Java script. More often you’ll get a nasty error that stops you in your tracks and screams “Fix me!” And, over the next couple of hundred pages, you’ll be able to do just that: fix the problems you’ll run across in typical PHP programs, whether you’ve written those programs or someone else has
PHP Is Interpreted
PHP code comes in the form of scoots, which are plain-text files that you create and fill with code. Whereas HTML uses lots of angle brackets and keywords like html, head, and ul, PHP uses lots of dollar signs ($) and keywords like mysql_query and echo. 50, HTML and PHP don’t look at all alike. But where they are alike is in the basic underlying format: they’re both just text. You can open up an HTML document not just in a web browser but in Notepad or an integrated development environment (IDE) like Eclipse or even a command-line editor like vi or emacs. The same is true for PHP: it’s just text. So get ready throughout this book, you’ll be typing words-albeit strange ones, with lots of underscores-and saving those words into text files called scripts.
Once you’ve got a script, you let a PHP program interpret that script. The PHP inter or eter is a piece of software on your web server that reads your script and makes sense of it, giving the web server output and directions about where to go next or how to handle a user’s form field entries. Your script-remember, just a text file-is interpreted, one line at a time, every time it is accessed.
This is a bit different from languages like Java or C++, which are cornoitea. In those languages You also write your code in text files but then run a command that turns those text files into something else class files binary files pieces of unreadable code that your computer uses.
PHP Doesn’t Run in the Browser
save, and open. Pretty easy stuff
Right now then you just need to know two things:
• It’s going to take a little more work to get your PHP programs working. You can’t just write and save a script and then open it the way you can HTML. Don’t worry you’ll learn exactly how to get PHP working both locally-on your computer-and remotely-on a web hosting company’s servers. But it’s going to take a little more effort.
It’s not trivial to set up everything you need to run PHP programs on your own computer-especially once you involve MySQL too (more on this in just a moment). That’s why Internet Service Providers (ISPs) and web hosting companies exist! They take care of that sort of thing. So although it’s possible to do all your PHP coding on your own machine it’s a lot more common to write your scripts and then send them to a remote web server. Sound scary? It’s not…but it’s important. You’ll spend a good bit of time in this book writing code and uploading it to a server
PHP is different from Java script and HTML in some important ways. You’ll get used to those differences but you’ll be a lot less frustrated and confused if you go in knowing that you’ll have to do some things differently when it comes to PHP