How are variables declared in PHP

Variable Types in PHP: Differences and Usage

PHP supports different types of variables. Data is stored in variables: for efficient processing there are different variable types such as string, integer, float, Boolean and array.

Integers (so no decimal places or rounding problems :)

Floating point number (float)
Numbers with decimal places that should be taken into account when rounding

String Character strings in which anything can be found (a number can also be defined as a character string)

Truth value, is normally set and queried with TRUE or FALSE

Variables "on ecstasy" - can easily accommodate large amounts of data (see chapter Array)

Examples of variable types

PHP source code: variable types in PHP

Variables and Forms

The security rule is do not trust any user input. It is therefore important if, for. B. an integer is expected to be made INTEGER using the corresponding function. Garbage information is thrown out, clean information is retained and you can assume a clean basis when evaluating.

Typing is very easy.

PHP source code: typing variables in PHP

The following conversions are possible by specifying the contents of brackets including brackets:

  • (int), (integer) - after integer
  • (float), (double), (real) - after float
  • (string) - after string
  • (bool), (boolean) - after boolean
  • (array) - after array

This means that before the data is saved in a file or database, it is simply set to its corresponding type for security reasons.

If you want to do it perfectly, the user can also get feedback in the form that something is wrong with the input, e.g. if the expected integer input is not one.

Example test integer specification

PHP source code: test for variable type and same content

Testing can also take place via the corresponding PHP function:

PHP source code: functions for testing variable types

If you find a bug, please let us know (no matter if typographical or content-related error).

With a mouse Mark the faulty point and take over with the following button:

After submitting it comes here feedback! Please do not send twice. Thanks.