Pages

Monday, May 13, 2013

PHP Syntax: Weird and Wonderful:

You’ve got a couple of PHP programs running, and one of them even works
with an HTML form. But so far, you’ve just been typing code. Even though
you’ve just gotten started with PHP, you’re ready to dig deeper, and start to
understand what’s going on in that code. In this chapter, you’re going to get comfortable
with a lot of the PHP syntax: that means learning what special words you
type into your programs, and what each one of those special words—usually called
keywords—tells PHP to do.
Fortunately, this learning doesn’t mean you can’t still build interesting programs that
run in a web browser. In fact, since almost everything that’s done with PHP involves
Web pages, all your scripts in this chapter will accept information from a Web form
and work with that information. So you’re not just learning PHP; you’re learning to
write Web applications.

Get Information from a Web Form 

In sayHelloWeb.php, you used this line to get the value of a variable called “name”
from the sayHello.html web form:
echo $_REQUEST['name'];
You may remember that $_REQUEST is a special PHP variable that lets you
get information from a web request. You used it to get one particular piece of
information—the user’s name—but it can do a lot more.

Access Request Parameters Directly

 

In fact, to see just how handy $_REQUEST really is, open up your text editor. Type
the code below, in which a visitor enters her name and several other important bits of
contact information, like her Twitter handle, Facebook page URL, and email address.
<html>
<head>
<link href="../css/phpMM.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id="example">Example 2-1</div>
<div id="content">
<h1>Join the Missing Manual (Digital) Social Club</h1>
<p>Please enter your online connections below:</p>
<form action="scripts/getFormInfo.php" method="POST">
<fieldset>
<label for="first_name">First Name:</label>
<input type="text" name="first_name" size="20" /><br />
<label for="last_name">Last Name:</label>
<input type="text" name="last_name" size="20" /><br />
<label for="email">E-Mail Address:</label>
<input type="text" name="email" size="50" /><br />
<label for="facebook_url">Facebook URL:</label>
<input type="text" name="facebook_url" size="50" /><br />
<label for="twitter_handle">Twitter Handle:</label>
<input type="text" name="twitter_handle" size="20" /><br />
</fieldset>
<br />
<fieldset class="center">
<input type="submit" value="Join the Club" />
<input type="reset" value="Clear and Restart" />
</fieldset>
</form>
</div>
<div id="footer"></div>
</body>
</html>

GOOGLE FUN

Google is very nice...... You Know here?
Click This link: http://www.google.com.hk/intl/zh-CN/landing/shuixia/



Sunday, May 12, 2013

Upload Your HTML, CSS, and PHP:

When you’re running a PHP program on your own machine, using the command
line, as soon as you’ve saved your PHP you can run it. But when you’re working with
web pages and web applications, things are a bit trickier.
When you’re building a web page, you have to upload your HTML, CSS, and any
JavaScript you’ve written to your own web server. Then, you access those files with
a browser, through a web address like www.yellowtagmedia.com/sayHello.html.
Typing that web address into your browser causes your server to supply your HTML
to whatever web browser requested the page.
PHP works the same way. Once you’ve written your PHP programs, you upload
them onto your web server with your HTML and CSS. Typically, you’ll end up with
files and directories like the following:
• Root or home directory (/). Your web root, where you put all your HTML. This
usually is the location referenced by a URL like yellowtagmedia.com/, without
any specific file after the web server name.
• CSS directory (css/). The directory where all your site’s CSS is stored.
• JavaScript directory (js/). Your JavaScript files go here. You’ll often see this
directory also called scripts/, but since PHP programs are also called scripts,
it’s a good idea to be more explicit in your naming.
• PHP directory (scripts/). Here’s where you put all your PHP programs. Again,
you could call this something more specific like php/ or phpScripts/, but more
often than not, websites use scripts/ for this directory, so following that lead
is a good habit to get into.
• Examples directory (ch01/, ch02/, and so forth). As you’re working through
the examples, you’re going to end up with a lot of PHP programs, and fast.
To keep everything organized, you should have a separate directory for each
chapter. So when you upload sayHello.html and sayHelloWeb.php, upload them
into ch01/sayHello.html and ch01/scripts/sayHelloWeb.php.

Write a PHP Script:

Now that you’ve got an HTML page sending information to sayHelloWeb.php, you
need to actually write that code. When you write PHP to run on the Web, it’s not
much different from the program you’ve already written (page 14). You have to get
information a little differently, because there’s no command line that a user can type
into. But other than that, things stay pretty much the same.
Open up a new text editor and type the PHP code shown here; it should look sort
of like an HTML-ized version of the sayHello.php program:
<html>
<head>
<link href="../../css/phpMM.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id="example">Example 1-1</div>
<div id="content">
<h1>Hello, <?php echo $_REQUEST['name']; ?></h1>
<p>Great to meet you. Welcome to the beginning of your PHP programming
odyssey.</p>
</form>
</div>
<div id="footer"></div>
</body>
</html>
Save this program as sayHelloWeb.php, and be sure you’ve got your file in plain
text with the right extension.
The first thing you probably noticed here is that this file looks a whole lot like HTML.
In fact, compared to sayHello.php, the first PHP program you wrote, this version
might look like a style of programming that’s a lot easier to learn. That’s because
once you’re using PHP to work and interact with web pages, a lot of what your PHP
programs will do is insert data into existing web pages, which means you’ll be working
with HTML a lot. Of course, that’s great news, because you already know HTML,
so you’ll be adding to what you know, rather than learning something completely
new from scratch.
Once you realize that a lot of this program is just HTML, you can probably already
guess what a lot of this program does. Here’s a section-by-section breakdown:
• The page starts out with a normal html element and head section.
• The body section begins, and sets up the page heading and example number,
just like the regular HTML page, sayHello.html.
• The page defines a heading with h1, and prints “Hello,.“
• The <?php tells the browser some PHP code is coming. Then, then $_REQUEST
variable is accessed, and a property called name within that variable is printed
using echo.
• The end of the PHP code is indicated with ?>.
• The rest of the HTML is output, just as in sayHello.html.
This program, like most PHP programs you’ll write, accepts its input from a web
page, either from one built in HTML like the pages you’ve created before, or from
another PHP program. It’s the job of that web page—sayHello.html in your case—to
get the user to enter her information, and then send that information on to this
program. The information from that HTML page is stored in $_REQUEST, which is a special variable in PHP.

Writing Your Second Program:

Start with an HTML Page


To get started, open up a new document in your text editor or favorite HTML editor,
and create this HTML page:
<html>
<head>
<link href="../css/phpMM.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="header"><h1>PHP & MySQL: The Missing Manual</h1></div>
<div id="example">Example 1-1</div>
<div id="content">
<h1>Welcome!</h1>
<p>Hello there. So I hear you're learning to be a PHP programmer!</p>
<p>Why don't you type in your name for me:</p>
<form action="scripts/sayHelloWeb.php" method="POST">
<p>
<i>Enter your name:</i> <input type="text" name="name" size="20" />
</p>
<p><input type="submit" value="Say Hello" /></p>
</form>
</div>
<div id="footer"></div>
</body>
</html>

Website Programing: This program does just a few simple things:

Website Programing: This program does just a few simple things:

This program does just a few simple things:

1. Identifies itself as PHP using <?php
2. Prints out a welcome message using the echo command
3. Asks the visitor for his name, again using echo
4. Gets the visitor’s name and stores it in something called $name
5. Says hello by printing out a message followed by what’s in $name
6. Finishes up with the ?> characters
It’s okay if a lot of this doesn’t make sense, but you probably already could have
figured out a lot of this, except maybe the weird line beginning with $name =. There
are also some strange characters, like \ns and STDIN, that you’ll learn about soon.
Just see if you can follow the basic path of things: the opening <?php, the printing,
the request for the user’s name, another bit of printing, and the closing ?>.
Now save this program. Name it sayHello.php, and make sure you add that .php
extension. Otherwise you’ll have problems down the line. Save the file some place
handy, like on your desktop, in your home directory, or in a folder you’re using to
keep all your PHP programs in as you’re learning.

That’s it; you’ve written your first PHP program!

Writing Your First Program....

You’ve got PHP; you’ve got a text editor. Now all you need is a PHP program, which
you’ll create in the next few minutes. Open up your text editor, and type the following
code, exactly as shown:


<?php
echo "Hello there. So I hear you're learning to be a PHP programmer!\n";
echo "Why don't you type in your name for me:\n";
$name = trim(fgets(STDIN));
echo "\nThanks, " . $name . ", it's really nice to meet you.\n\n";
?>


A lot of this code may look weird to you, and that’s OK. You’ll understand every bit
of it soon. Right now, just get used to looking at PHP, which is quite a bit different
from HTML or JavaScript.

WARNING: 
Some of the editors you might use, like TextEdit, automatically create rich text documents.
Rich text lets you use formatting, like bolding and underlining. You don’t want that in your PHP code, so look for
the option to use plain text, which doesn’t allow formatting.
If you’re using TextEdit, choose Format➝Make Plain Text. You won’t have that option if you’re already typing in
plain text. If you’re using Notepad, rich text isn’t an option, so you’ve got nothing to worry about.

PHP: What, Why, and Where?

PHP is ultimately text, taken by your web server and turned into a set 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
get familiar with PHP itself—and the best way to do that is to install PHP on your own
computer, even though most of your programs will run on a web server.
Then, you need to run an actual script. Don’t worry; it’s amazingly easy to write your
first program in PHP, and you’ll end up writing more than just one program before
you hit Chapter 2.
And through it all? You’ll begin taking control. PHP gives you the ability to be an
active participant in your web pages. It lets you listen carefully to your users and
say something back. So get going; no reason to leave you users with passive HTML
pages any longer.

Gathering Your Tools: You’ll need to take just a few steps before you can start with PHP. You can’t build a
website without a web browser, and you can’t write PHP without a few tools. But it
won’t take long before you’ve got your computer set up with your own customized
PHP programming environment.
Although PHP isn’t pre-loaded on every computer like web browsers are, you can
easily download PHP from the Internet, get it working on your computer, and get up
and running fast…all without spending a dime. On top of that, most of the easiestand best tools for writing PHP code are also free. All you need is your own copy of
the PHP language on your computer, plus a plain old text editor. This section shows
you where to find them.


PHP on the PC: PCs come with a lot of software pre-installed. Unfortunately, one program that most
PCs don’t come with is PHP. That’s okay though: you can get PHP up and running
in just a few minutes, as long as you have an Internet connection.
Note If.Open up your favorite Web browser and head to www.php.net. This site is PHP’s
online home, and it’s where you’ll download your own version of the PHP language,
along with all the tools you need to write and run PHP programs. Look along the
right side of the PHP home page for the Stable Releases heading; you can see it on
the right of Figure 1-1.

Figure 1-1

Once you’ve chosen a PHP version link, you’ll see a screen like Figure 1-2, with links
for the current version of PHP as well as at least one older version (which will have
a lower version number than the most current version).
Before you download PHP, though, take a look further down the page. There’s
a heading titled Windows Binaries, and that’s your ticket to getting PHP up and
running fast on your Windows machine. Clicking this link takes you to another site,
http://windows.php.net/download, which should look something like Figure 1-3.


 Take Control of Your PHP Installation
Like most of the programs on your computer, the PHP software
package (which includes the php program you’ve been running)
is updated fairly often. Most of the time, if you’re keeping
your computer updated with Apple’s Software Update, you
don’t have to worry about updating PHP separately. But if you
want to see what version of PHP you’re actually running, you
can type php –version into your Terminal window. You’ll get
back something like this:
Bretts-MacBook-Pro:~ bdm0509$ php -version
PHP 5.3.4 (cli) (built: Dec 15 2010
12:15:07)
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-
2010 Zend Technologies
Look at the very first line that PHP spits out; it tells you you’re
running version 5.3.4. (See the box on page 5 for more detail
on how version numbers work.)
If you want to get the very latest version of PHP, you can visit
www.php.net and download the PHP source code. That’s a
little trickier than just using the version preinstalled on your
Mac, though, so unless you’re really into commands like unzip
and tar, you can stick with what’s already on your machine.
By the way, if you’re not using your Mac’s Software Update
frequently, you may want to do so now. It’ll keep your software
current, without all the hassle of downloading programs on
your own. Just choose •➝Software Update to find and install
new software that’s available for your Mac. And if your software
is up to date, the dialog box lets you know.

What Is MySQL?

MySQL is a database. It stores your information, your users’ information, and anything
else you want to stuff into it. There’s actually a lot more nuance to MySQL—and
SQL, the language in which you’ll interact with MySQL (but better to save that for
Chapter 3—when you’ve got a little PHP and context under your belt).
For now, think of MySQL as a warehouse where you can store things to be looked
up later. Not only that, MySQL provides you a really fast little imp that runs around
finding all that stuff you stuck in the warehouse whenever it’s needed. By the time
you’re through this this book, you’ll love that imp…er…MySQL. It’ll do work that you
could never do on your own, and it’ll do that work tirelessly and quickly.

What Is PHP?


PHP is a programming language. It’s like JavaScript in that you spend 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 why it’s
called PHP, see the box below.)

Introduction

You’ve built a web page in HTML. You’ve even styled it with Cascading Style
Sheets (CSS) and written a little JavaScript to validate your custom-built web
forms. But that wasn’t enough, so you learned a lot more JavaScript, threw in
some jQuery, and constructed a whole lot of web pages. 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.
Maybe 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’s 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 all these are the case!—then
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.