by Kevin Yank of SitePoint.com
Basic Syntax and Commands
PHP syntax will be very familiar to anyone with an understanding of C, C++, Java, JavaScript, Perl, or any other C-derived language. A PHP script consists of a series of commands, or "statements", each of which is an instruction that the Web server must follow before proceeding to the next. PHP statements, like those in the above-mentioned languages, are always terminated by a semicolon (;
).
The following is a typical PHP statement:
echo( "This is a <B>test</B>!" );
This statement invokes a built-in function called echo and passes it a string of text: This is a <B>test</B>
! Built-in functions can be thought of "things that PHP knows how to do without us having to spell out the details". PHP has a lot of built-in functions that let us do everything from sending e-mail to working with information stored in various types of databases. The echo function, however, simply takes the text that it is passed and places it into the HTML code of the page at the current location. Consider the following:
<HTML>
<HEAD>
<TITLE> Simple PHP Example </TITLE>
</HEAD>
<BODY>
<P><?php echo("This is a <B>test</B>!"); ?></P>
</BODY>
</HTML>
If you paste this code into a file called test.php
(or test.php3
if your Web host has not configured .php
files to be recognized as PHP scripts) and place it on your Web server, a browser viewing the page will see the following:
<HTML>
<HEAD>
<TITLE> Simple PHP Example </TITLE>
</HEAD>
<BODY>
<P>This is a <B>test</B>!</P>
</BODY>
</HTML>
Notice the string of text contained HTML tags (<B>
and </B>
), which is perfectly acceptable.
You may wonder why we needed to surround the string of text with both parentheses and quotes. Quotes are used to mark the beginning and end of strings of text in PHP, so their presence is fully justified. The parentheses serve a dual purpose. First, they indicate that echo is a function that you want to call. Second, they mark the beginning and end of the list of "parameters" that you wish to provide to tell the function what to do. In the case of the echo function, you only need to give the string of text to appear on the page, but we'll be looking at functions that take more than one parameter (for which we'll list the parameters separated by colons), as well as functions that take no parameters at all (for which we will still need the parentheses, but won't type anything between them).
Variables and Operators
Variables in PHP are identical to variables in most other programming languages. For the uninitiated, a variable is a name given to an imaginary box into which any 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 value of 3:
$testvariable = 3;
PHP is a "loosely typed" language, which 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 written after the statement above, assigns a new value to our existing $testvariable
. In the process, the variable changes from containing a number to containing a string of text:
$testvariable = "Three";
The equals sign we used in the last two statements is called the "assignment operator", as it is 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.
The lines above each end with a comment. Comments are a way to describe what your code is doing by inserting explanatory text into your code and telling the PHP interpreter to ignore it. Comments begin with //
and end at the end of the same line. If you're familiar with /* */
style comments in other languages, these work in PHP as well. I'll be using comments throughout the rest of this series to help explain what the code I present is doing.
Getting back to the four statements above, the operators used allow you to add, subtract, multiply, and divide numbers. Among others, there is also an operator for sticking strings of text together:
// Assigns a value of "Hi there!".
$testvariable = "Hi " . "there!";
Variables may be used pretty much anywhere an actual value can be. Consider the following example:
$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 especially. 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. As the last line demonstrates, however, a string surrounded with single quotes will not convert variable names to their values.
User Interaction and Forms |
SitePoint.com is a fast growing Web Developer Community. Kevin Yank is the Editor of the SitePoint TechTimes, a fresh, technically oriented newsletter for the serious Webmaster. |