Phpdynamic

PHP and MySQL Programming Guides, Tutorials, & Articles

Beginning PHP – Language Syntax

Beginning PHP – Language Syntax

MAR 11

Syntax and Semantics

All programming languages must follow a proper syntax, which is a set of rules that control the arrangement of words, numbers, and symbols for writing valid code; and PHP is no exception. In the same sense that English sentences have to follow certain grammatical rules to have a proper meaning (semantic), all PHP statements must follow a certain set of syntax rules to execute correctly.

Statements

A PHP script is essentially a structured set of statements that are composed of programming building blocks such as variables, constants, and expressions. Statements are programming instructions that work together to perform a particular task. There are many types of statements in PHP, each used for a specific purpose such as displaying output, evaluating expressions, and calling functions. Following are some examples of various statements:

<?php
  //output statement
  print("Hello from PHP");

  //variable assignment statement
  $sum = 5 + 4;

  //simple function call statement
  date();
?>

Note, each statement in PHP must terminate with a semicolon character (;). Without the semicolon, the PHP interpreter will have trouble determining where one statement ends and the next statement begins.

From the PHP interpreter’s point of view, statements are perfectly valid if separated by semicolons regardless if all the statements span one continuous line or several lines. The interpreter ignores all white-space characters such as spaces, tabs, and new line-feeds. With this in mind, you should add spacing characters between your statements to make your code easier to read and understand. Consider the following examples:

<?php
  //example 1
  $tax = 5 * 0.025;print("Tax due : ");print $tax;

  //example 2
  $tax = 5 * 0.025;
  print("Tax due : ");
  print $tax;
?>

Both statement are valid and will produce the same results, but the second example is easier to read and modify.

Identifiers

Identifiers are exactly what the name implies, they uniquely identify variables, constants, and functions with a meaningful name. In the previous examples both $sum and $tax are user-created variable identifiers. You have the freedom to choose any identifier name you want, as long as the name compiles with a few simple rules.

PHP identifier names are case-sensitive and can consist of both upper-lowercase letters, numbers, and underscore characters (_). Identifiers must begin with a letter or an underscore and subsequent characters can include either additional letters, numbers, or underscores. PHP has several reserved identifiers called keywords that cannot be used as valid names; we’ll cover some of these keywords as we progress throughout this tutorial.

Consider writing identifiers with meaningful names that follow a standard naming style to make your scripts more consistent and readable. Conventionally, identifier names are usually lower case and multiple word identifiers are delimited with underscores, such as $tax_rate, total_qty(), or $login_name. Although, you can choose any identifier naming style you want, it is essential that you use a consistent style throughout your scripts.

We’ll look at some examples of both valid and invalid identifier names below:

<?php
/* Valid Identifiers */
$name
$_lunch
$total_hours
my_function()

/* Invalid Identifiers */
$lunch time //contains a space
$4hour //starts with a digit
$this //reserved keyword
?>

Expressions

Expression are the bread and butter of a program. They perform specific actions to transform and manipulate data. Expressions are composed of literals, operators, and operands. Literals are the actual data values used in a program; numbers, strings, and alphanumeric characters are all examples of literals. Operators are programming symbols that are used to perform the necessary computation on operands (data that is manipulated). Lets look at some examples:

<?php
/* Example 1 */
$sum = 4 + 5; //$sum = 9

/* Example 2 */
$total = $sum - 4; //$total = 5
?>

Looking at example 1, both the equal symbol (assignment) and the plus symbol (addition) are operators acting on three operands: the variable $sum and the literal values 4 and 5, respectively. In the example, the assignment operator (=) is used to store the value of the expression on the right side of the equal symbol into a variable called $sum. Note, the expression is evaluated first and then the resulting value is stored into the variable, in example 1 the number 9 was stored into the $sum variable and in example 2 the numerical value 5 was stored into the $total variable. In PHP, every data type has its own set of operators. We’ll cover them when we begin our discussion on PHP data types.

In this tutorial, we have introduced the concepts of PHP statements, expressions, and identifiers. With this introduction you can gain a better understanding of PHP syntax requirements and the logical structure of a script. In the following tutorials, we’ll augment this primer with more in depth examples and explanations.

  • Share/Bookmark

You can leave a response, or trackback from your own site.

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>