What are callable types in PHP

PHP / data types

Internal data types

PHP works largely typelessly. Whenever possible, PHP will recognize and convert the data type itself.

A general distinction is made between three data types. PHP5 knows the following data types for variables.

Scalar data types
Integer. "int" is an alternative notation for "integer".
Logical value ("TRUE" or "FALSE"), internally 0 or 1. "bool" is an alternative notation for "boolean".
Floating point number. "real" is an alternative notation for "double".
String of 0 to 2 billion characters.
Complex data types (compositions)
Array. It can contain multiple elements.
object
Special data types (for system-internal use)
A resource, i.e. a pointer to a file, database or similar external data source. Conversion to other types is not possible. The output produces information such as "Resource # 5".

Compared to real programming languages, the possibilities are limited, but sufficient for practical use.

Automatic type conversion

With automatic type conversion, PHP first tries to recognize the right side of the expression and then assigns it to the left side. PHP works according to the following rules:

  • A string is assumed for values ​​enclosed in double quotation marks (").
  • An integer is assumed for a number without a point (comma).
  • A floating point number is assumed for a number with a point (comma).

When evaluating expressions, the operator used determines the type of overall expression. If this forces a numeric expression, PHP tries to search a following string expression for numbers.

$ variable = "A"; echo (++ $ variable); echo "
\ n"; $ variable = 17; echo (++ $ variable); echo "
\ n"; $ variable = 17; $ variable = $ variable + 23.5; echo ($ variable); echo "
\ n";
B.
18
40.5

The string "5 pieces" is converted to "5" here.

$ variable = 12; $ variable = $ variable + "5 pieces"; echo ($ variable);
17

The period is the operator for linking strings, so that the result is "125 pieces".

echo "
\ n"; $ variable = 12; $ variable = $ variable. "5 pieces"; echo ($ variable);
125 pieces

Convert data types

To force a data type, the name of the data type can be placed in front of the expression. Since the data type conversion with operators has priority, it will only be absolutely necessary where there are no unambiguous operators.

$ variable = 100; $ fvar = (double) $ variable + "100.5"; $ svar = (int) $ fvar; echo ("Double: $ fvar, Integer: $ svar");
Double: 200.5, Integer: 200

Conversions do not make sense in all directions.

Target typeMeaningful source typesConversion principle
"TRUE" becomes "1", "FALSE" becomes "0".
Decimals are cut off (no rounding).
If no number is recognized, "0" is returned.
Unchanged.
If no number is recognized, "0" is returned.
Returns the number as a string.
An array is created with an element of the original type.
Will be converted directly.
Will be converted directly.
The result is an object with a property that is represented by the variable.
"0" becomes "FALSE", every other number becomes "TRUE".

When converting strings, PHP5 applies certain rules:

  • occurs when the character string begins with a valid numeric character (digit, plus, minus) and is followed by the characters ".", "e" or "E". If the first part of the string is a valid expression, the rest is ignored.
  • arises when the character string begins with a valid numeric character (digit, plus, minus) and after it Not the characters ".", "e" or "E" follow. If the first part of the string is a valid expression, the rest is ignored.

The characters "e" or "E" are used to represent exponents. The spelling "En"stands for" x10n". In general, the point". "Is used as a decimal separator. For output, numbers can be converted into the required form using the" number_format "function.

Conversion functions

In addition to specifying the data type in round brackets, the "settype" function can also be used. The variable to be converted is specified as the first parameter, the data type as a character string as the second parameter. The function returns "1" ("TRUE") if the conversion was successful, otherwise "0" ("FALSE").

settype ($ variable, "integer");

In addition, the derived "val" functions can also be used. These will return the converted variable if the conversion was successful.

functiondescription
Conversion to the data type "double".
Conversion to the data type "integer".
Conversion to the data type "string".

Determine data types

The "gettype" function can be used to find out which data type is being used. It returns the known type identifier as a character string. If the data type could not be recognized, the character string "unknown type" is generated.

echo "The data type is:", gettype ($ variable);

The use of "gettype" is too cumbersome with logical expressions. The following functions can therefore be used, which return "1" ("TRUE") or "0" ("FALSE").

function"TRUE" for the following data type

With the more general function "is_scalar" the basic data types (the so-called scalars) can be recognized. These are the data types "boolean", "float", "integer" and "string". Arrays and objects, on the other hand, are composed data types, not scalars.

Determine special variable states

The special form "resource" can also be tested with the "is_resource" function. After a file or database access, for example, it can be determined whether it was successful. Such functions return handlers that PHP uses in subsequent function calls to reconnect with the source.

The "get_resource_type" function not only returns information on whether a resource exists, but also its data type. It returns unique strings like "file" or "mysql link".

Some variables have the value "NULL" - they are then already created, but completely empty. Empty is a different state than an empty character string (which is already a character string) or the number "0" (which is clearly a number, so more than nothing). The difference is not always clear in simple comparisons with "==", because internal data type conversions take place here and only values, not types, are considered. So the following expression is true:

$ number = 0; if ($ number == NULL) echo "true";

But that is actually not correct, because "NULL" is a different state than "0", as just described. The operator "===" therefore includes the data type and correctly decides that "0" is not equal to "NULL".

Variables can contain names of functions and can be used to call the function indirectly. The "is_callable" function is used to check whether a variable contains such a "callable" name. If the call target is a method of a class, the output is in the form "array ($ object, 'method name')", ie completely as follows.

is_callable (array ($ object, "method name"));

Test the presence of variables

The "empty" function checks whether a value has been assigned to a variable. If the variable exists and its content consists of "0" or an empty string, "1" ("TRUE") is returned.

$ empty = empty ($ variable);

The "isset" function checks whether a variable exists. In the case of logical comparisons, it can be decisive whether a variable is filled with "0" or an empty character string, or has not yet been assigned at all. If the variable is present, "1" ("TRUE") is returned, otherwise "0" ("FALSE").

$ exists = isset ($ variable);

Dealing with the functions "empty" and "isset" can lead to tricky programming traps due to the primitive internal representation of the variable contents in PHP. The following table therefore contains all possible combinations of states for variables and the reactions of the two functions. You should keep in mind what PHP represents internally. For example, the status "TRUE" that can be assigned to a variable is identical to the assignment "$ str = 1;". The following expression is suitable to make sure whether the variable returns "FALSE" as content or is actually not defined.

if! (isset ($ str)) {...}
State after
empty! emptyeat! eatempty! emptyeat! eat
truefalsefalsetruetruefalsefalsetrue
truefalsetruefalsetruefalsefalsetrue
truefalsetruefalsetruefalsefalsetrue
falsetruetruefalsefalsetruefalsetrue
falsetruetruefalsefalsetruetruefalse
falsetruetruefalsefalsetruetruefalse
falsetruetruefalsefalsetruetruefalse
truefalsetruefalsetruefalsefalsetrue
truefalsetruefalsefalsefalsefalsetrue
falsetruetruefalsefalsefalsetruefalse
truefalsetruefalsefalsefalsefalsetrue
falsetruetruefalsefalsefalsetruefalse

The "NULL" state of a variable arises when it exists but does not contain anything. This may be more precise than "empty" because the value "0" is a value after all, while "NULL" stands for "nothing". "NULL" also exists as a constant for assigning "nothing". However, it is not possible to replace "is_null ($ value)" with the comparison "$ value == NULL", because comparisons always compare values ​​while the "is" functions determine the data type. If "$ value" contains the number "0", this is converted to "FALSE" for the comparison (Boolean operator) and then compared with "NULL", which internally also corresponds to "FALSE", from which then "0 == NULL "is" TRUE "follows, which probably does not correspond to the expected logic.

The "unset" function can be used to cancel an assignment and thus make a variable disappear.

Display information about a variable

The following three commands can be used to send the content of a variable directly to the web browser. It is advisable to set the output within "

" tags, since all functions use the carriage return "\ n" and do not primarily generate HTML. 

command"TRUE" for the following data type
Generates an output of the variable structure and the content. In the case of objects, the issue is limited to public members.
Generates an output of the variable structure and the content, but the representation is done in such a way that it is PHP code designed in such a way that the original variable could be displayed again. Private members are also issued for objects.
Generates an output of the variable structure and the content in a legible form. Private members are also issued for objects.