Introduction PHP Help

Given that you’re reading this book, the chances are good that you’ve built a web page in HTML. You’ve styled it by using Cascading Style Sheets (CSS) and maybe written a little JavaScript to validate your custom-built web forms. If that wasn’t enough, you’ve learned a lot more JavaScript, threw in some jQuery, and constructed a whole lot of web pages. Maybe you’ve even moved your JavaScript into external files, shared your CSS across your entire site, and validated your HTML with the latest standards.

But now you want more.

Perhaps you’ve become frustrated with your website’s inability to store user information in anything beyond cookies. Maybe you want a full-blown online store, complete with PayPal integration and details about what items are in stock. Or maybe you’ve simply caught the programming bug and want to go beyond what HTML CSS, and JavaScript can easily give you.

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 a programming language. It’s like JavaScript in that you wend most of your time dealing with values and making decisions about which path through your code should be followed at any given time. But it’s like HTML in that you deal with output-tags that your users view through the lens of their web browsers. In fact, PHP in the context of web programming is a bit of a mutt; it does lots of things pretty well, rather than just doing one single thing. (And, if you’ve ever wondered wify it’s called PHP, see the box on the following page.)

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,

php coding

php coding

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:

php coding

php coding

php coding

php coding

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.

php and MySQL

php and MySQL

JavaScript Is Loose, PHP Is… Less So

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.

The beauty of an interpreted language like PHP and JavaScript for that matter-is that you write your code and go. You don’t need a bunch of tools or subsequent steps. You write PHP, test it out in the browser and then write some more. It’s fast and that usually means it’s pretty fun

PHP Doesn’t Run in the Browser

There’s one other big difference between PHP and what you may be used to with HTML CSS, and JavaScript. It’s a big difference too in fact this difference is such a big deal that it’s going to affect everything you do when it comes to writing PHP scripts getting those PHP scripts to run, and checking them out in a web browser.

So what’s the difference? It’s this: PHP, unlike HTML or CSS or JavaScript, doesn’t run entirely in a browser

What does that mean? begins to get into the details, but for now, you just need to know that HTML, JavaScript, and CSS are entirely handled by your web browser software. Whether you use Internet Explorer, Apple Safari, Google Chrome, Mozilla Firefox, or Opera, once you have a browser, you have everything you need. That’s why you can write an HTML document, save it with an extension like .html, double-dick that file and voila: your browser opens (assuming you’ve got things set up on your computer the right way) and you’re looking at HTML. You can reference CSS in that HTML file as well as JavaScript, and the same thing happens. Write code,
save, and open. Pretty easy stuff

With PHP, you’ll need a bit more than that. The PHP interpreter interacts with your browser but doesn’t run in the browser automatically. In other words you cannot simply double-click a PHP script and expect a browser to pop up and handle things. HTML forms that submit to a PHP script won’t “just work” the way that HTML and JavaScript do.

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

Posted on January 13, 2016 in Installing PHP on Windows Without WAMP

Share the Story

Back to Top
Share This