Friday, January 5, 2018

PHP data types with example

Data types

All data stored in PHP variables fall into one of eight basic categories, known as data types. A variable’s data type determines what operations can be carried out on the variable’s data, as well as the amount of memory needed to hold the data.
         The PHP language supports eight data types. Five of these data types are scalar. The remaining three, however, has unique properties that differentiate them from others. As mentioned earlier, a PHP variable can hold any data type. Keep in mind that PHP variables behave according to the type of data they hold. Here are the data types that you will encounter while using PHP.
Data type variable 

      PHP provides total eight types of values, or data types:-
They are decided in three categories in data types:- 

1. scalar data type 
2. Compound collection data types 
3. Special data types 

scalar (single-value) types: 

1. integers.
2. floating-point numbers.
3. strings.
4. Booleans.

compound collection data types :

5. arrays.
6. objects.

The remaining two are special types: 

7. resource.
8. NULL. 

1.   Integer types :- 

Integers are whole numbers, such as 1, 12, and 256. The range ofacceptable values varies according to the details of your platform but typically extends from −2,147,483,648 to +2,147,483,647. Specifically, the range is equivalent to the range of the long data type of your C compiler.
          Unfortunately, the C standard doesn’t specify what range that long type should have, so on some systems you might see a different integer range. Integer literals can be written in decimal, octal, binary, or hexadecimal. Decimal values are represented by a sequence of digits, without leading zeros. The sequence may begin with a plus ( + ) or minus ( − ) sign.
      If there is no sign, positive is assumed. Examples of decimal integers include the following
    Octal numbers consist of a leading 0 and a sequence of digits from 0.

2. Floating- point numbers types :- 

A floating-point number, also called real number, is PHP's own version of the C language's “double” type. 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. In general, a floating-point value can have an exponent, a decimal point, and “+” or “-” sign. The list given below shows some valid floating- point numbers:
+ 0.8e-3

3. String types : -

In PHP, strings are sequences of characters (i.e. letters and numbers) that are null-terminated all the time. Unlike other computer languages, however, PHP remembers the length of a string internally rather than relying on the null terminator. This approach allows the PHP interpreter to handle binary information easily. For instance, you can use this function to generate images on-the-fly and send it to a web browser. The largest size a string can have depends on the C compiler and computer that you're using. However, you can be sure it can handle 2GB of data without any problems.

 Important Note: Don't create programs that verify the limit discussed above. You will likely reach the memory limit of your platform first.

While adding a string value to your PHP code, you may use single quotes, double quotes, or here-docs. Let's discuss these options in detail:

Single Quotes:- 
              This is the simplest option that you can use when creating a string. Write your string between a pair of quotes. This approach supports two types of escape characters, which are: \' and \\. You will learn about escape characters later.

Double Quotes:-
                 This option is more complex than the previous one. You can use this to hold any character. However, when working with a special symbol, you need to use the backslash character. Adding a backslash before a special character is called “escaping.”Here are the escape characters that you will encounter while using PHP:

◆ \n –Use this to add a newline character to your codes. A newline character is the character you'll get after pressing the Enter key.

◆ \t –With this, you can add a tab character to your PHP codes.

◆ \\ - You can use this symbol to add a backslash character to your scripts. As you can see, the backslash character is used to activate the special functions of other characters. Thus, you need to type two backslash characters in order to get an ordinary backslash.

◆ \”- Double quotes are used to enclose strings. To add a normal double quote character, you need to introduce it using a backslash.

◆ \r –This escape symbol allows you to insert a line feed to your PHP script.

◆ \$ - Use this symbol to add a normal dollar sign to your codes. As you know, $ is used to create a PHP variable.

◆ \0 –With this symbol, you can add the 0 character of ASCII. \{ octal number} –Use this to add an ASCII character to your code. Here, you should use the octal representation of the character you want to add.

◆ \x{ hexadecimal number} –This escape character is almost identical to the octal one. The only difference is that this escape symbol requires the hexadecimal representation of the character.

4. Booleans types :-

A Boolean value represents a “truth value”—it says whether something is true or not. Like most programming languages, PHP defines some values as true and others as false. Truth and falseness determine the outcome of conditional code such as:

if ( $alive ) { ... } 

In PHP, the following values all evaluate to false :

• The keyword.
• false The integer 0.
• The floating-point value 0.0.
• The empty string ( "" ) and the string "0" .
• An array with zero elements.
• The NULL value.

5. Object types :- 

PHP also supports object-oriented programming (OOP). OOP promotes clean modular design, simplifies debugging and maintenance, and assists with code reuse.

           Classes are the building blocks of object-oriented design. A class is a definition of a structure that contains properties (variables) andmethods (functions). Classes are defined with the class keyword:

 class Person 

public $name = '' ; 
function name ( $newname = NULL ) 

if ( ! is_null ( $newname )) 

$this -> name = $newname ; 

return $this -> name ; 


6. Resources types :- 

Many modules provide several functions for dealing with the outside world. For example, every database extension has at least afunction to connect to the database, a function to send a query to the database, and a function to close the connection to the database. Because you can have multiple database connections open at once, the connect function gives you something by which to identify that unique connection when you call the query and close functions a resource (or a “handle”).

Example :-

7. Null types :- 

There’s only one value of the NULL data type. That value is available through the case-insensitive keyword NULL . The NULL value represents a variable that has no value (similar to Perl’s undef or Python’s None ):

$aleph = "beta" ; 
$aleph = null ; 
                    // variable's value is gone 
$aleph = Null ; // same 
$aleph = NULL ; // same 

Use the is_null() function to test whether a value is NULL —for instance, to see whether a variable has a value:

 if ( is_null ( $x )) in

// $x is NULL

8. Array types :- 

An array holds a group of values, which you can identify by position (a number, with zero being the first position) or some identifying name (a string), called an associative index:

$person [ 0 ] = "Smith" ;
$person [ 1 ] = "Jhon" ;
$person [ 2 ] = "Robin" ;

$creator [ 'Light bulb' ] = "Smith" ;
$creator [ 'Rotary Engine' ] = "Jhon" ; $creator [ 'Toilet' ] = "Robin" ;

The array() construct creates an array. Here are two examples:

$person = array ( "Smith" , "Jhon" , "Robin" ); 
$creator = array ( 'Light bulb'  "Smith" ,                                          'Rotary Engine' 
"Jhon" , 
"Robin" );

There are several ways to loop through arrays, but the most common is a foreach loop:-

foreach ( $person as $name ) { 
echo "Hello, { $name } \n " ; 

foreach ( $creator as $invention => $inventor ) { 
echo " { $inventor } created the { $invention } \n " ;

The output of this array is display are computer pages:-

Hello , Smith
Hello , Jhon 
Hello , Robin
Smith created the Light bulb 
Jhon created the Rotary Engine 
Robin created the Toilet. READ MORE


Please comment and follow this site blog inbox