| Start |
|---|
| Type Comparison There are two types of comparison (opens new window): loose comparison with You can also use strong comparison to check if type and value don't match using A typical example where the |
| Boolean Boolean (opens new window) is a type, having two values, denoted as This code sets the value of
Booleans can be used in if statements like this: Due to the fact that PHP is weakly typed, if
Any other value results in To avoid this loose comparison, you can enforce strong comparison using To convert a type into boolean, you can use the or call the Boolean conversion to a string (note that Boolean conversion to an integer: Note that the opposite is also possible: Also all non-zero will return true: |
Float For historical reasons "double" is returned by Floats are floating point numbers, which allow more output precision than plain integers. Floats and integers can be used together due to PHP's loose casting of variable types: php does not show float as float number like other languages, for example: WarningFloating point precision (From the PHP manual page (opens new window)) Floating point numbers have limited precision. Although it depends on the system, PHP typically give a maximum relative error due to rounding in the order of 1.11e-16. Non elementary arithmetic operations may give larger errors, and error propagation must be considered when several operations are compounded. Additionally, rational numbers that are exactly representable as floating point numbers in base 10, like 0.1 or 0.7, do not have an exact representation as floating point numbers in base 2 (binary), which is used internally, no matter the size of the mantissa. Hence, they cannot be converted into their internal binary counterparts without a small loss of precision. This can lead to confusing results: for example, floor((0.1+0.7)*10) will usually return 7 instead of the expected 8, since the internal representation will be something like 7.9999999999999991118.... So never trust floating number results to the last digit, and do not compare floating point numbers directly for equality. If higher precision is necessary, the arbitrary precision math functions and gmp functions are available. |
| Strings A string in PHP is a series of single-byte characters (i.e. there is no native Unicode support) that can be specified in four ways: Single QuotedDisplays things almost completely "as is". Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, one can escape it with a back slash ', and to display a back slash, one can escape it with another backslash \ Double QuotedUnlike a single-quoted string, simple variable names and escape sequences (opens new window) in the strings will be evaluated. Curly braces (as in the last example) can be used to isolate complex variable names. HeredocIn a heredoc string, variable names and escape sequences are parsed in a similar manner to double-quoted strings, though braces are not available for complex variable names. The start of the string is delimited by NowdocA nowdoc string is like the single-quoted version of heredoc, although not even the most basic escape sequences are evaluated. The identifier at the beginning of the string is wrapped in single quotes. |
| Callable Callables are anything which can be called as a callback. Things that can be termed a "callback" are as follows: Callbacks can be denoted by |
| Null PHP represents "no value" with the Setting the variable to null: Checking if the variable was set to null: Null vs undefined variableIf the variable was not defined or was unset then any tests against the null will be successful but they will also generate a Therefore undefined values must be checked with |
| Type Casting PHP will generally correctly guess the data type you intend to use from the context it's used in, however sometimes it is useful to manually force a type. This can be accomplished by prefixing the declaration with the name of the required type in parenthesis: But be carefull: not all type casts work as one might expect: |
| Resources A resource (opens new window) is a special type of variable that references an external resource, such as a file, socket, stream, document, or connection. There are different (sub-)types of resource. You can check the resource type using You can find a complete list of built-in resource types here (opens new window). |
| Type Juggling PHP is a weakly-typed language. It does not require explicit declaration of data types. The context in which the variable is used determines its data type; conversion is done automatically: |
| Integers Integers in PHP can be natively specified in base 2 (binary), base 8 (octal), base 10 (decimal), or base 16 (hexadecimal.) Integers are 32 or 64 bits long, depending on the platform. The constant Integer values are automatically created as needed from floats, booleans, and strings. If an explicit typecast is needed, it can be done with the Integer overflow will be handled by conversion to a float: There is no integer division operator in PHP, but it can be simulated using an implicit cast, which always 'rounds' by just discarding the float-part. As of PHP version 7, an integer division function was added. (Note that the extra parentheses around |
Read Article
https://develop.horje.com/learn/1434/reference