Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • PHP Tutorial
  • PHP Exercises
  • PHP Array
  • PHP String
  • PHP Calendar
  • PHP Filesystem
  • PHP Math
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP GMP
  • PHP IntlChar
  • PHP Image Processing
  • PHP DsSet
  • PHP DsMap
  • PHP Formatter
  • Web Technology
Open In App
Next Article:
PHP Variables
Next article icon

Variables and Datatypes in PHP

Last Updated : 10 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A variable in PHP is a container used to store data. The value of a variable can change during program execution. PHP variables start with a dollar sign ($), followed by the variable name.

$name = "GFG";
$age = 30;

Declaring Variables in PHP

To declare a variable, you simply use the dollar sign followed by the variable's name and assign it a value.

PHP
$variableName = "value"; 

Rules for Naming Variables

There are a few rules when naming variables in PHP:

  • Variable names must start with a dollar sign ($), followed by a letter or an underscore.
  • The rest of the variable name can contain letters, numbers, and underscores.
  • Variable names are case-sensitive.
  • For example, $name and $Name are different variables.

Variable Scope

PHP variables have different scopes depending on where they are declared:

  • Global scope: Variables declared outside of functions are accessible throughout the script.
  • Local scope: Variables declared inside a function are only accessible within that function.
  • Static scope: Variables declared as static within a function retain their value across multiple function calls.
PHP
//Driver Code Starts{ <?php //Driver Code Ends }    $globalVar = "I am global";    function test() {     $localVar = "I am local";     echo $globalVar;  // This will cause an error because $globalVar is not in the local scope   }    test();  //Driver Code Starts{ ?>  //Driver Code Ends } 

For more details read the article – PHP Variables

Datatype in PHP

In PHP, data types refer to the type of data a variable can hold. PHP supports several data types, including scalar types (integers, floats, booleans, and strings) and compound types (arrays and objects).

1. String

A string is a sequence of characters enclosed in either single quotes (') or double quotes ("). Strings can store text or a combination of text and numbers.

<?php
$name = "Anjali"; // String with double quotes
$greeting = 'Hello, World!'; // String with single quotes
?>

2. Integer

An integer is a whole number without a decimal point. It can be positive or negative. PHP supports both decimal and octal numbers.

<?php
$age = 30; // Decimal integer
$negative = -10; // Negative integer
?>

3. Float (Double)

A float (also known as a double or real number) is a number with a decimal point.

<?php
$price = 19.99; // Float
$temperature = -5.5; // Negative float
?>

4. Boolean

A boolean represents two possible values: true or false. It is used in logical operations and control structures.

<?php
$isStudent = true; // Boolean value true
$isAdult = false; // Boolean value false
?>

5. Array

An array is a collection of values stored in a single variable. PHP supports both indexed arrays (numeric keys) and associative arrays (string keys).

<?php
// Indexed array
$colors = array("red", "green", "blue");
// Associative array
$person = array("name" => "Anjali", "age" => 25);
?>

6. Object

An object is an instance of a class. Objects can hold both data and methods to manipulate that data. PHP is an object-oriented language, and objects are central to working with classes.

<?php
class Person {
public $name;
public $age;

function __construct($name, $age) {
$this->name = $name;
$this->age = $age;
}

function greet() {
return "Hello, " . $this->name;
}
}

$person1 = new Person("Anjali", 30);
echo $person1->greet(); // Output: Hello, Anjali
?>

7. NULL

The NULL data type represents a variable with no value assigned. A variable is considered NULL if it has been explicitly set to NULL or if it has not been initialized.

<?php
$emptyVar = NULL; // Variable with NULL value
?>

For more details read the article – PHP Data types


Next Article
PHP Variables

A

anjalisa6ys
Improve
Article Tags :
  • PHP
  • Web Technologies

Similar Reads

  • Variables and Datatypes in JavaScript
    Variables and data types are foundational concepts in programming, serving as the building blocks for storing and manipulating information within a program. In JavaScript, getting a good grasp of these concepts is important for writing code that works well and is easy to understand. VariablesA varia
    3 min read
  • PHP Date and Time
    PHP provides functions to work with dates and times, allowing developers to display the current date/time, manipulate and format dates, and perform operations like date comparisons, time zone adjustments, and more. In this article, we'll discuss PHP date and time. Why are Date and Time Important in
    5 min read
  • PHP Variables
    A variable in PHP is a container used to store data such as numbers, strings, arrays, or objects. The value stored in a variable can be changed or updated during the execution of the script. All variable names start with a dollar sign ($).Variables can store different data types, like integers, stri
    5 min read
  • PHP mb_convert_variables() Function
    The mb_convert_variables() is an inbuilt function in PHP that transforms the character code into variables. Syntax: mb_convert_variables( $to_encoding, $from_encoding, $var, ...$vars ): string|falseParameters: This function accepts four parameters that are described below: $to_encoding: The characte
    1 min read
  • What are the differences between PHP constants and variables ?
    PHP Constants: PHP Constants are the identifiers that remain the same. Usually, it does not change during the execution of the script. They are case-sensitive. By default, constant identifiers are always uppercase. Usually, a Constant name starts with an underscore or a letter which is followed by a
    2 min read
  • Variable-length argument list in PHP
    Given a set of arguments whose length is unknown at present, we will see how a function can work with these unknown numbers of arguments whose quantity will vary depending on the requirement. We will take up each word one by one to deeply understand the topic we are dealing with. Variable: It is the
    2 min read
  • PHP array_values() Function
    The array_values() is an inbuilt PHP function is used to get an array of values from another array that may contain key-value pairs or just values. The function creates another array where it stores all the values and by default assigns numerical keys to the values. Syntax:array array_values($array)
    2 min read
  • ES6 | Variables
    A variable is a named container in the memory, that stores the values. In simple words, we can say that a variable is a container for values in Javascript. The ES6 Variable names are called identifiers. The rules to keep in mind while naming an identifier. An identifier can contain alphabets and num
    4 min read
  • PHP $_FILES Array (HTTP File Upload variables)
    How does the PHP file handle know some basic information like file-name, file-size, type of the file and a few attributes about the file that has been selected to be uploaded? Let's have a look at what is playing behind the scene. $_FILES is a two-dimensional associative global array of items that a
    3 min read
  • How to get a variable name as a string in PHP?
    Use variable name as a string to get the variable name. There are many ways to solve this problem some of them are discussed below: Table of Content Using $GLOBALSUsing $$ OperatorUsing debug_backtrace()Using get_defined_vars() and array_search()Method 1: Using $GLOBALS: It is used to reference all
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences