Setup Menus in Admin Panel

  • No products in the cart.

  • LOGIN

PHP Data types

The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects.

PHP supports eight primitive data types:

  1. Integer,
  2. Floating point number or Float,
  3. String,
  4. Booleans,
  5. Array,
  6. Object,
  7. resource and
  8. NULL.

These data types are used to construct variables. Now let’s discuss each one of them in detail.

1.PHP Integer

An integer is a whole number (without decimals) that must fit the following rules:-

Rules for integers:

  • An integer must have at least one digit.
  • An integer must not have a decimal point.
  • An integer can be either positive or negative.
  • Integers can be specified in three formats:
  1. decimal (10-based),
  2. hexadecimal (16-based – prefixed with 0x) or
  3. octal (8-based – prefixed with 0)

In the following example $a is an integer. The PHP var_dump() function returns the data type and value:

Example

<?php
$a = 2543;
var_dump($a);
?>

Result:

int(2543)


2.PHP Float

A floating point number or float, is a number that includes a decimal point.

<?php
$x = 13.425;
?>

3.PHP String

string is a sequence of characters, like “Welcome to Phphelp!”
string can be any text within a set of single or double quotes.

<?php
$string1 = "Welcome to Phphelp!"; //double quotes
$string2 = 'Welcome to Phphelp!'; //single quotes
?>

Note:-
You can also join two strings together using dot(.) concatenation operator.
example:echo $string1.$string2

4.PHP Boolean

Boolean represents two possible values   either 1 (true) or 0 (false).

<?php
$x = true; $y = false;
?>

Booleans are often used in conditional testing.Most of the data types can be used in combination with one another. In this example, string and integer are put together to determine the sum of two numbers.

<?php
$int = "5";
$str =50;
$sum = $int + $str;
echo($sum);
?>

Result:

55

 

Note:

PHP automatically converts each variable to the correct data type, according to its value. So, the variable $str is treated as a number in the addition.

5.PHP Arrays

An array is a variable that can hold more than one value at a time. Every PHP developer must know how to use them and how to combine array functions to make code readable and short. An array is useful to aggregate a series of related items together.

Example:

<?php
$books = array("Php programming", "C programming", "java programming"); 
echo "I like " . $books[0] . ", " . $books[1] . " and " . $books[2] . ".";
?>

6.PHP Objects

An object is a data type which stores data and information on how to process that data.

Initially, we must declare a class of object. For this, we use the class keyword.

In PHP, an object must be explicitly declared.

Example
<?php
class Book {
function Book() {
$this->model = "PHP";
}
}

// create an object
$program = new Book();

// show object properties
echo $program->model;
?>

 

Result:

PHP

7.PHP resource

A resource is a special variable, holding a reference to an external resource.

Resource variables typically hold special handlers to opened files and database connections.

 

8.PHP NULL

The special NULL value is used to represent empty variables in PHP.
It identifies variables being empty or not and useful to differentiate between the empty string and null values of databases.
There is only one value of type NULL, and that is the case-insensitive keyword NULL.

Example:

<?php
$value = NULL;
?>

ASSIGNMENT : How data types in PHP are differ from other programming languages?Explain. MARKS : 10  DURATION : 1 week, 3 days

ASSIGNMENT : How many data types are there in PHP?Explain and write example for each. MARKS : 10  DURATION : 1 week, 3 days

SEE ALL Add a note
YOU
Add your Comment
 

Advanced Course Search Widget

Popular Courses

Course Categories

© PHPhelp. 2002 - 2018 All rights reserved.
X