Menu Close


Introduction to PHP

Rasmus Lerdorf released the first version of PHP way back in 1994. It was designed as a light weight scripting language for dynamic websites. It is used by: Facebook, Wikipedia, Flickr, Yahoo!, iStockPhoto, Tumblr, WordPress and many other popular websites.

Syntax Overview

PHP Is a scripting language for web pages similar to Microsoft ASP. The difference is that PHP is open source and is usually deploy on Linux while ASP is deploy on Windows platform. 

The PHP script can be created in a HTML page, using a special tag: <?php… ?>. So before you learn PHP you should learn HTML. If you do not know yet HTML that’s OK, you will learn some during this tutorial. 


Note: When you embed PHP code in an HTML file, you need to use the .php file extension for that file, so that your web server knows to send the file to PHP for processing.

String Concatenation

Variable names in PHP start with $ prefix. PHP is a dynamic language. Type of variable is automatically detected by the PHP engine. In next example we concatenate a string literal with variable using a dot. 

Variable Rules:

  • variable name stat with “sigil” symbol $,
  • variable name is case sensitive,
  • variable name can contain digits, underscore and ASCII characters (a..z,A..Z).

Note: Statement “echo”, will create output for the browser, so it can contain HTML tags.

PHP Comments

PHP comments are like C comments. In addition, symbol “#” will start also a line comment like in Python. Line comments start with “//”, block comments are enclosed between “/* … */”.

Note: Remember HTML comments are also possible, using notation <!– …. –>, so you can comment out the entire <php? … ?> block, but this will not stop the code engine to execute PHP code inside HTML comments. So you can create dynamic comments using PHP. Is that something?


String Interpolation

One very useful feature of PHP is the ability to replace a variable inside a string template without a special notation. The only thing to do is to use double quotes for string to be used as a template.



Note: You can see inside the string the back-slash “\$first” will prevent interpolation. 

Functions & Scope

In PHP, variables can be declared global, local or static. The global variables are visible in any PHP code. Local variables are visible in functions.  To make global variables visible in function, you must use keyword: “global”. This will prevent “shadowing” effect.


Note: The global variables are stored into a global dictionary $GLOBAL. You can query a variable by its name using expression: $GLOBAL[‘name’]. 

Static variables

These variables are local to functions where they are defined. Unlike regular variables these variables are preserving the state when a function is called second time. Variable is not re-initialized second time.


String Output 

You have already visualized several examples that print out a string into HTML document. This is done using “echo” statement. This statement can be used with (…) that are optional and can have one or multiple parameters, all are output in HTML document.

You can do the same with “print” statement, except this statement is not going to enable multiple parameters. “print” is a little bit slower than “echo”. Print also can be used with (…).

HTML Escape

PHP can be used in combination with HTML and XML tags. The general idea is, other content in *.php files is ignored if is not enclosed in recognized tags: <?php?>. A cool trick is to use some php and some html using conditionals:


Note: The $expression can be a variable declared in another <?php?> fragment. So your php code can be fragmented into multiple tags but the variables are global, they can be used in any other <?php?> fragment after they are defined.

Alternative Syntax

Actually most control statements offer alternative syntax. This is useful to make PHP code more readable when is entangled with HTML. In next example you can see a bluet list created with alternative syntax for “while” statement:


Short Tag

The short tag <?= $expression ?> can be used in php files instead of <?php echo $expression ?>. This is called “short php tag” and is useful when you have a string variable to show immediately in your page.

PHP Keywords

Do not let anybody fool you into believe that PHP is a toy language. It is not so! I have counted 56 keywords in PHP, compared with 51 in Java. In my next articles I will explain how to use these keywords to write you first PHP scripts.   

abstract and array as break callable case catch class clone const continue declare default die do echo else elseif empty enddeclare endfor endforeach endif endswitch endwhile eval exit extends final finally for foreach function global goto if implements include include_once instanceof insteadof interface isset list namespace new or print private protected public require require_once return static switch throw trait try unset use var while xor yield

Next article: Data Types