PHP Variables, Constant, Data Types

  • Post category:Blog / PHP

In this blog you will learn about PHP Variable, Constant and Data Types

Table of Contents

What is variable in PHP? (PHP Variable)

  • Variable is name that can store variable in location and that can store data at run time.
  • Variable visibility is defined by the scope of variable.
  • Global variable in PHP can accessible anywhere in a script within the application.
  • Local variable in PHP can accessible within a script where it is defined.

Rules for PHP variable naming

  • PHP variable always starts with $ sign, that follow name of variable. For Example, $name
  • PHP variable name should start with any letter or with underscore sign.
  • PHP variable name should not start with number (although you can start it with number). Like $1 is not a right way to declare variable.
  • PHP variable name must contain only alphabet, numeric or underscore.
  • PHP variable name is a case-sensitive. $name is not same as $Name.

Example of Variable in PHP

<?php
    $name = "tamim";
    echo $tamim;
?>

Output

tamim

PHP is Loosely type of language!! Why?

If you have the noticed one thing in code written above, we have never declared data type in above code. PHP associates a data type automatically to the variable. That is what makes PHP loosely type of language. As we have not added data type while declaring variable we can store any value in variable.

What is constant in PHP? (PHP Constant which is similar to PHP Variable but not same)

Constant is just like PHP variable only difference is that we can’t change the value of constant on run time. Constant can be declared using define keyword. We don’t need to use $ sign while using constant

Example of Constant in PHP

<?php
     define("NAME","TAMIM");
     echo NAME;
?>

PHP Data Types

PHP Variables is able to store data of different types, and different data types is able do different things. A Data type is the categorization of data into a category according to their attributes

PHP Variables supported data types is mentioned below

  • String
  • Integer
  • Float (floating point numbers – also called double)
  • Boolean
  • Array
  • Object
  • NULL
  • Resource

Alpha and numeric characters are classified as strings

Whole numbers(like 123) are classified integers

Numbers with decimal points(Like 3.14) are classified as floating points.

True or false values are classified as Boolean.