PHP Variables and Data Types

11/9/2025

PHP variables and data types with code examples

Go Back

PHP Variables and Data Types – Complete PHP Tutorial for Beginners

In PHP programming, variables and data types are among the most important fundamentals. Before building dynamic pages or processing form data, you must understand how variables work and which data types you can store in PHP. This tutorial explains PHP variables, their rules, and all the major data types used in PHP.


PHP variables and data types with code examples

What is a Variable in PHP?

A variable is a memory storage container that holds data. In PHP, every variable starts with a $ symbol.

Example:

<?php
$name = "John";
$age = 25;
?>

Here:

  • $name is a variable storing text

  • $age is a variable storing a number


Rules for Writing PHP Variables

RuleExplanation
Must start with $Every variable should begin with $ symbol
Cannot start with a number$1name is invalid
Must start with letter or underscore$name or $_data is allowed
Case-sensitive$Data and $data are different

PHP Data Types

PHP supports multiple data types. These help define the type of value stored inside variables.

1. String

Stores text inside quotes.

<?php
$language = "PHP";
?>

2. Integer

Stores whole numbers.

<?php
$year = 2025;
?>

3. Float (Double)

Stores decimal numbers.

<?php
$price = 99.99;
?>

4. Boolean

Can store only true or false.

<?php
$isLoggedIn = true;
?>

5. Array

Stores multiple values in one variable.

<?php
$colors = array("Red", "Green", "Blue");
?>

6. Object

Stores instance of a class.

<?php
class Car {
  public $model = "Toyota";
}
$car = new Car();
?>

7. NULL

Represents an empty variable value.

<?php
$user = null;
?>

Displaying Variable Using echo

You can display stored value using echo keyword.

<?php
$name = "Shubham";
echo $name;
?>

Conclusion

PHP variables and data types are the base of any PHP application. If you understand how to store text, numbers, array data, Boolean values, and objects properly, you can easily move to the next chapters such as operators, conditions, loops, and functions.

Table of content

  • Introduction to Apache Hive
  • Hive Architecture and Components
  • Hive Modes
  • Installation and Setup
    • Installing Hive on Linux/Windows
    • Configuring Hive with Hadoop
    • Verifying the Installation
  • Working with Hive Tables
    • Internal (Managed) Tables
    • External Tables
    • Creating Tables in Hive
    • Altering and Dropping Tables
    • Partitioning in Hive
    • Bucketing in Hive
  • HiveQL Basics
    • SELECT Queries
    • Filtering Data with WHERE
    • Sorting and Grouping Data
    • Using Joins in Hive
  • Advanced Hive Concepts
    • Partition Pruning
    • Dynamic Partitioning
    • Query Optimization in Hive
    • Working with Hive Indexes
    • ACID Transactions in Hive
  • File Formats in Hive
    • Text File
    • ORC (Optimized Row Columnar)
    • Parquet
    • Avro
    • Sequence File
  • Hive Functions
    • Built-in Functions (String, Date, Math)
    • Aggregate Functions
    • User-Defined Functions (UDFs)
  • Integrating Hive with Other Tools
    • Hive and Apache Spark
    • Hive and Pig
    • Hive and HBase
  • Hive Interview Questions
  • Best Practices in Hive
    • Performance Optimization
    • Handling Large Datasets
    • Security and Access Control
  • FAQs and Common Errors in Hive
    • Troubleshooting Hive Issues
    • Frequently Asked Questions
  • Resources and References
    • Official Hive Documentation
    • Recommended Books and Tutorials