HTML5 and CSS3 All-in-One For Dummies
Book image
Explore Book Buy On Amazon

A PHP variable can be named almost anything in HTML5 and CSS3 programming. 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 (at www.php.net) to find out whether your variable name is an illegal word.

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.

Concatenation

Concatenation is the process of joining smaller strings to form a larger string. 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 and addition:

image0.jpg
<?php
 //from helloVariable.php
 $output = "World!";
 print "<p>Hello " . $output . "</p>";
 print "<p>" . $output + 5 . "</p>";
?>

The previous code takes the variable output with the value World and concatenates it to Hello when printed. Next, it adds the variable output to the number 5. When PHP sees the plus sign, it interprets the values on either side of it as numbers.

Because output has no logical numerical value, PHP assigns it the value of 0, which it adds to 5, resulting in the output of

5

being sent to the browser.

Interpolating variables into text

If you have a bunch of text to print with variables thrown in, it can get a little tedious to use concatenation to add in the variables. Luckily, you don't have to!

With PHP, you can include the variables as follows:

image1.jpg
<!DOCTYPE html>
<html lang = "en-US">
<head>
 <meta charset = "UTF-8" />
 <title>helloInterpolation</title>
</head>
<body>
<?php
 $firstName = "John";
 $lastName = "Doe";
 print "<p>Hello $firstName $lastName!</p>";
?>
</body>
</html>

This process is called interpolation. Because all PHP variables begin with dollar signs, you can freely put variables right inside your string values, and when PHP sees a variable, it will automatically replace that variable with its value.

Interpolation works only with double-quoted strings because double quotes indicate PHP should process the string before passing it to the user.

About This Article

This article is from the book:

About the book author:

Andy Harris taught himself programming because it was fun. Today he teaches computer science, game development, and web programming at the university level; is a technology consultant for the state of Indiana; has helped people with disabilities to form their own web development companies; and works with families who wish to teach computing at home.

This article can be found in the category: