Apr 18, 2014

QNAP/Linux PHP Programming 00 - Introduction

The article is an introduction for readers who have experiences on coding, such as C/C++ programming. Hence, common programming syntax, naming policy, or coding style are omitted in this blog.


The PHP is named from "Hypertext Pre-processor", a programming language for developing web applications (CGI programs) that interacts with databases. Thus, you can regard PHP as a parser/interpreter for coding web applications with an HTML-script-like syntax.
  • PHP runs on the server side like scripts embedded in HTML (like MS ASP or VB scripts).
  • PHP supports several popular databases, like MS SQL Server, MySQL, PostgreSQL, Oracle, Sybase, Informix, etc.
  • System functions can be invoked from PHP so that many useful and powerful execution files on Unix or Windows systems can be integrated into PHP applications.
  • Many build-in functions and external library modules: access cookies variables and set cookies, read/write files, send/receive emails, encrypt data, etc. E.g. 24 Cool PHP Libraries You Should Know About.
  • PHP works with almost all Web Server software on widely used OS platforms (Windows, Unix, Mac, etc.), including Microsoft's Internet Information Server (IIS). However, Apache Server is strongly recommended.
PHP libraries/modules supported IPKG of QNAP
<?php ... ?>
PHP codes begin with tag "<?php " and end with tag "?>". HTML tags and PHP codes can be mixed like the following examples. Two widely used begin-end tags of PHP codes are also introduced in this example. NOTE: ASP-like tag <% ... %>.
<html>
 <head>
  <title>Hello! HTML</title>
 <head>
 <body>
  <?php echo "Hello! PHP codes embedded in HTML"; ?>
  <?php echo "<h1>Hello! PHP codes contain HTML tags<h1>"; ?>
 <? echo "<h1>Hello! ? codes contain HTML tags<h1>"; ?>
  <script language="php">echo "<h1>Hello! script codes contain HTML tags</h1>";</script></body>
</html>
The syntax of PHP is C-Like style + script-style.
Single-line comments: // ... or # ...
Multi-lines comments: /* ... */
If you want to assign some string to a variable ($s) or write html contents with structured style, "Here Document" writing style (<<<TAG) is useful. For example, common-used tags can be ENDEOFEOLHTMLXML, etc. are also useful to build clear programming-style. Although PHP is whitespace/tab insensitive, don't put any whitespaces or tabs at the end of "<<<END" and "END;"; otherwise, something wrong will happen in PHP parser that is hard to debug.
<<<END
...
END;
NOTE: ", ', tab, in the following example.
Snapshot of PHP codes: PHP tags, print, 
PHP Syntax
  • A PHP statement must be end with a semicolon ;.
  • PHP codes is case sensitive, e.g. $var and $VAr are different.
  • PHP variable, like Perl, is started with $. PHP variable has no type initially, it depends on the assigned value: e.g., number (3.14), string ('abc'), or constant (TRUE). PHP does well in automatic type conversions.
  • Variable names must begin with a letter or underscore character.
  • Constant variable use define, e.g., define("PI", 3.14159); echo PI; 
  • Magic constant variables: __LINE__, __FILE__, __FUNCTION__, __CLASS__, __METHOD__ are useful, e.g. extract line# or function name of error code.

PHP Object Types
5 simple types: integers, doubles, booleans, NULL, string.
Boolean type: TRUE (value != 0 or not empty string/array) or FALSE. The following results of variables are TRUE or FALSE?

  • TRUE: $n = 3 + 0.14159; $s = "not empty"; $array[49] = "has elements";
  • FALSE: $a = array(); $nul = NULL; $n = 1 - 1; $s = "";

NULL is a special type that only has one value: NULL or null.

Three complex types as followings. Concepts of these types are similar to those we knew in C/C++, C# or Java languages.
  • Arrays: collections of values with any dimensions.
  • Objects: instances of programmer-defined classes, consisting of values and functions that are specific to the class.
  • Resources: special variables referring to resources external to PHP, such as database connections.

String/Output Representation
  • "string" or 'string', but " " can contain $var
  • constant string contains string variables, e.g. echo "$s1 + $s2" //'$s1 + $s2' --> wrong expression
  • string concatenation operator: $s1.$s2 (NOTE: + is operator of number "add", )
  • echo and print are the same command, but echo is more efficient: echo(string); echo "string"; echo 'string'; echo $string; print(string); print "string"; print 'string'; print $string; all expressions are the same.
Special character: escape-sequence replacements.

  • \n is replaced by the newline character
  • \r is replaced by the carriage-return character
  • \t is replaced by the tab character
  • \$ is replaced by the dollar sign itself ($)
  • \" is replaced by a single double-quote (")
  • \\ is replaced by a single backslash (\)

Sample codes for testing aforementioned statements

PHP Operators (Arithmatic/Logical/Assignment/Conditional operators and operator precedence) are similar to most of programming languages, just try errors and learn the experience. Decision makings of "if .. else", "switch", "? : " statements and loop (while and for) statements are also as our common sense, thus we omit them in this blog.

If you are navie in programming, more detail of learning PHP is available from: http://www.tutorialspoint.com/php/

Learn by doing is the rule of thumb for learning any programming languages. After reading this article, just do it for your desire Apps.

No comments :

Post a Comment