Visual Studio 2010!

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Getting Started with PHP Programming
By Jeff Cogswell

Rate This Article: Add This Article To:

Getting Started with PHP Programming - PHP Syntax and Rules
( Page 3 of 5 )

Some PHP Syntax and Rules

Just as with other programming languages, in PHP, you can create variables and process them in different in different ways. With PHP, variable names are preceded with a dollar sign, such as $amount. The variable name itself (after the $) can start with a letter or underscore, and then followed by any sequence of letters, digits, or underscores. (That’s like most languages – variables can have letters, digits, and underscores, but can’t start with a digit.) And technically, the dollar sign isn’t part of the variable name.

Thus, $amount100 is a valid variable name, as is $_amount. But $1a is not valid because it starts with a digit.

Variables are assigned with a single equal sign followed by a value, like so:

$amount = 100;  // This assigns 100 to $amount
$name = "Angela"; // This assigns the string Angela to $name.

(Notice also that comments start with // and extend to the end of the line. You can also create comments that span multiple lines by starting them with /* and ending them with */.)

Variables are not strongly typed in the sense that once you define a variable, it doesn’t have to always hold the same type. However, types do exist in PHP. Here are the basic types:

  • integer
  • boolean
  • float
  • string

The previous two lines of code created an integer (100) and a string (Angela). Additionally, you can create arrays of these types, as well as objects. (I'll cover both of these in a future article.) There are also a couple of “special types” that you can use, which, again I'll cover in a future article.

String literals (such as Angela) can be in either single quotes or double quotes. Thus, the following two lines do the same thing as each other:

$name = "Angela";
$name = 'Angela';

However, there's a minor difference in the way PHP handles the two. Double-quoted strings can have escape sequences in them, like so:

$somestring = "Angela\tJeff"

where the \t will be replaced by a tab character. An escape sequence is a special set of codes that result in special characters in your string. The backslash followed by a t creates a tab. A backslash followed by an n creates a newline. A backslash followed by an r creates a carriage return.

Important note: Tabs and newlines and other whitespace have odd effects in PHP when printed out using the echo statement. The whitespace ends up in the resulting HTML, but HTML normally ignores whitespace and doesn't render it in the browser! So if you print out a word followed by a newline, followed by another word, you won't see the two words on separate lines in the browser. You'll only see the newline if you click View->Source in the browser. This can be frustrating, and so I want to warn you early. If you want a newline to appear in the browser, you have to print out an HTML <br /> tag (which creates a line break), like this:

<?php echo ("hello<br>there"); ?>

Back to escape sequences, when you create a string with single quotes, the backslash doesn't represent an escape. Thus, this string:

$somestring = 'Angela\tJeff'

will be exactly that: The string Angela followed by a backslash, then a t, then the string Jeff. That's the difference between quoting your strings with double quotes (escape sequences get replaced with their respective special characters) and single quotes (escape sequences don't get replaced).

Embedding Variables in Strings

One kind of cool feature (that some other scripting languages also have) is the way you can embed variables inside a string literal. Look carefully at this code:

<html>
<body>
<?php
    $a = 10;
    $name = "Angela";
    echo ("Hello $name, your amount is $a.");
?>
</body>
</html>

Look closely at the string inside the echo statement. The string uses double-quotes and has variable names embedded right in it ($name and $a). PHP will replace these variable names with their actual value to build the final string. Thus, the final output in the browser for this PHP file is:

Hello Angela, your amount is 10.

Note that this will only work with double-quotes. If you change the quotes to single quotes in this example, the variables won't be replaced (or “expanded” as the process is called). Instead, you'll see exactly what's in the string as-is.

PHP has some pretty sophisticated algorithms for figuring out where the variable names start and end. But sometimes it does get confused, especially if you have variable names that start with the same sequence of letters. If you want to be absolutely specific about your variable names, put curly braces around them like so:

echo ("Hello {$name}, your amount is {$a}.");

Dealing with Numbers

Integer and floating-point variables behave the way you might expect. I suggest taking a look at these two pages in the official PHP documentation to learn more about them:

http://www.php.net/manual/en/language.types.integer.php

http://www.php.net/manual/en/language.types.float.php



 
 
>>> More Microsoft Languages Articles          >>> More By Jeff Cogswell