Data Types and Variable in PHP

Data means information and playing with data is the main job of a computer programmer. As a PHP programmer, you have to know how many data types are used in PHP programming. There are 8 kinds of data in PHP language and this article will explain these data types now. But before starting feel free to study how to do first program in PHP discussed in previous article because it will help you to learn PHP variable and data types so easily.

Data Types and Variable in PHP

Data Types and Variable in PHP

PHP plays with 8 kinds of data.  These are…

  •  Integer: Real number without decimals. Such as 1, 5, 100, 1000 and so on.
  •  Float: Real number with decimals. Such as 35.5, 100.10 and so on.
  •  String: Sum of several characters that means a word, a sentence or several sentences.
  •  Boolean: Either true or false value. True represents 1 and false represents 0.
  •  NULL: This is a special data type in PHP which has no data value.
  •  Array: Various types of data are kept within this type with index value.
  • Resource: These types of data come from database.
  • Object:This type of data is used in object oriented programming. Since PHP can be used as object oriented programming language, this is one kind of data type in PHP.

Variable in PHP

Variable is a container of data. You can assign a data value within a variable and use it when you need.

How to declare variable in PHP

In PHP programming, variable name starts with a dollar ($) sign. Any name start with dollar sign is considered as a variable in PHP coding.

Example: $variable_name

Rules to declare variable

  • Variable must start with character or underscore ( _ ) but not with number.
  • There will be no space between variable names.
  • Variable should not start with operator like (+, -, *, / or . ).
  • It is best practice to write variable with small letter. Generally Capital letter is used to declare Constant.

You should always keep in mind that you must assign a data value when you declare a variable in PHP programming otherwise you will get an error. If you do not want to assign a value when you declare a variable, you can assign NULL data which will make your code error free.

Example of valid variable declaration

  • $num=20                                                                              ;   Integer value
  •  $float_num=50.5                                                               ;   Float value
  •  $name=”Mahbub”                                                              ;   String value
  •  $_string_value=”This is string declaration.”               ;   String value
  • $status=true                                                                         ;   Boolean value
  •  $null_value=NULL                                                            ;   NULL declaration

PHP is enough intelligent to understand data types. You do not need to declare data types explicitly like C language or Java language.

Example of invalid variable declaration

  • $1num=10                                         ; Because variable name start with number
  •  $first name =”Mahbub”                ; Space is not allowed between variable names
  •  $last+name=”Hasan”                    ; Operator is not allowed in variable declaration

 Example of variable use in PHP coding

Data Types in PHP

In the above program, I have just assigned data into the variables. Then I print those data with the echo function. Notice at line 9 where I have used dot (.) operator to add string value and data of a variable together.

In this article, I have explained PHP variable and data types from very beginning. I hope you are now able to use variable and data types accurately in your program. If you face any problem, feel free to contact with me from Contact us page. I’ll try my best to stay with you. In next article, I will explain Operators and Expression used in PHP programming from very beginning. Spend some times to study that article and clear your programming concept about Operator and Expression in PHP.

One Response
  1. True Religion Jeans

Leave a Comment

Your email address will not be published. Required fields are marked *