Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [198]

By Root 1517 0
why they are rarely used in PHP programming.

echo or print?

echo is another way to generate your code for the browser. In almost all circumstances, you use echo exactly like you use print. Everyone knows what print does, but echo sounds like I should be making some sort of dolphin noise.

The difference is that print returns a value, and echo doesn’t. print can be used as part of a complex expression, and echo can’t. It really just comes down to the fact that print is more dynamic, whereas echo is slightly (and I’m talking very slightly here) faster.

I prefer print because there’s nothing that echo can do that print can’t.

To see a more detailed discussion, go here: www.faqts.com/knowledge_base/view.

phtml/aid/1/fid/40.


Working with Variables PHP-Style

Variables are extremely important in any programming language and no less so in PHP.

A variable in PHP always begins with a $.

A PHP variable can be named almost anything. There are some reserved words that you can’t name a variable (like print, which already has a meaning in PHP), so if your program isn’t working and you can’t figure out why, try changing some variable names or looking at the reserved words list (in the online help at http://www.php.net) to find out whether your variable name is one of these illegal words.

PHP is very forgiving about the type of data in a variable. When you create a variable, you simply put content in it. PHP automatically makes the variable whatever type it needs. This is called loose typing. The same variable can hold numeric data, text, or other more complicated kinds of data. PHP determines the type of data in a variable on the fly by examining the context.

Even though PHP is cavalier about data types, it’s important to understand that data is still stored in one of several standard formats based on its type. PHP supports several forms of integers and floating-point numbers. PHP also has great support for text data. Programmers usually don’t say “text,” but call text data string data. This is because the internal data representation of text reminded the early programmers of beads on a string. You rarely have to worry about what type of information you’re using in PHP, but you do need to know that PHP is quietly converting data into formats that it can use.

Escape sequences

In the first section of this chapter, “Creating Your First PHP Program,” you see that you can escape double quotation marks with a back-slash. Quotation marks aren’t the only thing you can escape, though. You can give a whole host of other special escape directives to PHP.

The most common ones are

\t: Creates a tab in the resulting HTML

\n: Creates a new line in the resulting HTML

\$: Creates a dollar sign in the resulting HTML

\”: Creates a double quote in the resulting HTML

\’: Creates a single quote in the resulting HTML

\\: Creates a backslash in the resulting HTML

PHP can take care of this for you automatically if you’re receiving these values from a form. To read more, go here: http://us3.php.net/types.string.


Concatenation

Concatenation is the process of joining smaller strings to form a larger string. (See Book IV, Chapter 1 for a description of concatenation as it’s applied in JavaScript.) PHP uses the period (.) symbol to concatenate two string values. The following example code returns the phrase oogieboogie:

$word = “oogie “;

$dance = “boogie”;

Print $word . $dance

If you already know some JavaScript or another language, most of the ideas transfer, but details can trip you up. JavaScript uses the + sign for concatenation, and PHP uses the period. These are annoying details, but with practice, you’ll be able to keep it straight.

When PHP sees a period, it treats the values on either side of the period as strings (text) and concatenates (joins) them. If PHP sees a plus sign, it treats the values on either side of the plus sign as numbers and attempts to perform mathematical addition on them. The operation helps PHP figure out what type of data it’s working with.

The following program illustrates the difference between concatenation

Return Main Page Previous Page Next Page

®Online Book Reader