Saturday, June 2, 2012

PHP Basic Syntax and PHP Commands



PHP syntax will be very familiar to anyone with an understanding of C, C++, C#, Java, JavaScript, Perl, or any other C-derived language. A PHP script consists of a series of commands, or statements. Each statement is an instruction that must be followed the Web server before it can proceed to the next. PHP statements, like those in the above-mentioned languages, are always terminated by a semicolon (;).

This is a typical PHP statement:
echo 'This is a <b>test</b>!';


This is an echo statement, which is used to send output to the browser. An echo statement simply takes the text it’s given, and places it into the page’s HTML
code at the current location. Order this 350 page hard-copy PHP/MySQL book now! 45

Basic Syntax and Commands
In this case, we have supplied a string of text to be output: 'This is a <b>test</b>!'. Notice that the string of text contains HTML tags (<b> and </b>), which is perfectly acceptable. So, if we take this statement and put it into a complete PHP script (echo.php in the code archive), here’s the code we get:


File: echo.php


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple PHP Example</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p><?php echo 'This is a <b>test</b>!'; ?></p>
</body>
</html>


If you place this file on your Web server, a browser that views the page will see this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple PHP Example</title>
<meta http-equiv="content-type"
content="text/html; charset=iso-8859-1" />
</head>
<body>
<p>This is a <b>test</b>!</p>
</body>
</html>


Our today.php example contained a slightly more complex echo statement:
File: today.php (excerpt)

echo date('l, F dS Y.');

Instead of giving echo a simple string of text to output, this statement invokes a built-in function called date and passes it a string of text: 'l, F ds Y.'. Built-in functions can be thought of as things that PHP knows how to do without our needing to spell out the details. PHP has many built-in functions that let us do everything from sending email, to working with information stored in various
46 Order this 350 page hard-copy PHP/MySQL book now!

Chapter 3: Getting Started with PHP

types of databases. In this case, the date function produces a text representation of the current date, using the string it is given to determine the format. You may wonder why we need to surround the string of text with both parentheses (()) and single 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 two purposes. First, they indicate that date is a function that you want to call.

Second, they mark the beginning and end of a list of parameters that you wish to provide, in order to tell the function what to do. In the case of the date function, you need to provide a string of text that describes the format in which you want the date to appear.1 Later on, we’ll look at functions that take more than one parameter, and we’ll separate those parameters with commas. We’ll also consider functions that take no parameters at all. These functions will still need the parentheses, though we won’t type anything between them.

By PHP with 1 comment

1 comments:

Very nice informative article. As we know PHP is open source and easy language to learn. You can create a good website with the help of PHP coding.

Post a Comment

    • Popular
    • Categories
    • Archives