PHP What Why and Where? PHP Help

PHP is ultimately just text that is taken by your web server and turned into a slit of commands and information for your web browser. And because you’re just working in text, there’s not a lot you have to do to get going as a PHP programmer. You need to become familiar with PHP itself, and the best way to do that is to install PHP on your own computer as well as becoming familiar with how PHP runs on a remote web server.

Then, you need to run an actual script. Don’t worry; it’s amazingly easy to write your first program in PHP.Not only that, you’ll run your script. upload it to your web server and access your script with a web browser …and that’s all in the first two chapters!

Throughout the process, you’ll begin taking control. With PHP,you become an active participant in your web pages. PHP lets you listen carefully to your users and say something back. So get going; there’s no reason to leave your users with passive HTML pages any longer

PHP Comes in Two Flavors: Local and Remote

One of the most difficult things to get a handle on when it comes to PHP prl5gramming doesn’t have much to do with programming at all. It’s figuring out just how PHP runs, how it interacts with your web browser and web server, and why it’s not possible to just double-click a PHP file on your hard drive and see the script in that file run.

HTML and CSS Run Within a Web Browser

First, it’s worth thinking back to when you were a wee programmer, writing your first HTML page. You could save that page in a file, name that file with a ./liml extension, and boom-you had a web page. Double-click that file, and on most computers, you see that page open up in a web browser. That’s because just as acor file is connected to the Microsoft Word program, a ntrn! file is connected to a web browser (specifically, the browser you’ve chosen as the default on your computer). Figure 1-1should give you an idea.

HTML and C55 Run Within a Web Browser

HTML and C55 Run Within a Web Browser

If you keep thinking back, you probably added some styling to your HTML pages. Using the style attribute and <style></style> tags in your HTML document, you could change fonts, add striping to your table rows, and generally spice up other-  wise boring text.

Then, at some point, some well-meaning web designer slapped your hand and insisted that you start writing all your CSS in external style sheets, and referencing those files in the head of your HTML, like this:

<link rel·”stylesheet” href·”styles/mysite.css” type·”text/css” />

You might even have a few style sheets for the benefit of people viewing your website on mobile devices or printing out a page

<link rel.”stylesheet” href·”styles/mysite.css” type·”text/css” media·”all” />
<link rel·”stylesheet” href.”styles/print.css” type·”text/css” media.”print” />

But you can still double-click that HTML file, and your browser knows what to do (see Figure 1-2). That’s because, once again, the web browser is completely capable of not just rendering HTML, but applying all those C55 styles to tl’re page, too. Again, no extra software needed

At this point, even though you’re using only two technologies-HTML and CSS-you need only a single program to handle those technologies: the web browser.

HTML and C55 Run Within a Web Browser

HTML and C55 Run Within a Web Browser

JavaScript Adds Complexity, but Not Software

Next up in the pantheon of web technologies that every designer and fledgling procrarnrner needs to learn: JavaScript. Suddenly, you weren’t limited to elements that never moved and text that never changed. Whether it was simple phone number validation, more advanced jQuery functions that turned boring gray boxes into animated buttons and <div> elements into tabs, or even the new HTML5 canvas object, within which you could build entire JavaScript-based 3D games, your pages suddenly had new life with JavaScript.

But just as with HTML and (SS, JavaScript is at heart a web technology, and even more specifically, a /JY()!’v’;er-oascC1 tectmoiociy. In other words, support for JavaScript is part and parcel of your web browser. In fact, if a new version of JavaScript were to appear-something that rarely happens these days-you’d need to download a new version of your tJi ,:-,;” r to get that version of JavaScript. Just as you can’t upgrade your HTML installation outside of your browser, you can’t upgrade your JavaScript installation outside of your browser

You Probably Have Multiple Versions of JavaScript Already

Think about it: if JavaScript is built in to your browser, and you have more than one browser, you actually have multiple installations of JavaScript on your computer. Suppose that you have Internet Explorer and Firefox; you’ve got the JavaScript tnstallatlon that came with Internet Explorer “r;{i the one that came with Firefox. Add Chrome or Opera to the mix, and you’ve got a few more installations. And, if you have multiple versions of a single browser-like Firefox 3.6.3 for testing with older Linux-based systems and the most current version (14 something-or-other as of late), they each have a different JavaScript installation. Even though JavaScript doesn’t g~t updated very often, those multiple installations usually translate into multiple versions of JavaScript, because JavaScript isn’t a product that is distributed by a central organization to browser developers. Rather, document that defines how things should work to be considered as JavaScripl. That means that each browser has to write code that matches that specification so that they can say, “Yes, you can run JavaScript in our browser!” Furthermore, each browser does things a bit differently, and ‘that’s why a website feature that works perfectly in Firefox’ might not quite work perfectly in Internet Explorer, and vice versa. So, even if you have two browsers that implement the same version of the JavaScript specification, tpe code in that implementation isn’t identical; there are usually differences. What does all this mean to you? Test your code- JavaScript, PHP, or otherwise-in as many browsers as you can. Things aren’t always the same in every browser, and it’s up to you-not your users-to handle inconsistencies

Figure 1-3 shows you how Java5cript fits in (hint just as HTML and C55 do).

Figure 1-3 shows you how Java5cript fits in (hint just as HTML and C55 do).

PHP Is Not Part of Your Browser

And here’s where things change from the easy, browser-centric view of the world. When you download a web browser, you get HTML, C55, and Java5cript, but you do t get PHP. PHP scripts-which you’ll soon be writing-have to be interpreted by the PHP interpreter program, called oro. And, you can’t just add a PHP interpreter to your browser. It doesn’t know what to do with scripts and isn’t built to interpret PHP.

Instead, you need PHP on a web server. It’s the web $0flf”I-not the web L)ro’o/,,,”- that can interact with a PHP interpreter. Your browser can handle HTML on its own, but it has to make a request to a web server to deal with PHP scripts. That server can take your PHP scripts and run them, and then take the response and send it back to your browser. Your browser can then understand and handle the response.

So, Figure 1-4 adds a couple of new wrinkles: the PHP interpreter, the magical thing that takes the PHP scripts you’ll be writing and does something useful with them; and a web server to communicate with that interpreter. These both live outside of your web browser. In this scenario, the browser now makes a request to the server and then takes the response and shows it to you

PHP Is Not Part of Your Browser

PHP Is Not Part of Your Browser

1. A web browser makes a request for some page. That page might be a URL on a remote web server, or a local file on your computer.

2. Assuming that the request goes to a web server, the web server returns HTML (and C55 and Java5cript).or, in the case of PHP, passes the PHP request on to the PHP interpreter.

3. The PHP interpreter does what it’s supposed to: it interprets, or runs, the PHP.The result of that should be something that a browser car1.understaod,like HTML. It passes this result, or response, back to the web server.

4. The web server gives the browser back something that the browser can understand: the HTML result of interpreting a PHP script, or C55, or Java5cript, or a combination of all of the above.

Understanding this difference in how PHP works, as opposed to HTML, C55, and Java5cript, is important because it determines the approach you’ll take to writing PHP scripts and getting those scripts to run.

Write Anywhere, Run Where There’s PHP
The cool thing about HTML, C55, and Java5cript is that because they’re built in to browsers and you can download browser-s so easily, those technologies become instantly available. It’s tough to even find a computer wit/wuta browser preinstalled. So, you turn on your computer for the first time, and boom, you can start creating web pages immediately. Double-click the HTML file, your browser fires up, and you’re good to go.

But PHP isn’t part of that browser. It’s not always preinstalled. If YOLl write a PHP script and then double-click it, you’ll probably see a code editor launch, but not something that will actually run that script. Even worse, if your browser does open up your PHP script, it’s not a web server. It doesn’t have a PHP interpreter. It will just show you your code, rather than run it, and what good is that to anyone?

This long prelude is just a big warning: although it’s easy enough to start writing PHP scripts, you can’t just open them in Dreamweaver or Fir~fox and expect them to run. You’ll end up frustrated and annoyed, and that’s no good for anyone .

1. You can go through the lengthy process detailed In the next section and install PHP on your local computer. This process will take some time, and you’ll have to monkey around a bit with your computer at a system and network level. You’ll also need a local web server to handle the PHP interpreting part of the gig. This way, you’ll not only have a browser that can handle HTML, (55,and Javascript, but a complete setup that can take on PHP without a problem, too-right on your own computer.

2. You can write your scripts locally and always upload them to an Internet Service Provider (ISP) or web hos.tlng company. Every 15Pand web hosting company supports PHP, and you usually don’t have to do anything more than name your scripts with aphp extension. This option involves less initial setup, but it means that every time you edit your script, you need to upload it again to your 15P.It also means that double-clicking your PHP script won’t do aPlything-more than, at best, open your editor. You can’t test your scripts on your own computer.

Both choices are equally good, and which one you choose depends largely on your circumstances. Even though it might seem perfectly natural to jump right into uploading your scripts, you aren’t always qoinq to have a network connection. (The SOUM you just heard was the cheering of all the programmers who have an hour-long commute into work on their local metro or subway!) For those unwired situations, it’s nice to be able to keep developing on your own computer without the need to access your hosting provider. Note only that, installing PHP on your own computer is great for understanding what the PHP interpreter actually does.

So, before you start writing scripts that you can’t even run, it’s time to get PHP working on your own computer (if you want to), and then talk about getting scripts running out there in the wild, as well.

PHP on the Windows-Based Computers (Wamp Server Installation)

Open your favorite web browser and head to This is the online home of WAMP, which stands for Windows, Apache, MySQL, PHP. The site is shown in Figure 1-5.

Select the relevant Download link for your version of Windows. If you’re not sure, you can go to your Control Panel, select System, and then poke around. You’ll see either “32-bit Operating System” or “64-bit Operating System,” and that tells you what you want. Just select the first link on the top-left of the page that matches your system.

When downloading starts, you see a warning-actually, a couple of them-about needing some C++ extensions. Click the link for your system (see Figure 1-6), download the extensions (see Figure 1-7), and then run the downloaded file. You’ll need to allow the downloaded program to update your system, accept a license agreement, and install the extensions. When that’s complete, a screen appears like the one in Figure 1-8.

wampserver

wampserver

wampserver

wampserver

wampserver download

wampserver download

VIsual Studio

VIsual Studio

Once you’ve installed the C++ extensions, go back to  select Downloads again, and then click the download link. This time, you can ignore the warning. Click the words “you can download it directly.”

The ad-heavy site you’re taken to will trigger a download in a few seconds. Then, save and run that file; you’re installing WampServer. Figure 1-9 is what you’re aiming for

wampserver setup

wampserver setup

Accept the license and default installation directory (typically C: ,wamp). You might want to create a Quick link icon, or at least a desktop shortcut. and then let installation take off. Select your default browser. You’ll then be asked about allowing Apache to access public networks (Figure HO). The best option here is usually the default supplied by the WampServer installer.

wampserver setup

wampserver setup

You then have a few other options for PHP mail, and then you’re finished. Launch WampServer, and you should see…nothing! Well, almost nothing. On the right side of the taskbar, notice there is now a little green “W” (check out Figure 1-11)

PHP MySQL

PHP MySQL

Click the green W icon to see all of the things you’ve been reading about, like PHP, MySQL, and Localhost, as shown in Figure 1-12

wampserver

wampserver

You’re almost done. Select the top option, Localhost. (If you don’t remember what local host means, see the box on page 22.) A new web browser window or tab opens with an address that references your own locally installed web server. This Server Configuration page presents information about your own web server setup (see Figure 1-13). It isn’t particularly impressive to look at, but it’s proof that your Windows computer can now serve up web pages.

While on the Server Configuration page, in the Tools section (about halfway down the page), click the phcntC() link. A page opens that looks something like Figure 1-14,which , is everything you’ll ever need to know about your local PHP installation.

More important, it means that your browser made a request to a web server, and that web server processed some PHP (the phpinfo function) and handed back a response to your browser. Not only CdI’ you run PHP on your computer, you just did

server configuration

server configuration

PHP version

PHP version

PHP on the Mac (Default Installation)

If you’ve got a Mac, you’ve got more than just a sleek, shiny machine and way too many ways to spend even more money with Apple, you’ve already got PHP installed. To prove it, open the Terminal application on your Mac. If you’ve never used Terminal, don’t worry; you’ll get used to it quickly and find it’s one of your best friends for working with PHP. Go to Aooltcaticns-eutilities-e Terminal.

Once you’ve found the Applications folder, open it and find the Terminal application. It looks like a computer monitor with a black screen and a little white arrow, as shown in Figure 1-15

Terminal program

Terminal program

Terminal program

Terminal program

To ensure that PHP is installed on your system, type php (all in lowercase letters) and press Enter. Unfortunately, the way to know things are working is if you con’t see anything but that blank cursor, a little further down in Terminal. It won’t even blink at you anymore; it’s just a boring, dark gray square.

Press Control-C to stop that single eye from hanging around and to display the blinking cursor again. This time, type which php. The which command lets you know where on your computer the program you type is located. In this case, you’re asking where the php program is located. You’ll probably get something back that looks like Figure 1-17;for the computer in this example, php is in the /usr/bin directory. You’ll probably get a similar result.

Terminal program

Terminal program

PHP on the Mac (MAMP Installation)

Although it’s nice that Macs come with PHP already installed, there might just be” a better option-one worth doing a bit of downloading and installing for yourself. That better option is MAMP, which stands for Mac, Apache, MySQL, PHP. This is the Mac counterpart to WAMP, the easy windows PHP installation (page 22) that you, as a Mac user, probably skipped.

MAMP doesn’t improve on the PHP installation that came on your Macs; it does integrate MySQL-which you’ll need before you know it-as well as the Apache web server and several helpful tools for working with PHP scripts and MySQL databases. You even get a simple control panel for starting up your local web server and MySQL database. Those additions are a nice perk, and coupled with how easy it is to install MAMP, you might just want to ditch the default PHP installation and get MAMP going.

MAMP and MAMP pro

MAMP and MAMP pro

Simply click the “Download now” button under MAMP and then grab a coffee and wait for the installer to download.

Now, launch the installer. Click Next a few times to select your hard drive and agree to the license. Keep going until the installer informs you that MAMP is ready to install, as shown in Figure 1-19.

Standard Install on Macintosh HD

Standard Install on Macintosh HD

Once the installation is complete, go to Applications-tMAMP. You’ll see a nifty control panel, a la Figure 1-20,

MAMP

MAMP

Your installation might try to automatically start both an Apache server and the database, Still, you can configure these easily by clicking the Preferences button. You should probably check the Ports tab and ensure that there aren’t any issues with any other software on your computer. You can do this all within MAMP, as shown in Figure 1-21.

MAMP server

MAMP server

You can also click the PHP Preferences option and see a few things that. honestly, probably don’t matter much to you (see Figure 1-22). Just leave these alone. In fact. there’s almost never a reason to mess with these selections. Mostly, it’s good to know that yes, MAMP did indeed install PHP (along with a web server and MySQL)
with just a few mouse clicks

MAMP server

MAMP server

There’s not much else to do now, so you can close Preferences and click the “Open start page” option to get a nice browser page like the one shown in Figure 1-23. Here’s where you’ll spend lots of your troubleshooting time as well as digging into databases once you’ve mastered the command line tools for MySQL that you’ll learn about

Welcome to MAMP

Welcome to MAMP

Before moving on, you can verify that this is all doing what it should. At the top of the MAMP start page, click the “pnplnfo” tab. A screen appears, similar to that in Figure 1-24. What’s significant here isn’t all the information listed; you needn’t concern yourself with that just yet. What is cool, though, is that you’re looking at a PHP script that’s been interpreted by a PHP interpreter (installed as part of MAMP).

The interpreter then fed the output of that script to your new MAMP-installed web server, which in turn handed that response to your web browser. Proof that you’re already running PHP

php varsion 5.3.5

php varsion 5.3.5

Get Out Your Text Editor

All the programs you’re going to write in PHP are plain, old text files. Writing PHP isn’t a lot different than writing HTML or (55 or Java5cript. You’ll type different things, of course, but these are all just text files saved with a special extension. You use for HTML,.L for (55, .> for Java5cript, and now you’ll use ‘h” for PHP files.

Because PHP is just text, you’ll want a good text editor in which to work. If you’re in Windows, you can use Notepad. As simple as that program is, it’s perfect for coding in PHP. If you’re on a Mac, TextEdit is a great choice. The good news is that each ‘of these programs comes preinstalled on your computer, so you don’t have  to download or buy anything. The bad news is that none of these proqrarns «.«.

you’re writing PHP,so you don’t get much help if you type something wrong or want to organize your files without resorting to Windows Explorer or the Finder. These programs are simple, but limited.

On the other hand, there are quite a few editors out there that are built specifically to handle PHP. For instance, for Windows, you can download Nu5phere PhpED (“CiST) ‘,’re CC’fl”D. ciu([S”);’i,fJcf. “‘”Yj), which is shown in Figure 1-25. You’ll pay a bit for a program like Nu5phere-usually between $50 and $100-but you’ll get fancy

color coding ..help with special language features, and in a lot of cases, some nifty file organization features and the ability to upload your PHP directly to your web server

NuSphere PhpED

NuSphere PhpED

If you’re on a Mac, the two leading candidates for editors that do text plus lots of other cool things are BBEdit (www bdrebones.com. ‘orooucts/boea.: /;11oe >:: htli1i) and TextMate (,vww mecrotnstes com). Both are Mac-only programs, and both offer similar features on the Mac as does PhpED for Windows: color-coding, file management, help documentation, and support for HTML, CSS, JavaScript, and a lot more. You can see BBEdit in action in Figure 1-26; you’ll need to drop $100 to get your own copy, though.

BBEdit

BBEdit

You can see what TextMate looks like in Figure 1-27. It’s a little sjrnpler than BBEdit, so if you’ve never used a programming editor, this might be easier to begin with . Textt-tate costs around $60, slightly less than BBEdit.

Text Editors: Mashing Up Programs

Although prnqrarnstlke PhpED,BBEdit, and TextMate are billed as text editors, they’re actually lots of programs rolled into one. Imagine having a text editor, a file management tool like f/indows Explorer or Finder, a tel net or terminal program, an FTPclient, and some glue to hold them all together. That’s more or less what these programs give you: a bunch of things all rolled into one single software package.

What’s great about these “text editors plus” is that they offer you all sorts of features, and you don’t need five or six icons in your Mac’s Dock or shortcuts on your Windows desktop. You have access to almost everything you’ll typically need to build web pages or program in PHP,right at your fingertips.

What’s not so great, though, is that generalized tools aren’t often as fully featured as specific tools. In other words, a program that tries to do everything usually does lots of things decently, as opposed to lots of programs that only do one thing, but do that one thing really well.

Much of the time, you’re making a choice between convenience and features. If you only use FTPto upload files to a server on occasion, you almost never work with your computer’s command line, and you get a kick out of colored editors, the bundled text editors with lots of extra features migllt be a good fit.

Whether you use a more full-featured text editor or not, thoucn, at some point you might need to ditch the editor and use an actual FTPortelnet program. As long as you’re comfortable diving into those programs “iI,,!’u! the use of an editor from time to time, by all means, go forth in code in TextMate or PhpED without worry.

Once you’re comfortable writing PHP code, you can spend some time playing with all these different enhanced editors. You can see what you like, discover whether an editor is perfect for you, or realize you’re a Notepad or TextEdit programmer at heart. There’s no one right option for PHP; all of these choices work just fine.

If you’re just starting out, though, try to use a simple text editor-Note pad on Windows or lextEdit on the Mac. You’ll learn a lot more about PHP this way, even if you don’t get all the bells and whistles of one of the full-featured editors. Besides, once you understand PHP and have learned to work with it manually, you’ll appreciate , and be able to use the features of the other editors a lot more effectively

Posted on January 11, 2016 in PHP What Why and Where

Share the Story

Back to Top