3. Variables and Data Types
Declaring Variables
In PHP, a variable is a container used to store data that can change during the execution of a script. All variables in PHP start with a dollar sign ($
) followed by the variable name. A variable name can include letters, numbers, and underscores, but it must start with a letter or an underscore.
Here’s how you declare a variable:
<?php
$name = "John"; // String variable
$age = 25; // Integer variable
$isStudent = true; // Boolean variable
?>
In the example above:
-
$name
is a variable that stores a string value. -
$age
is an integer variable. -
$isStudent
is a boolean variable (eithertrue
orfalse
).
PHP Data Types
PHP supports several types of data. The most common ones are:
-
String: A sequence of characters enclosed in quotes (either single
'
or double"
).- Example:
$name = "John Doe";
- Example:
-
Integer: A whole number (positive or negative).
- Example:
$age = 30;
- Example:
-
Float (or Double): A number with a decimal point.
- Example:
$price = 19.99;
- Example:
-
Boolean: A true or false value.
- Example:
$isValid = true;
- Example:
-
Array: A collection of values. You can store multiple items in a single variable.
- Example:
$colors = array("Red", "Green", "Blue");
- Example:
-
Object: A data structure that can contain both data and functions (methods). We will explore objects in later sections when we dive into object-oriented programming.
-
NULL: A special data type that represents a variable with no value.
- Example:
$emptyVar = NULL;
- Example:
Variable Naming Conventions
While naming variables, there are a few rules and conventions to keep in mind:
- A variable name must start with a letter or underscore (
_
), followed by letters, numbers, or underscores. - Variable names are case-sensitive. For example,
$Name
and$name
are different variables. - It's good practice to use descriptive names for your variables to make the code easier to understand.
Example of good variable names:
<?php
$firstName = "John";
$lastName = "Doe";
$isStudent = true;
?>
Avoid using vague names like:
<?php
$x = "John";
$y = 25;
?>
Type Casting and Type Juggling
PHP automatically converts (or "casts") data from one type to another when necessary. This is called type juggling. For example, if you try to add a string and an integer, PHP will convert the string to a number if possible.
Example of type juggling:
<?php
$num = "5"; // string
$sum = $num + 10; // $num is automatically converted to an integer
echo $sum; // Outputs: 15
?>
However, if you need to explicitly convert a variable from one type to another, you can use type casting:
<?php
$str = "123.45";
$int = (int) $str; // Cast string to integer
echo $int; // Outputs: 123
?>
Constants
In PHP, constants are variables that cannot be changed once they are defined. You define a constant using the define()
function.
Example:
<?php
define("PI", 3.14159);
echo PI; // Outputs: 3.14159
?>
Unlike variables, constants do not start with a dollar sign ($
) and are case-sensitive by default.
Summary of Variables and Data Types:
- PHP variables start with
$
and can hold different types of data like strings, integers, floats, booleans, arrays, and objects. - PHP supports automatic type conversion (type juggling), but you can also explicitly cast data types.
- Constants are similar to variables but cannot be changed once defined.