<<

PHP Data Types

Class - 4

Powered by Axixa Technologies What is Data Type?

1) Data Types defines the type of data a variable can store. 2) Data types specify the size and type of values that can be stored. 3) Variable does not need to be declared its data type adding a value to it. PHP is a Loosely Typed Language so here no need to define data type. 4) To check only data type use gettype( ) function. 5) To check value, data type and size use var_dump( ) function. Example:

$str hold value="Hello" (contain string value length 5). List of Data Type in PHP

PHP supports ten primitive types in Three group.

a) Four scalar(It holds only single value) types : 1. Boolean 2. Integer 3. Float (floating-point number, aka double) 4. String

b) Four compound(Multiple values in single variable) types: 5. Array 6. Object 7. Callable 8. Iterable

) Two special types:

9. Resource 10. NULL

$show_error = True; a.1) Booleans var_dump($show_error);

This is the simplest type. Like a switch that has only two states ?> ON means true(1) and OFF means false(0) Output:

A boolean expresses a truth bool(true) value. It can be either TRUE or FALSE. octal number (equivalent to 83 decimal) The size of an integer is platform-dependent. $a = 0x1A; // number (equivalent to 26 decimal) Integers hold only whole numbers including positive and negative $a = 0b11111111; // binary number (equivalent to 255 decimal) numbers, i.e., numbers without fractional part or decimal point. They $a = 1_234_567; // decimal number (as of PHP 7.4.0) can be decimal (base 10) default, octal (base 8) or hexadecimal (base 16) or ?> binary (base 2). The binary integers ……………………………………………………………………………………………………………………….. can be declared with leading 0b and PHP has the following functions to check if the type of a variable is the octal integers can be declared with integer: leading 0 and the hexadecimal can be declared with leading 0x. is_int() As of PHP 7.4.0, integer literals may is_integer() - alias of is_int() contain underscores (_) between is_long() - alias of is_int() digits, for better readability of literals. These underscores are removed by PHP's scanner.

$c = 7E-10; It is also called numeric data types.

$ = 1_234.567; // as of PHP 7.4.0 A number with a fractional ?> component. …………………………………………………………………………………………………………………….. The size of a float is PHP has the following functions to check if the type of a variable is platform-dependent, although a float: maximum of approximately 1.8e308 with a precision of roughly 14 is_float() decimal digits is a common value is_double() - alias of is_float() (the 64 bit IEEE format). a.4) Strings

A string is series of characters, where a character is the same as a byte. This means that PHP only supports a 256-character set. Non numeric data type String can hold letters,numbers and special characters. String value must be enclosed eighter in single quotes or double quotes. The string in PHP is implemented as an array of bytes and an integer indicating the length of the buffer. As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 2GB (2147483647 bytes maximum). A can be specified in four different ways: I. Single quoted II. Double quoted III. Heredoc syntax IV. Nowdoc syntax (since PHP 5.3.0) a.4.I) Single quoted

To specify a literal single quote, escape echo 'This will not expand: \n a newline'; it with a (\). To specify a //Outputs: This will not expand: \n a newline literal backslash, double it (\\). All other instances of backslash will be treated echo 'Variables do not $expand $either'; as a literal backslash: this means that //Outputs: Variables do not $expand $either the other escape sequences you might ?> be used to, such as \r or \n, will be ……………………………………………………………………………………………………………………….. output literally as specified rather than Widely used Escape Sequences in PHP having any special meaning. \' – To escape ' within single quoted string. \” – To escape “ within double quoted string. Variables and escape sequences for \\ – To escape the backslash. special characters will not be expanded \$ – To escape $. when they occur in single quoted \n – To add line breaks between string. strings. \t – To add tab space. \v - To add vertical tab space. \r – For carriage return.

Another way to specify a echo “This will not expand: \n a newline”; string is to enclose it in /* double quotes (the character Outputs: '). This will not expand: a newline */ Variables and escape sequences for special $name=”Rajesh”; characters will expanded. echo “I am $name from Axixa”; //Outputs: Iam ?> a.4.III) Heredoc(Strings) classic single quote, double quotes ………………………………………………………………………………………………………………………… . Top tokens for heredoc The most common token is EOT, by far. It Token is supposed to mean ‘End Of Text’, which EOT is always rather strange as an opening EOF . Of course, it is much more EOD useful at the end of the string. HTML It is very important to note that the line SQL with the closing identifier must contain END no other characters, except a semicolon ICS XML (;). ‘EOF’ EOS STRING a.4.IV) Nowdoc (Strings)

A nowdoc is identified with the same <<< sequence used for heredocs, but /*Output: the identifier which follows is enclosed Example of string in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply spanning multiple lines to nowdoc identifiers, especially those regarding the appearance of the using Nowdoc syntax.*/ closing identifier. ?> NULL

The special NULL value represents a variable with no value. NULL is the only possible value of type null. A variable is considered to be null if: 1) It has been assigned the constant NULL. 2) It has not been set to any value yet. 3) It has been unset(). See also the functions is_null() and unset(). Casting to NULL: This feature has been DEPRECATED as of PHP 7.2.0. Relying on this feature is highly discouraged. Casting a variable to null using (unset) $var will not remove the variable or unset its value. It will only return a NULL value. Thanks!

Contact us:

AXIXA Technologies Bikaner (Raj.) +91 9251435299 (Rajesh) [email protected] www.axixatechnologies.com

Axixa youtube link