PHP Guide. Everything You Need To Know About PHP Datatypes And Variables

Posted by TotalDC

Now that we looked at what PHP is. Let’s start diving deeper into it and talk about PHP datatypes and variables.

Data Types in PHP

PHP doesn’t have explicit defined data types. PHP determines the data type by analyzing the attributes of given data. PHP supports following data types:

  • Integer – whole numbers. In example -2, 0, 3, 58.
  • Floating point number – decimal numbers in example 3.14. They are also known as double or real numbers.
  • Character string – In example Hello World.
  • Boolean – true or false statements.
  • Array – named and indexed collections of other values.
  • Object – instances of programmer-defined classes, which can package up both other kinds of values and functions that are specific to the class.

Variables in PHP

Variables in PHP like in other programming languages like JavaScript are containers which hold reusable data. It’s basic unit of storage in program. In PHP variable values can change over the course of a script. Worth noticing that in PHP variables does not need to be declared before adding a value to it and PHP automatically converts the variable to the correct data type depending on its value. All variables can be reused throughout the code. And like in JavaScript we use = to assign value to a variable.

Variable Naming Rules in PHP

  • All variables in PHP start with a $ sign, followed by the name of the variable.
  • A variable name must start with a letter or the underscore character _.
  • A variable name cannot start with a number.
  • A variable name in PHP can only contain alpha-numeric characters and    underscores (A-z0-9, and _).
  • A variable name cannot contain spaces.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: