This section provides you with a minimum knowledge of PHP. If you already know PHP, then I still advise you to scan through these pages as we will also will describe how you can modify the behavior of the many PHsPeed components.

Variables


Variables are placeholders for values. They have a unique name that starts with a dollar sign. If you come from other languages, PHP is weakly typed, which means that a variable can store virtually any data structure, which also can change over time. (In PHP 8.x it is also possible to do some strict typing, but that is out of scope here). A variable name always starts with a '$' and is followed by a sequence of letters and numbers, but the first character of the name must be a letter. Use a significant identifier. $name, $address are valid names that also is self-explanatory. $a, $b are not...

You can declare variables anywhere in your code, but they only exists in the context that they are used. So if you have created a procedure with a variable, then this variable will be forgotten as soon as you leave the procedure. We'll get back to that later. You declare a variable by assigning a value to it. See the next sample:


$name='janssen';                // name will be a string

$housenumber=12;                // house number is an integer.


Because variables can be declared anytime, it is possible to generate issues if you reference a variable without a value. For instance


echo $person; 


will generate an error if this is the first time that the variable is used and does not have an (initial) value. When you receive data from the web, it is not always clear that the data you require will be there. To overcome this issue, you can test if there is a value:


if(isset($person)) {

   echo $person;

}


This sample shows two new concepts: a conditional statement that will be executed when the value is true (isset($person)). A function (isset) that performs a certain task (here to test if the variable has a value). A block between {} that shows the full code that is going to be executed when the condition is true.


Variable types

In the above sample we've seen a string and an integer. In PHP you can have different types of data:

Integer.

This An integer is a whole number and has the same range as the “long” data type of C. In most machines (e.g. Intel Pentium processors), an integer is a 32-bit signed number that can be as low as -2,147,483,648 or as high as 2,147,483,647 Some examples: 


  • 10
  • 100
  • -1023

Floating point numbers

A floating-point number is a number that can contain decimals and an exponential notation. When accessed using a typical platform, a floating-point number is 8 bytes in size and can reach as high as 1.8E+308 down to 2.2E-308. A floating-point value can have a “+” or “-” sign. Some examples;


  • 12.40
  • 34.3E2
  • -23.44

String

Strings are a sequence of characters and numbers between single or double quotes. Some examples:


"this is a string"

'this is a string'


The difference is that a string between double quotes will be interpreted to find and replace variables.


Suppose the following:


$name= 'john';

$text = "hello $name";


After execution $text will contain hello john where


$name= 'john';

$text = 'hello $name';


$text will contain hello $name


When to use the single and double quotes? If you have no replacement to be executed use single quotes, as it is more efficient. But if you require a string containing a single quote, then use double quotes:


$text="my name is O'connor"';

Boolean

A boolean variable can only contain two logical values: true and false. The result of a condition is always true or false. 


$result=8 > 3 


$result will contain true


Null

When a variable has the value null, then it exists, but has no value. You can test if the value is null, but you cannot use it in expressions, without generating a run time error.

Arrays

An array is a list of values of any kind. In PHP you can have two separate array types, a regular array, than contains a number of different elements an dan associative array that handles key=>value pairs. 

An example of a regular array  is:


$a[0]=12

$a[1]=14

$a[2]=15


A regular array contains a variable name and its index in that array to represent a value. So the value of $a[1] is 14


An associative array is similar but with a 'bite':


$a['mynumber']=12

$a['yournumber']=1

$a['hisnumber']=12


Here the value of $a[1] is undefined. But the value of $a['yournumber'] is 1

Scope of variables


The scope of a variable describes its validity at any point. In general, when you have written functions, variables are only valid within the procedure that they are created. So what if you need that value anywhere in your program. In that case you can declare the variable as global. In this case the variable will be created once and live until the application terminates.

Session variables

Session variables are variables that persists over your complete session. So if your PHP application terminates then these variables 

Superglobal variables


When you have created a web application then the content of a form is sent to your application. So how do you read those variables. First of all there are two way to send data to your form. The first on is when you type an url with parameters.


https://www.mysite.com/myapp.php?name=janssen&housenumber=12


These variables are part of the url and are send as GET variables.


If you have a form and hit the submit button then the content of the field are send as POST variables.


PHP has 'superglobal' variables that contains this data:


$_ENV[] - This is an array that contains environment variables.
$_GET[] - This array holds all of the “GET” variables gathered from the user’s web browser


Because these variables are 'always' there they are mentioned here for clarity. PHsPeed retrieves this data and makes them available as session variables under their own name while protecting the data against XSS. Therefore using the variables directly is not recommended. But if you need certain information, then it is there.


Casting variables


If you perform calculations, then it can be necessary to 'change' the datatype. Suppose you have a string "10" then you cannot calculate with that value. Before you do, you need to cast the value to a number:


$a="10";

$b=(int) $a + 10;


Object variables ('properties')

Just to be complete, PHsPeed is fully object oriented.  That means that there are rules to access variables that are bound to that object (usually called 'properties'). In the paragraph 'object oriented programming' we will go deeper into this but know that if you have to access a variable of a certain object (or 'class') that you need to 'point' to them like:


$myclass->property. (Class variables are called properties)


Your html form contains fields that you want to process in your code. These fields are also objects. To access those, you can use the short $$ notation. So if you have a field on the form then the object in your code is called 'address', then you can access this object with $$adress->some property.

PHsPeed considerations


Get variables. 


The 'get' variables are available as session variables. To set and retrieve a session variable use the function getSessionVar and setSessionVar. We'll get back to this later. Avoid to access the $_Get and $_Post arrays directly, to avoid security issues. 


Put variables.


The put variables are fully managed by PHsPeed. All data becomes available in the value property of the component that manages the field on the form.