Variables and Data Types
One of the first things to understand when learning any programming language are variables and data types.
Variables
In PHP, variables are declared with a dollar sign $
followed by the variable's name. PHP is a loosely typed language, which means that you don't have to declare the data type of a variable when you create one. Here's an example:
Data Types
PHP supports several data types, which define what kind of data a variable can hold:
String: A sequence of characters, like "Hello, World!". Strings are defined by enclosing the text in quotes:
Integer: A non-decimal number. Integers can be positive or negative:
Float (or double): A number with a decimal point or a number in exponential form:
Boolean: Represents two possible states:
TRUE
orFALSE
:Array: An ordered map (a type of data structure that pairs keys with values):
Object: Instances of classes, which PHP allows you to create so that you can use objects to manage and organize your code (discussed in detail in a later section):
NULL: A special data type representing a variable with no value. A variable of data type NULL is a variable that has no value assigned to it:
Last updated