Php Data Types

Data Types in Php

PHP supports the following data types:

  • String (Ex- Techalltype)
  • Integer (Ex- 786)
  • Float  (Ex- 786.50)
  • Boolean (Ex- True, False)
  • Array
  • Object
  • NULL
  • Resource

 

 

  • Php as String :-

A string is basically collection of characters, which make meaningful sense, in php string strings are defined in quotes. Lets see with an example:-

<?php
$x = “Hello Techalltype”;
$tech = ‘Hello Techalltype’;

echo $x;
echo “<br>”;
echo $tech ;
?>

Ouput:-

Hello Techalltype   // display value of string stored in $x variable.

Hello Techalltype  // display value of string stored in $tech variable.

General rules for declaring String:-

  1. Use of Alphabets (Ex – techalltype).
  2. Alphabets Number Alphabets (Ex- tech123alltype).
  3. Alphabets underscore Alphabets (Ex- Tech_alltype).
  4. Underscore Alphabets (Ex- _techalltype).
  5. Not start with Numbers (Ex- 123techalltype).
  • Php as Integer :-

Lets show with an example :-

<?php
$x = 786;
var_dump($x);  //  var_dump function is used to know variable types here it is an integer
?>

Output:-

int(786)

Basic of Integer

  1. In general terms integer are the numbers.
  2. It should be minimum  1 in numbers.
  3. Doesn’t include any alphabets.
  4. Numbers can be positive or negative

Important points related to Integer and string

  1. Generally php understand that the variable values  assigned in single/double quotes is a string. (Ex – $tech= “Tech all Type”;).
  2. Integer values are assigned without any quotes. (Ex- $techalltype = 100;).

Note:- If any integer is assigned in quotes(Single/Double) php understand as string, and if any string is assignd without quotes(Single/Double) it will act as integer.

  • Php as Float:-

Float is just like integer with decimal point.

Lets see with an example.

<?php
$x = 786.123;
var_dump($x);   //  var_dump function is used to know variable types here it is an Float because it consist of decimal point
?>

Output:-

float(786.123)

  • Php as Boolean:-

A Boolean used to check possible states: TRUE or FALSE. It is used in testing condition.

Let’s see with an example:-

<?php
$x = true;
echo $x;
?>

Output:-

1

I am Hitesh from Jamshedpur (India). I have been working as a Web Application Developer from last 4+ years. I love diverse and attention catching web presence for a variety of users. Also I love to learn new things in Web Development.

Tags: , , , , , , , , ,