Friday 19 July 2013

Trying Out a Little PHP

9. Type </body> to close the <body> HTML tag from Step 5 and then
press Enter.

This tells the Web browser that you’re done with the body of the Web
page.

10. Type </html> to close the <html> tag from Step 1 and then press Enter.

This tells the Web browser that you’re at the end of the HTML
document.

When you’re done with Steps 1-10, double-check that the code in your text editor looks like this:

Book II

<html>

<head>

<title>This is a Simple PHP Page</title>
</head>

<body>

<?php echo ‘<p>Testing my new PHP function</p>’; ?>
</body>

</html>

Trying Out a Little PHP

6. Type <?php to tell the Web browser to start a PHP function and then
press Enter.

See the preceding section on starting and stopping PHP functions.

7. Type echo ‘<p>Testing my new PHP function</p>’; and then press Enter.

This is the function that you want PHP to execute on your Web page.

This particular function echoes the text “Testing my new PHP function” and displays it on your Web site.

8. Type ?> to tell the Web browser to end the PHP function and then
press Enter.

Trying Out a Little PHP

 

 

3. Type <title>This is a Simple PHP Page</title> and then press Enter.

The <title> HTML tag tells the browser to display the text between two tags as the title of the document in the browser title bar. (Note: All HTML tags need to be opened and then closed, just like PHP tags that we describe in the preceding section. In this case the <title> tag opens the command, and the </title> tag closes it and tells the Web browser that you’re finished dealing with the title).

4. Type </head> to close the <head> tag from Step 2 and then press
Enter.

5. Type <body> to define the body of the Web page and then press Enter.

Anything that appears after this tag displays in the Web browser
window.

Trying Out a Little PHP

 

To test some PHP code, follow these steps to create a simple HTML Web page with an embedded PHP function:

 

1. Open a new, blank file in your default text editor — Notepad

(Windows) or TextMate (Mac) — type <html>, and then press Enter.

The <html> tag tells the Web browser that this is an HTML document and should be read as a Web page.

2. Type <head> and then press Enter.

The <head> HTML tag contains elements that tell the Web browser

about the document; this information is read by the browser but hidden from the Web page visitor.

Exploring PHP Basics

In this case, get_info() represents the tag function, which grabs informa-
tion from the database to deliver it to your blog. The information retrieved
depends on what tag function appears between the two PHP commands.

Every PHP command you start requires a stop command. For every <?php,
you must include the closing ?> command somewhere later in the code.
PHP commands structured improperly cause ugly errors on your site, and
they’ve been known to send programmers, developers, and hosting provid-
ers into loud screaming fits. You find a lot of starting and stopping of PHP
throughout the WordPress templates and functions. The process seems as
though it would be resource intensive, if not exhaustive, but it really isn’t.

Always, always make sure that the PHP start and stop commands are sepa-
rated from the function with a single space. You must have a space after

<?php and a space before ?> — if not, the PHP function code doesn’t work. So make sure that the code looks like this: <?php get_info(); ?> — not like this: <?phpget_info();?>

Exploring PHP Basics



As we state earlier, WordPress is based in PHP (a scripting language for

creating Web pages) and uses PHP commands to pull information from the


 

MySQL database. Every tag begins with the function to start PHP and ends
with a function to stop it. In the middle of those two commands lives the

request to the database that tells WordPress to grab the data and display it.

 

A typical template tag, or function, looks like this:

 

<?php get_info(); ?>

This example tells WordPress to do three things:
✦ Start PHP: <?php

✦ Use PHP to get information from the MySQL database and deliver it to
your blog: get_info();

✦ Stop PHP: ?>

Exploring PHP Basics



Before you play around with template tags (covered in Book VI) in your


WordPress templates or plugin functions, you need to understand what
makes up a template tag and why, as well as the correct syntax, or func-
tion, for a template tag as it relates to PHP. Additionally, have a look at the
WordPress files contained within the download files. Many of the files end
with the .php file extension — an extension required for PHP files, which
separates them from other file types, such as JavaScript (.js) or CSS

(.css).

 

Exploring PHP Basics

WordPress requires PHP in order to work; therefore, your Web hosting

provider must have PHP enabled on your Web server. If you already have
WordPress up and running on your Web site, you know PHP is running and
working just fine. Currently, the PHP version required for WordPress is ver-
sion 4.3 or later.

In the very near future, WordPress will drop support for PHP version 4.
When this happens, version 5 will be the only version supported by the
WordPress platform. Ask your Web host to upgrade you to PHP version 5
sooner rather than later because most WordPress developers (for plugins,
themes, and so on) base their development practices on PHP version 5.

Exploring PHP Basics

Here content refers to the data stored in the MySQL database; that is, your
blog posts, pages, comments, links, and options that you set up in the
WordPress Dashboard. However, the theme (or design) you choose to use
for your blog — whether it’s the default theme, one you create, or one you
have custom designed — isn’t part of the content in this case. Theme files
are part of the file system and aren’t stored in the database. Therefore, it’s
a good idea to create and keep a backup of any theme files that you’re cur-
rently using. See Book VI for further information on WordPress theme
management.

When you look for a hosting service, keep an eye out for the hosts that pro-
vide daily backups of your site so that your content (data) won’t be lost in
case something happens. Web hosting providers who offer daily backups as
part of their services can save the day by restoring your site to its original
form. Additionally, Book II, Chapter 5 covers important information about
backing up your Web site.

 

Exploring PHP Basics

PHP is a server-side scripting language for creating dynamic Web pages.

When a visitor opens a page built in PHP, the server processes the PHP com-
mands and then sends the results to the visitor’s browser. MySQL is an open
source relational database management system (RDBMS) that uses
Structured Query Language (SQL), the most popular language for adding,
accessing, and processing data in a database. If that all sounds like Greek to
you, just think of MySQL as a big file cabinet where all the content on your
blog is stored.

Every time a visitor goes to your blog to read your content, he makes

a request that’s sent to a host server. The PHP programming language

receives that request, makes a call to the MySQL database, obtains the

requested information from the database, and then presents the requested information to your visitor through his Web browser.

Understanding How PHP and MySQL Work Together

 

WordPress uses a PHP/MySQL platform, which provides everything you need to create your own blog and publish your own content dynamically, without knowing how to program those pages. In short, all your content is stored in a MySQL database in your hosting account.

Introducing PHP and MySQL



 

In Book VI, you dig into the code necessary to create functions and
features on your Web site. Many, if not all, of these functions and fea-


 

tures use Hypertext Preprocessor (PHP) tags. When combined with the

WordPress code, these tags make things happen (such as displaying post content, categories, archives, links, and more) on your Web site.

One of the reasons WordPress is the most popular content management
system (CMS) is that you don’t need to know PHP code to use it. That’s to
say, you can use WordPress easily without ever looking at any of the code
or template files contained within it. However, if you want to tweak the set-
tings of your WordPress theme (flip to Book VI) or the code of a particular
plugin (see Book VII), you need to understand some basics of how PHP

works. But don’t worry; you don’t need to be a PHP programmer.

 

This chapter introduces you to the very basics of PHP and MySQL, which is
the database system that stores your WordPress data. After you read this
chapter, you’ll understand how PHP and MySQL work together with the
WordPress platform to serve up your Web site in visitors’ browsers.

 

This book doesn’t turn you into a PHP programmer or MySQL database

administrator, but it gives you a glimpse of how PHP and MySQL work

together to help WordPress build your Web site. If you’re interested in finding out how to program PHP or become a MySQL database administrator, check out PHP and MySQL For Dummies by Janet Valade (Wiley).

Finding and Using Free and Easy FTP Programs

Here are some quick and easy steps for using your FTP program to CHMOD a file, or edit its permissions on your Web server:

1. Connect the FTP client to your Web server.

2. Locate the file you want to CHMOD.

3. Open the file attributes for the file.

Right-click the file on your Web server and choose File Permissions. (Your FTP client, if not FileZilla, may use different terminology.)

4. The Change File Attributes window appears, as shown in Figure 2-6.

5. Type the correct file permissions number in the Numeric Value field.

This is the number assigned to the permissions you want to give the file.
Most often, the plugin or theme developer provides you which permis-
sions number to assign to the file or folder; typically, it will be either 644
or 755. (The permissions in Figure 2-6 are assigned the value of 755.)

6. Click OK to save the file.

Finding and Using Free and Easy FTP Programs



Typically, folders and files within your Web server are assigned permissions




of either 644 or 755. Usually, you’ll see PHP files, or files that end with the
.php extension, with permissions set to 644 if the Web server is configured
to use PHP Safe Mode.

This is a very basic look at file permissions because, usually, you will not
need to mess with file permissions on your Web server. In case you do
need to dig further, you can find a great reference on file permissions from
Elated.com at http://www.elated.com/articles/understanding-
permissions.


You may run across a situation where you’re asked to edit and change the

file permissions on a particular file on your Web server. With WordPress

sites, this usually happens when dealing with plugins or theme files that
require files or folders to be writable by the Web server. This practice is
referred to as CHMOD, an acronym for Change Mode. When someone says,
“You need to CHMOD that file to 755,” you’ll know what they are talking
about.

Changing File Permissions

Each set of permissions has a numeric code assigned to it, identifying what
type of permissions are assigned to that file or folder. There are a lot of
them, so here are the most common ones that you run into when running a
WordPress Web site:

✦ 644: Files with permissions set to 644 are readable by everyone and
writable only by the file/folder owner.

✦ 755: Files with permissions set to 755 are readable and executable by
everyone, but only writable by the file/folder owner.

✦ 777: Files with permissions set to 777 are readable, writable, and execut-
able by everyone. For security reasons, you should not use this set of
permissions on your Web server unless absolutely necessary.