Basic PHP Language Elements

Using the Hello World! program as a starting point, we will examine the basic elements of the PHP language.

Basic Syntax Rules in PHP

PHP has adopted many aspects of the C programming language (the interpreter itself is written in C). However, there are some major aspects in which it differs from C. The major aspects and syntactic elements of PHP include the following.

  • PHP is an interpreted language: it is not compiled. Instead it is interpreted through an interpreter that parses commands in a script file line-by-line. Any syntax errors, therefore, become runtime errors.
  • PHP is dynamically typed: you do not declare variables or specify a variable’s type. Instead, when you assign a variable a value, the variable’s type dynamically changes to accommodate the assigned value. Variable names always begin with a dollar sign, $.
  • Strings and characters are essentially the same thing (characters are strings of length 1). Strings and characters can be delimited by either single quotes or double quotes. "this is a string", 'this is also a string'; 'A' and "a" are both single character strings.
  • Executable statements are terminated by a semicolon, ;
  • Code blocks are defined by using opening and closing curly brackets, { ... }. Moreover, code blocks can be nested: code blocks can be defined within other code blocks.

PHP also supports aspects of OOP including classes and inheritance. There is also no memory management in PHP: it has automated garbage collection.

Though not a syntactic requirement, the proper use of whitespace is important for good, readable code. Code inside code blocks is indented at the same indentation. Nested code blocks are indented further. Think of a typical table of contents or the outline of a formal paper or essay. Sections and subsections or points and sub-points all follow proper indentation with elements at the same level at the same indentation. This convention is used to organize code and make it more readable.

PHP Tags

PHP code can be interleaved with static HTML or text. Because of this, we need a way to indicate what should be interpreted as PHP and what should be treated as static text. We can do this using PHP tags: the opening tag is <?php and the closing tag is ?>. Anything placed between these tags will be interpreted as PHP code which must adhere to the syntax rules of the language.

A file can contain multiple opening/closing PHP tags to allow you to interleave multiple sections of HTML or text. When an interpreter runs a PHP script, it will start processing the script file. Whenever it sees the opening PHP tag, it begins to execute the commands and stops executing commands when it sees the closing tag. The text outside the PHP tags is treated as raw data. The interpreter includes it as part of its output without modifying or processing it.

PHP Libraries

PHP has many built-in functions that you can use. These standard libraries are loaded and available to use without any special command to import or include them. Full documentation on each of these functions is maintained in the PHP manual, available online at https://php.net. The manual’s web pages also contain many curated comments from PHP developers which contain further explanations, tips & tricks, suggestions, and sample code to provide further assistance. There are many useful functions that we’ll mention as we progress through each topic. One especially useful collection of functions is the math library. This library is directly adapted from C’s standard math library so all the function names are the same. It provides many common mathematical functions such as the square root and natural logarithm. Dull documentation can be found in the PHP manual https://php.net/manual/en/ref.math.php. To use these, you simply “call” them by providing input and assigning the output to a variable.

$x = 1.5;
$y = sqrt($x); //y now has the value
$z = sin($x); //z now has the value sin (x) = sin (1.5)

In both of the function calls above, the value of the variable $x is “passed” to the math function which computes and “returns” the result which then gets assigned to another variable.

PHP Comments

Comments can be written in PHP code either as a single line using two forward slashes, //comment or as a multiline comment using a combination of forward slash and asterisk: /* comment */ . With a single line comment, everything on the line after the forward slashes is ignored. With a multiline comment, everything in between the forward slash/asterisk is ignored. Comments are ultimately ignored by the interpreter. Consider the following example.

//this is a single line comment
$x = 10; //this is also a single line comment, but after some code
/*
  This is a comment that can
  span multiple lines to format the comment
  message more clearly
*/
$y = 3.14;

Entry Point & Command Line Arguments

Every PHP script begins executing at the top of the script file and proceed in a linear, sequential manner top-to-bottom. In addition, you can provide a PHP script with inputs when you run it from the command line. These command line arguments are stored in two variables, $argc and $argv . The first variable, $argc is an integer that indicates the number of arguments provided including the name of the script file being executed. The second, $argv is an array of strings consisting of the command line arguments. To access them, you can index them starting at zero, the first being $argv[0] , the second $argv[1] , the last one would be $argv[$argc-1] . The first one is always the name of the script file being run. The remaining are the command line arguments provided by the user when the script is executed.


Licenses and Attributions


Speak Your Mind