PHP Data Types
PHP Tutorial

PHP Data Types

 

PHP DATA TYPES

In programming, if we want to use values(like 10,20) then we have to use data type(int, float, char) to represent its base type as real, integer, character, or Boolean that determines the possible values for that type. We can perform an action on values of that type. The meaning of data and the way values of that can be stored.

PHP supports eight primitive data types.

Four primitive types

  1. Boolean(true and false)
  2. Integer(3,5)
  3. Float(3.56,6.78)
  4. String(“xcnotes”)

Two compound types

  1. Array
  2. Object

finally two special types

  1. resource
  2. NULL

VARIABLE

A Variable is a temporary storage area for storing value given by the user or program. variable’s type depends on data types followed by programming language. In PHP $ sign represent a variable to store value and work with it.

Example:

<?php

$a=100;

echo $a;

?>

In PHP using any variable, we don’t need to the declaration of it(use $a,$b), because in PHP variables get the data type when we assign any value. In the above example, the integer variable is automatically defined after getting the same type value.

Naming conventions for PHP variables


There are the following rules for naming a PHP variable:

  1. All Variables in PHP start with a $ sign, followed by the name of the variable($a).
  2. A variable name must start with a letter or the underscore character “_”.
  3. A variable name can not start with a number(like $1a).
  4. A variable name in PHP can only contain alpha-numeric characters and underscores (A-Z, 0-9).
  5. A variable name can not contain spaces(like $first number).

Note: $ sign must be the first character in declaring the name of a variable using PHP.

Note: Variables in PHP are case sensitive so be careful when using $x or $X both are not the same name of a variable.

Float(decimal values like 45.67)

<?php

$many = 2.2888800;

$many_2 = 2.2111200;

$few = $many + $many_2;

print( $few);

echo “<br>”;

echo $few;

?>

Double data type in PHP

Boolean(true or false)

<?php

$true_value = true;

$false_value = false;

Print (“true_value = ” . $true_value);

Print (” false_value = ” . $false_value);

?>

String(sequence of character like”xcnotes”)

<?php

$variable = “xcnotes”;

$literally = “My $variable will not print!”;

print($literally);

$literally = “My $variable will print!\n”;

print($literally);

?>

String data type in PHP

Two variables

<?php

$first_number = 10;

$direct_text = ‘Xcnotes ‘;

print($direct_text . $first_number);

?>

Variable in PHP

Array(Store multiple values in one variable)

<?php

 $array = array(“name”=>”Toyota”,”type”=>”Celica”,”colour”=>”black”,”manufactured”=>”1991″);

 $array2 = array(“Toyota”,”Celica”,”black”,”1991″);

 $array3 = array(“name”=>”Toyota”,”Celica”,”colour”=>”black”,”1991″);

 print_r($array);

echo'<br>’;

 print_r($array2);

echo'<br>’;

 print_r($array3);

?>

Array in PHP

Recommended Posts

C++ Programming

Visibility modes in C++

In C++, visibility modes refer to the accessibility of class members (such as variables and functions) from different parts of a program. C++ provides three visibility modes: public, private, and protected. These modes control the access levels of class members concerning the outside world and derived classes. Public: Members declared as public are accessible from […]

Rekha Setia 
C++ Programming

Inheritance in C++

In C++, inheritance is a fundamental concept of object-oriented programming (OOP) that allows you to create a new class based on an existing class, known as the base or parent class. The new class is called the derived or child class. Inheritance facilitates code reuse and supports the creation of a hierarchy of classes. There […]

Rekha Setia 
C++ Programming

C++ Classes and Objects

In C++, classes and objects are fundamental concepts that support object-oriented programming (OOP). Here’s a brief overview of classes and objects in C++: Classes: In C++, a class is a user-defined data type that allows you to encapsulate data members and member functions into a single unit. Classes are the building blocks of object-oriented programming […]

Rekha Setia 

Leave A Comment