sitepoint_day3_php variables, operators, and comments

1
About the Author Kevin Yank Kevin began developing for the Web in 1995 and is a highly respected technical author. He wrote Build your own Database Driven Website using PHP and MySQL, a practical step-by-step guide published by SitePoint, and he's co-author of the SitePoint Tech Times, a bi-weekly newsletter for technically-minded web developers. Kev believes that any good webmaster should have seen at least one episode of MacGyver. View all articles by Kevin Yank... By: Kevin Yank June 16th, 2010 Reader Rating: 9 Variables in PHP are identical to variables in most other programming languages. For the uninitiated, a variable can be thought of as a name that’s given to an imaginary box into which any literal value may be placed. The following statement creates a variable called $testvariable (all variable names in PHP begin with a dollar sign) and assigns it a literal value of 3: $testvariable = 3; PHP is a loosely typed language. This means that a single variable may contain any type of data, be it a number, a string of text, or some other kind of value, and may change types over its lifetime. So the following statement, if you were to type it after the statement above, assigns a new value to the existing $testvariable. In the process, the variable changes type: where it used to contain a number, it now contains a string of text: $testvariable = 'Three'; The equals sign we used in the last two statements is called the assignment operator, as it’s used to assign values to variables. Other operators may be used to perform various mathematical operations on values: $testvariable = 1 + 1; // Assigns a value of 2 $testvariable = 1 - 1; // Assigns a value of 0 $testvariable = 2 * 2; // Assigns a value of 4 $testvariable = 2 / 2; // Assigns a value of 1 From the above examples, you can probably tell that + is the addition operator, - is the subtraction operator, * is the multiplication operator, and / is the division operator. These are all called arithmetic operators, because they perform arithmetic on numbers. Each of the lines above ends with a comment. Comments are a way to describe what your code is doing. They insert explanatory text into your code—text that the PHP interpreter will ignore. Comments begin with // and they finish at the end of the same line. If you need a comment to span several lines, you can instead start your comment with /*, and end it with */. The PHP interpreter will ignore everything between these two delimiters. I’ll use comments throughout the rest of this book to help explain some of the code I present. Returning to the operators, there’s another one that sticks strings of text together, called the string concatenation operator: $testvariable = 'Hi ' . 'there!'; // Assigns a value of 'Hi there!' Variables may be used almost anywhere that you use a literal value. Consider this series of statements: $var1 = 'PHP'; // Assigns a value of 'PHP' to $var1 $var2 = 5; // Assigns a value of 5 to $var2 $var3 = $var2 + 1; // Assigns a value of 6 to $var3 $var2 = $var1; // Assigns a value of 'PHP' to $var2 echo $var1; // Outputs 'PHP' echo $var2; // Outputs 'PHP' echo $var3; // Outputs '6' echo $var1 . ' rules!'; // Outputs 'PHP rules!' echo "$var1 rules!"; // Outputs 'PHP rules!' echo '$var1 rules!'; // Outputs '$var1 rules!' Notice the last two lines in particular. You can include the name of a variable right inside a text string, and have the value inserted in its place if you surround the string with double quotes instead of single quotes. This process of converting variable names to their values is known as variable interpolation. However, as the last line demonstrates, a string surrounded with single quotes will not interpolate the variable names it contains. PHP Variables, Operators, and Comments Sitepoint : New Articles, Fresh Thinking for Web Developers and Desi... http://articles.sitepoint.com/print/php-variables-operators-comments 1 sur 1 14/12/2010 14:46

Upload: michel-lecomte

Post on 06-Mar-2016

215 views

Category:

Documents


0 download

DESCRIPTION

Notice the last two lines in particular. You can include the name of a variable right inside a text string, and have the value inserted in its place if you surround the string with double quotes instead of single quotes. This process of converting variable names to their values is known as variable interpolation. However, as the last line demonstrates, a string surrounded with single quotes will not interpolate the variable names it contains. Kevin Yank $testvariable = 'Three';

TRANSCRIPT

Page 1: Sitepoint_Day3_PHP Variables, Operators, and Comments

About the Author

Kevin Yank

Kevin began developing for the

Web in 1995 and is a highly

respected technical author. He

wrote Build your own

Database Driven Website using

PHP and MySQL, a practical step-by-step

guide published by SitePoint, and he's

co-author of the SitePoint Tech Times, a

bi-weekly newsletter for technically-minded

web developers. Kev believes that any good

webmaster should have seen at least one

episode of MacGyver.

View all articles by Kevin Yank...

By: Kevin Yank

June 16th, 2010

Reader Rating: 9

Variables in PHP are identical to variables in most other programming languages. For the uninitiated, a

variable can be thought of as a name that’s given to an imaginary box into which any literal value may be

placed. The following statement creates a variable called $testvariable (all variable names in PHP

begin with a dollar sign) and assigns it a literal value of 3:

$testvariable = 3;

PHP is a loosely typed language. This means that a single variable may contain any type of data, be it a number, a string of text, or some other kind of

value, and may change types over its lifetime. So the following statement, if you were to type it after the statement above, assigns a new value to the

existing $testvariable. In the process, the variable changes type: where it used to contain a number, it now contains a string of text:

$testvariable = 'Three';

The equals sign we used in the last two statements is called the assignment operator, as it’s used to assign values to variables. Other operators may be

used to perform various mathematical operations on values:

$testvariable = 1 + 1; // Assigns a value of 2

$testvariable = 1 - 1; // Assigns a value of 0

$testvariable = 2 * 2; // Assigns a value of 4

$testvariable = 2 / 2; // Assigns a value of 1

From the above examples, you can probably tell that + is the addition operator, - is the subtraction operator, * is the multiplication operator, and / is

the division operator. These are all called arithmetic operators, because they perform arithmetic on numbers.

Each of the lines above ends with a comment. Comments are a way to describe what your code is doing. They insert explanatory text into your code—text

that the PHP interpreter will ignore. Comments begin with // and they finish at the end of the same line. If you need a comment to span several lines,

you can instead start your comment with /*, and end it with */. The PHP interpreter will ignore everything between these two delimiters. I’ll use

comments throughout the rest of this book to help explain some of the code I present.

Returning to the operators, there’s another one that sticks strings of text together, called the string concatenation operator:

$testvariable = 'Hi ' . 'there!'; // Assigns a value of 'Hi there!'

Variables may be used almost anywhere that you use a literal value. Consider this series of statements:

$var1 = 'PHP'; // Assigns a value of 'PHP' to $var1

$var2 = 5; // Assigns a value of 5 to $var2

$var3 = $var2 + 1; // Assigns a value of 6 to $var3

$var2 = $var1; // Assigns a value of 'PHP' to $var2

echo $var1; // Outputs 'PHP'

echo $var2; // Outputs 'PHP'

echo $var3; // Outputs '6'

echo $var1 . ' rules!'; // Outputs 'PHP rules!'

echo "$var1 rules!"; // Outputs 'PHP rules!'

echo '$var1 rules!'; // Outputs '$var1 rules!'

Notice the last two lines in particular. You can include the name of a variable right inside a text string, and have the value inserted in its place if you

surround the string with double quotes instead of single quotes. This process of converting variable names to their values is known as variable

interpolation. However, as the last line demonstrates, a string surrounded with single quotes will not interpolate the variable names it contains.

PHP Variables, Operators, and Comments

Sitepoint : New Articles, Fresh Thinking for Web Developers and Desi... http://articles.sitepoint.com/print/php-variables-operators-comments

1 sur 1 14/12/2010 14:46