Please enable JavaScript to view this site.

Process Designer

Navigation: PHP API > Introduction to PHP

Use variables and comments

Scroll Prev Top Next More

Of course it is not practical to use PHP to display text only. PHP can be used for calculations or process forms and files. The basis to do so are the variables. These variables can be stored and reused. It is useful to name the variables meaningfully and set comments where necessary so the code can be understood by more people than just the programmer itself. Comments are merely made to inform and are not interpreted, but can be quite useful to others. Comments are distinguished by single-line comments for short notes starting with a // and multi-line comments for explanatory texts or longer annotations limited by /* */. Example:

<?php

// single-line comment

 

/* Multi-line comment

Here could be a longer explanatory text for the program

*/

?>

All further examples should be documented this way to contribute towards a better understanding and store explanations directly in the program code. This reduces the amount of text significantly.

In our second example Hello world! is used twice. Einmal im Once in the <title> element and once in the <body> element. If you wanted to display Hello PHP world! instead of Hello world, you would have to change the text in two places. An alternative to this is to save the text in a variable and use it to display the text.

An example to illustrate this:

<?php

// Here the text is saved within a variable

$helloWorld = "Hello World!";

?>

<!DOCTYPE html>

<html lang="en">

<head>

 <title>

 <?php 

/* 

Variable output in title element

The previously saved text "Hello world!" is displayed

Attention: Do not use apostrophes. It would theoretically work, but is not necessary in this case

*/

 echo $helloWorld;

 ?> 

 - Example

 </title>

</head>

<body>

<?php

//"Hello world!" is displayed again

echo $helloWorld;

?> 

</body>

</html>

The PHP file starts with a single-line comment starting with //. After the first commenting line, a variable is assigned. To assign a variable, use a $ symbol followed by the name of the variable. The variable name should only consist of letters A-Z, numbers 0-9 and _. Please do not use language-specific letters (such as Ä, å, Б) or other special characters. Additionally you have to pay attention to capitalization of the variable name when you refer to it. The value of a variable is assigned with an equal sign. The variable can be displayed by using echo twice in the code.