Writing PHP code and HTML code together

Since PHP is a server side scripting language, if you run a PHP page on a local machine, as opposed to on a web server (hosted somewhere), it will simply output everything as plain text.

PHP code must start with <?php and must end with ?>.

HTML code can NOT be written inside of PHP tags (as described above), however PHP code can be written inside HTML.

In order to display something on the browser using a PHP script we need to use the syntax echo and put whatever you want to write in double quotes followed by a semicolon ";". For example I am going to use PHP code to display "hello world" onto the screen:



hello world!

Variables in PHP

Variables in PHP are defined using a "$". Although the best practice is to display the content of a variables without using "", it is possible to put variables inside the "" and PHP will compile and display the variables content.

String concatenation in PHP

In HTML strings were concatenated used "+". In PHP they are concatenated using a period; ".".

$string1 = "Hello";
$string2 = "World!";
echo $string1." ".$string2;


Hello World!

Numeric variables in PHP

Numeric variables in PHP are defined similar to text variables. Mathematical operations can be carried, and displayed on screen if needed.

Boolean variables in PHP

Boolean variables in PHP can be defined using true and false. In PHP, boolean value of True is 1, while a False has a value of blank.

$boolean = true;
echo "<p>The value of my boolean variable is ". $boolean . </p>"


The value of my boolean variable is 1.

$boolean = false;
echo "<p>The value of my boolean variable is ". $boolean . </p>"


The value of my boolean variable is .

Variables storing variable names

In PHP, variables can store variable name. Given this, it is possible to use variables containing variable names in computations using $$.

$name = "Pavan";
$nameField = "name";
echo "<p>The content of variable "nameField" is ".$nameField.".<.p>";
echo "<p>The content of the variable stored in variable "nameField" is ".$$nameField.".<p>;

$$nameField = "Lori";
echo "<p>The content of variable "nameField" is ".$nameField.".<.p>";
echo "<p>The content of the variable stored in variable "nameField" is ".$$nameField.".<p>;


The content of variable "nameField" is "name".

The content of the variable stored in variable "nameField" is "Pavan".

The content of variable "nameField" is "name".

The content of the variable stored in variable "nameField" is "Lori".