LINK DOWNLOAD MIỄN PHÍ TÀI LIỆU "Tài liệu Geeting Good with PHP pdf": http://123doc.vn/document/1037131-tai-lieu-geeting-good-with-php-pdf.htm
Chapter 3 39
Control Structures 39
if and else (and elseif ) 39
for / foreach 41
return / break / continue 45
switch 47
require / include / require_once / include_once 49
Final Thought on Control Structures 52
PHP Internal Functions 53
String Functions 53
Breaking Up and Getting Together 54
A Case of Changed Case 55
Keeping Thing Trimmed 56
Replacements 56
How Long? 57
Needle in a Haystack 57
Et Cetera 58
Array Functions 58
Pushin’ and Poppin’ 58
Mappin’ and Walkin’ 60
Searching High and Low 61
Slicin’ and Dicin’ 62
Sorting Things Out 64
Counting Your Chickens (After They Hatch) 64
Summing it all Up 65
Date and Time Functions 65
parse_date 65
time 66
strftime 66
Math Functions 67
max / min 67
mt_rand 67
round / ceil / floor 68
JSON Functions 68
File Functions 69
fopen 69
Reading a File 70
Table of Contents6
Writing a File 72
fclose 72
The Oddities 73
Summary 73
Chapter 4 76
Scope 76
Superglobals 77
$_GET 79
$_POST 83
Persistence 85
Cookies 85
Sessions 89
Databases 91
Summary 103
Chapter 5 105
Keeping Things Safe 105
When Things Go Wrong 110
Errors 110
Warnings 111
Notices 112
Handling Errors 113
.htaccess 113
Frameworks 114
Deploying 115
Conclusion 116
Appendix A: What We Didn’t Cover 118
Appendix B: Further Resources 119
About the Author 120
Chapter 1
It’s more than fair to say that PHP is one of the mainstays of the
Internet. It’s been around for over a decade and a half, and in that
time it’s become the default rst foray into the world of server-side
coding for many. If you’re attempting to make that move now, I
hope this book will prove a worthy guide.
So, let’s go! Please keep your hands in the book or on your key-
board at all times; eating and drinking is permitted, but no ash
photography.
What is PHP?
Before we actually get started, I want to make sure you know what
you’re getting into. After all, it’d be a crying shame for you to read
two-thirds of the book before realizing that PHP isn’t what you
wanted to learn.
So, what is PHP? First off, the name PHP stands for “PHP: Hyper-
text Preprocessor.” Ignoring the mind-bending recursive part
(https://en.wikipedia.org/wiki/Recursive_acronym), this means that
PHP is primarily used for preprocessing hypertext. You’ll often
intermix PHP with HTML; the HTML isn’t processed until it gets to
the browser, but the PHP is executed on the server, and its output
(typically HTML or some other text) replaces the PHP code.
This tells us two things: rstly, PHP is a server-side language. None
of your PHP ever hits the browser — it’s processed on the server.
The other thing that might not be entirely obvious if you’ve just
worked with HTML and CSS previously is that PHP is a program-
ming language. It’s not like HTML and CSS at all: when you’re
writing PHP, you’re writing real code that will perform some task,
usually based on some input or variable conditions. Of course, this
could be just outputting some text or HTML, but often it’s more.
Chapter 19
Who is this Book For?
There’s no way that a single book could meet every single PHP
programmer wannabe where they are and help them learn the
ropes. And this book doesn’t need to do that, since there are
plenty of other books, websites, and tutorials that are top-notch.
Here’s who I imagine the audience of this book to be: it’s the
designer who wants to learn PHP so that he or she can use some
of the great PHP-based content management systems in their
client work. It’s the front-end developer who’s good with HTML,
CSS, jQuery, and maybe some raw JavaScript, and wants to start
building more dynamic websites from scratch. If you’re someone
who understands the front-end of the web pretty well, but you
wouldn’t really call yourself a “programmer,” then this book will, I
hope, be helpful to you.
So, yes, I’m aiming for beginners, but I’m also aiming for short: this
book is meant to be read in a weekend (okay, maybe a long week-
end). This means that there’s plenty of PHP goodness that I just
don’t have room to address. To make amends for this, I’ve included
two appendices. Appendix A is a list of topics that we didn’t dis-
cuss: it’s a good list of things to check out. Appendix B is a list of
resources to check out: blogs, books, and more.
Why Learn PHP?
Just in case you’re still on the fence about learning PHP at all, let’s
take a minute to talk about what you can do with it. The problem
here is that asking what can be done with PHP is like asking what
can be done with a paintbrush. My little sister can mess around
with one and do something pretty creative. But give one to Van
Gogh or Picasso, and, well, that’s a completely different story.
It’s the same with PHP. After reading this book, you should be able
to do some basic, yet really handy things that will improve your
websites. However, don’t forget that there are very popular libraries
Chapter 110
and frameworks that use PHP. Some of the biggest websites
you’ve ever visited are coded in PHP; ever heard of Facebook?
So, what will you be able to do with PHP? Check this out:
• You’ll be able to change values on your site based on user
input or other values (e.g. change the greeting based on the
time of day.)
• You’ll be able to use the information that a user enters into
a form, maybe by giving them appropriate content based on
that info (think search results) or by storing that information
within a database.
• You’ll be able to let your users upload les to your server.
• You’ll be able to build pages “on the y” by combining tem-
plates with content from a database, all right as the viewer
requests that specic page.
If any of these things sound enticing, good! And if they don’t,
maybe they’ve made you think of something else that you’ve
wanted to do with your websites. Whatever your aspirations may
be, there’s a pretty good chance you can achieve them with PHP.
One thing to note: PHP is a regular programming language, and
as such, it’s capable of more than just adding some punch to your
website. You could use it to write scripts and programs that have
nothing to do with the web and servers. This isn’t overly common,
but it can be done. However, the plan here is to stick to PHP in the
context of the web, deal?
How Do You Install PHP?
Still with me? Good. So, you’ve decided that you really do want
to learn PHP? Well, then, we’d better get it installed. Since PHP
is a server-side language, and the language doesn’t execute in
your browser, you need to install the PHP interpreter on your local
machine if you want to develop in PHP. While it’s a pretty similar
Chapter 111
process on both Windows and Mac OS X, I’ll walk you through
both. In both cases, there are great packages that bring all the
necessary pieces to the game and make it incredibly easy to start
playing.
Installing PHP on Windows
On a Windows computer, the best
way to get PHP onto your system is
by installing WAMP; besides PHP, this
package has Apache2 for a web server
and MySQL for databases. You prob-
ably aren’t familiar with these technolo-
gies but don’t worry; we’ll explore them
later.
We’ll begin by heading over to the
WAMP Homepage (http://wampserver.
com/en).
Click Start Experimenting WAMPServer. This will bring you to the
downloads section of the page. Choose the correct download,
depending on whether you’re running a 32- or 64-bit rig. A form will
You’ve probably figured
out what WAMP stands for,
right: Windows, Apache2,
MySQL and PHP.
Chapter 112
pop up, but you don’t have to ll it out, just click the link “download
it directly.” Once it’s downloaded, run that puppy.
It’s a pretty normal installation process; the only parts that might
trip you up are the last few steps. The installer will ask you to
choose your default browser; just browse to the right .exe le and
hit Open. Then, it will ask you to set some PHP mail parameters;
just leave the defaults. After you’re nished, WAMP Server should
launch automatically (if you haven’t unchecked that box). Hence-
forth, you’ll nd a Start WAMP Server item in your programs menu.
Once you choose that, you’ll see an icon in your task bar:
If you click on that icon, you’ll get a menu that looks like this:
See that Put Online option? Click that. In a second or so, your
servers will be online. Then, click the Localhost option, at the top of
that menu. This will open your browser to the WAMP start page.
To use WAMP, you’ll have to put your PHP code in the right place.
That right place is the www folder, which you’ll nd at C:\Progra m
Files\WAMP\www (you’ve got a shortcut to the www folder in the
Chapter 113
WAMP task bar menu). Any folders that you make in that directory
will show up as projects on the WAMP start page. Or, you can just
send your browser to http://localhost/YOUR_FOLDER_NAME to
see your work.
Installing PHP on Mac OS X
If you’re running Mac OS X, using MAMP is the best way to get up
and running with PHP. Load the MAMP homepage (http://www.
mamp.info/) and click the Download Now button under the grey
MAMP logo (you won’t need MAMP Pro today).
The WAMP start page.
Chapter 114
Once the rather large zip le has
nished downloading, open it up
and run the M A M P.p k g . Walk through
that installer; there shouldn’t be
any surprises. Once you’re done,
launch the MAMP app found at
/Applications/MAMP/MAMP.app within
your applications folder. You’ll get a
window that looks like this:
The servers will start up immediately, and a “Welcome to MAMP”
page should open. That’s it! You’ve installed MAMP and have a
working version of PHP on your Mac.
You might want to check out the MAMP start page; you can bring it
up in your default browser by clicking the Open start page button.
Yes, that’s right: MAMP
stands for Mac, Apache2,
MySQL and PHP.
Không có nhận xét nào:
Đăng nhận xét