After writing my article Making BellaBuffs Work Better for You, I realized while coding my own fanlisting (TWO ON TWO) that, while I am experienced in programming, not everyone who maintains fanlistings are. How would, for example, a PHP newbie know they can’t include quotation marks in their customized text? How could someone who doesn’t have five programming manuals on their bookshelf realize simply trying to include a link in their introduction would cause the script to break? How can such things be achieved painlessly? Well, it requires a bit of specialized knowledge. And so, I welcome you to Part II of Making BellaBuffs Work Better for You


The Basics
First, let’s take a look at your basic variable/definition line of PHP code.

$variable = "value";

The first symbol is a dollar sign; it indicates the text directly attached to it is a variable. Variable names may contain upper- and lowercase alphanumeric characters (a-z, A-Z, 0-9) and the underscore (_). Variables cannot be started with a number, and while it is legal to start variables with an underscore, it’s not a recommended practice because there are predefined variables in PHP that use it; you might accidently mess something up by redefining one of these variables. Do remember that variables are case-sensitive, so $Variable is different from $variable. It’s also good convention to keep your variables brief and concise — concise so you always know what they mean, and brief so you have less opportunities to misspell them!

As you saw in Part I of this tutorial, in BellaBuffs, you call variables from the file prefs.php to your other pages so they will be displayed when the browser loads them. Well, for anything to be displayed, you must assign the variable with a value. The assignment operator is the equal sign (=). If your value is NOT a number, then you must put either single or double quotation marks around the text. (I recommend using double quotation marks simply because it is the convention followed by Jem in her script.) These quotation marks indicate the value of the variable in question is a string… which means it is a textual rather than numerical value. However, if your value is a number, you do not put quotation marks (“”) around it.

The semicolon signals the end of each line. It’s like the closing tags in HTML or the semicolon in CSS. Don’t ever forget it, or there’ll be a nasty error.

The Problem of the Quotation Mark
If writing under the grammar rules of British English, it is quite possible fanlisting owners would not run into this problem. However, Americans using this tutorial who do not have nit-picky programming knowledge will be very frustrated. So, the million dollar (pound?) question is… how should quotations be formatted?

‘Should they be in single quotation marks?’

“…or should they be surrounded by double quotes?”

Remember how I said strings must be encased by either single or double quotation marks? Well… what happens if the introductory text calls for a quotation? If following Jem’s convention of using double quotation marks to surround strings AND going by American English grammar standards, the line in PHP will look something like this:

$welcome = "Welcome to <strong>Higbee's Department Store</strong>, the fanlisting for fans of the best Christmas movie ever, <em>A Christmas Story</em>... and those who say, "You'll shoot your eye out, kid!" to everyone throughout the holiday season.";

A PHP novice might not recognize the problem at hand, but a more fastidious programmer would recognize it in a heartbeat. Here’s the thing… if a quotation mark is inserted somewhere in a string of text, the browser reading the file is going to think that is the end of the string, rather than considering it part of the string. So, this is what the browser is going to try to output:

$welcome = "Welcome to <strong>Higbee's Department Store</strong>, the fanlisting for fans the best Christmas movie ever, <em>A Christmas Story</em>... and those who say, "

Of course, because there is no semicolon after that quotation mark, this will simply land the reader with an error on their screen.

Now, quick fix says simply use single quotation marks to denote strings if quotations need to be included… but what happens if there is a contraction in a string?

$welcome = 'Welcome to <strong>Higbee'

The above is what the browser is going to try to output. But, once again, notice the lack of a semicolon. Alas, the viewer is once more secured with the same problem he/she had to start with.

…So what’s the fix?
In a phrase? A character code. While it’s possible to simply enter a character code for each apostrophe used and surround your strings with single quotation marks, I believe it more practical to insert a character code for each double quotation mark used within the string, because I think apostrophes are more likely to be included in text than quotes. The character code for double quotation marks is &quot;. So, your PHP code will look like this:

$welcome = "Welcome to <strong>Higbee's Department Store</strong>, the fanlisting for fans the best Christmas movie ever, <em>A Christmas Story</em>... and those who say, &quot;You'll shoot your eye out, kid!&quot; to everyone throughout the holiday season.";

Which will render in a browser like so:

Welcome to Higbee’s Department Store, the fanlisting for fans the best Christmas movie ever, A Christmas Story… and those who say, “You’ll shoot your eye out, kid!” to everyone throughout the holiday season.

…which is exactly the desired result!

Links, Images, and other HTML tags with required attributes…
Obviously, double quotation marks are used to surround values of attributes in HTML. Probably the most common HTML tag that would be used in our customized text is the anchor tag:

<a href="http://www.greydove.org">greydove.org</a>

This will break the script the same way a quotation mark for a quote would. It’s a bit befuddling when first considered, but it simply requires the same fix as above. Simply use the character code &quot;. Thus, your line of PHP code will look like this:

$jointxt = "Not afraid of shooting your eye out? Well, why don't you <a href=&quot;join.php&quot;>join</a>?";

And it will be rendered as so:

Not afraid of shooting your eye out? Well, why don’t you join?

This works because 1. PHP doesn’t read &quot; as a quotation mark; 2. When the variable is called from prefs.php and into the file the browser is trying to render, the browser begins reading it as text and HTML… this is why your tags, such as <em>, work; and 3. The browser reads &quot; as a quotation mark, so the anchor tag is seamlessly integrated into the page without confusion.

The quotation mark issue, to me, would be the most pressing problem a PHP newbie would have to tackle, so I thought it necessary to follow up with this article. But, this isn’t all there is; what about those anal coders (like myself…) who like their HTML to be perfectly tabbed? Anyone can view the source of greydove.org and see that it’s practically a work of art in structure, if I do say so myself.

…and I know I can’t be the only one who writes their markup like this.

So stick around, everyone, and keep an eye out for Part Trois of Making BellaBuffs work better for you… how to incorporate tabbed and structured markup.

Links:



2 Responses to “Making BellaBuffs work better for you… Part II”  

  1. The other option would be to escape the quotation marks with a backslash; I don’t know if I can demonstrate that in WP comments without it stripping it all out though.

    In fact, just ignore me :|


  1. 1 Making BellaBuffs work better for you « Chicken Scratches

Leave a Reply