Back to Blog
Lesson 3 of the PHP: Modern PHP from the Ground Up course
PHPJuly 14, 20264 min read

PHP Variables and Data Types: A Beginner's Guide

Master PHP variables and data types. Learn how to declare variables, assign values, and leverage dynamic typing to build robust, server-side applications.

PHPWeb DevelopmentProgramming BasicsBackendVariables

Previously in this course, we explored PHP syntax and browser output to understand how the server executes code and sends HTML to the client. Now that you can display static text, it's time to make your application dynamic by storing and manipulating information.

Understanding PHP Variables

In programming, a variable is essentially a named container used to store information that your application can reference or change later.

In PHP, all variables start with a dollar sign ($). This is a unique identifier that tells the engine, "everything following this sign is a variable name."

Declaring and Assigning Variables

You don't need to declare a variable type in advance because PHP uses dynamic typing. You simply assign a value using the assignment operator (=), and PHP automatically determines the variable's type based on that value.

PHP
<?php
$username = "Alice"; #6A9955">// String
$age = 25;           #6A9955">// Integer
$price = 19.99;      #6A9955">// Float
$isActive = true;    #6A9955">// Boolean
?>

A few rules to remember:

  1. Variable names are case-sensitive ($User and $user are different).
  2. They must start with a letter or an underscore, followed by letters, numbers, or underscores.
  3. They cannot contain spaces or special characters.

Core Scalar Data Types

PHP handles data through several types. As you build your MVC application, you will primarily work with these four scalar types:

Data TypeDescriptionExample
StringA sequence of characters"Hello World"
IntegerWhole numbers (positive/negative)42
FloatNumbers with decimal points3.14
BooleanLogical values (true or false)true

Working with Strings

Strings are sequences of characters wrapped in either single quotes (') or double quotes ("). Double quotes allow for "variable interpolation," meaning you can insert a variable directly into the string.

PHP
$name = "Developer";
echo "Hello, $name!"; #6A9955">// Outputs: Hello, Developer!
echo 'Hello, $name!'; #6A9955">// Outputs: Hello, $name! (literal)

Integers and Floats

Integers are standard whole numbers. Floats (also known as doubles or real numbers) are for precision. PHP is flexible; if you perform math on an integer, it may automatically convert it to a float if the result requires decimal precision.

Booleans

Booleans are the backbone of your control flow. They represent the truth value of an expression. You will use these extensively when we eventually cover conditional logic—for example, checking if a user is logged in or if a form submission is valid.

Identifying Data Types

Since PHP is dynamically typed, you might sometimes wonder what is inside a variable. You can use the gettype() function to inspect the type or var_dump() to see both the type and the value.

PHP
$count = 10;
echo gettype($count); #6A9955">// Outputs: integer

$price = 10.50;
var_dump($price);     #6A9955">// Outputs: float(10.5)

Hands-on Exercise: Building a User Profile

Let's advance our running project by creating a simple "Profile" logic. Create a file named profile.php and define variables for a user's details.

Exercise:

  1. Declare a variable $userName with a string value.
  2. Declare $userAge as an integer.
  3. Declare $isMember as a boolean.
  4. Use echo to print a message like: "User [Name] is [Age] years old. Membership status: [true/false]."

Tip: When echoing a boolean, PHP converts true to 1 and false to an empty string. You may want to use var_export($isMember) to see the actual boolean representation.

Common Pitfalls

  • Forgetting the $ sign: This is the most common error for beginners coming from other languages. Always start with $.
  • Case Sensitivity: Expect bugs if you define $product and try to access $Product.
  • String interpolation confusion: Beginners often use single quotes when they want to inject a variable into a string. Remember: double quotes for interpolation, single quotes for literal strings.
  • Implicit conversion: Be aware that adding a string "10" and an integer 5 will result in 15 because PHP tries to be helpful by converting types. While convenient, this can lead to logic errors in larger applications.

Frequently Asked Questions

Q: Can I change the data type of a variable after I've assigned it? A: Yes. Because of dynamic typing, you can assign an integer to a variable and later assign a string to that same variable. While allowed, it is generally considered poor practice for readability.

Q: What happens if I use a variable I haven't defined? A: PHP will throw a "Warning: Undefined variable" and treat the value as null. Always ensure your variables are initialized before use.

Recap

We've covered the basics of storing data in PHP. You now know how to declare variables using the $ prefix, identify the four main scalar data types, and use var_dump to debug your values. These variables are the bricks we will use to build our models and controllers in the upcoming lessons.

Up next: Strong Typing and Scalar Type Hinting

Similar Posts