Paramclasses Academy - Blog

Tutorials Details

What is Variables, Constants, Data Types, Comments, Echo and Print

What is Variables, Constants, Data Types, Comments, Echo and Print

October 9, 2026

Meta Title: Core PHP: Variables, Data Types, Echo & Print

Meta Description: Learn PHP basics for beginners, including variables, constants, data types, comments, echo, and print with simple code examples and real-world applications.

Focus Keyword: Core PHP Tutorial for Beginners

Secondary Keywords: PHP Variables, PHP Constants, PHP Data Types, PHP Echo and Print, PHP Programming Examples, Learn PHP Basics, PHP Practical Programs

Suggested URL Slug: core-php-module-2-php-basics

Introduction to PHP Basics

Welcome to Module 2 of our Core PHP Programming Tutorial for Beginners. In Module 1, we learned what PHP is, why it is used, how PHP works, how to install XAMPP, and how to run our first PHP program.

In this module, we will learn the fundamental concepts of PHP programming, including variables, constants, data types, comments, and the echo and print statements.

These concepts are essential for developing dynamic websites, admin panels, login systems, e-commerce websites, news portals, and database-driven web applications.

If you are a beginner, do not worry. Each topic includes a simple explanation, PHP code examples, and practical applications.

1. What Are Variables in PHP?

A variable is used to store information that a program can use later. The stored information can be a name, age, price, quantity, or other value.

In PHP, every variable name begins with the dollar sign ($).

PHP Variable Syntax

<?php
$variableName = "value";
?>

Here, $variableName is the variable, and "value" is the information assigned to it.

Example 1: Creating a Variable

<?php
$name = "Parmanand";
$age = 36;
$city = "Raipur";

echo $name;
echo "<br>";
echo $age;
echo "<br>";
echo $city;
?>

Output:

Parmanand

36

Raipur

Explanation

  • $name stores the text "Parmanand".

  • $age stores the number 36.

  • $city stores the text "Raipur".

  • The echo statement displays the values.

  • The HTML <br> tag creates a line break.

Rules for PHP Variables

  1. A variable must begin with $.

  2. A variable name must start with a letter or underscore after $.

  3. A variable name cannot start with a number.

  4. Variable names are case-sensitive.

  5. Variable names can contain letters, numbers, and underscores.

For example:

<?php
$name = "Rahul";
$age = 25;
$_city = "Raipur";
$user_name = "Amit";

echo $name;
?>

The following are invalid variable names:

$1name = "Rahul";
$my-name = "Amit";

Real-World Example: Customer Information

Suppose you are developing a customer management system. You need to store a customer's name, phone number, and city.

<?php
$customerName = "Rahul Sharma";
$phone = "9876543210";
$city = "Raipur";

echo "<h2>Customer Details</h2>";
echo "Name: " . $customerName . "<br>";
echo "Phone: " . $phone . "<br>";
echo "City: " . $city;
?>

Real-world application: Variables are used to handle customer details, employee records, student information, product data, and other information in web applications.

Phone numbers are stored as strings because they are identifiers, not numbers used for arithmetic.

2. What Are Constants in PHP?

A constant is a named value that cannot be reassigned after it has been defined.

Constants are useful for values that should remain fixed throughout an application, such as a company name, tax rate configuration, or application name.

PHP supports defining constants with the define() function or the const keyword.

Example 1: Using define()

<?php
define("SITE_NAME", "Paramwebinfo");
define("CITY", "Raipur");

echo SITE_NAME;
echo "<br>";
echo CITY;
?>

Output:

Paramwebinfo

Raipur

Example 2: Using const

<?php
const COMPANY_NAME = "Paramwebinfo";
const COURSE_NAME = "Core PHP";

echo COMPANY_NAME;
echo "<br>";
echo COURSE_NAME;
?>

Output:

Paramwebinfo

Core PHP

Difference Between Variables and Constants

Variables Constants
Begin with $. Do not use $ when accessed.
Values can be reassigned. Values cannot be reassigned after definition.
Example: $price = 500; Example: define("PRICE", 500);
Used for changing or temporary data. Used for fixed configuration values.

Real-World Example: Website Information

<?php
define("WEBSITE_NAME", "Paramwebinfo");
define("WEBSITE_CITY", "Raipur");

echo "<h1>" . WEBSITE_NAME . "</h1>";
echo "<p>Our office is located in " . WEBSITE_CITY . ".</p>";
?>

Real-world application: Constants can be used for application settings and fixed configuration values. Sensitive credentials should be stored securely in environment variables or an appropriate configuration system rather than hard-coded into publicly accessible files.

3. What Are Data Types in PHP?

A data type defines the kind of value stored in a variable.

PHP supports several data types, including strings, integers, floats, booleans, arrays, objects, null, and resources.

Let's understand the most common data types with examples.

3.1 String

A string is a sequence of characters, such as a name, city, address, or message.

<?php
$name = "Parmanand";
$message = "Welcome to PHP Programming";

echo $name;
echo "<br>";
echo $message;
?>

Output:

Parmanand

Welcome to PHP Programming

Real-world use: Customer names, product names, email addresses, and website content.

3.2 Integer

An integer is a whole number without a decimal point.

<?php
$age = 36;
$quantity = 10;

echo $age;
echo "<br>";
echo $quantity;
?>

Output:

36

10

Real-world use: Counting products, recording quantities, and storing whole-number counts.

3.3 Float

A float is a number that can contain a decimal point.

<?php
$productPrice = 499.50;
$discount = 10.5;

echo $productPrice;
echo "<br>";
echo $discount;
?>

Output:

499.5

10.5

Real-world use: Measurements, prices, percentages, and calculations involving decimal values. For financial applications requiring exact decimal arithmetic, use appropriate decimal handling rather than relying blindly on floating-point calculations.

3.4 Boolean

A boolean has one of two values: true or false.

<?php
$isLoggedIn = true;
$isAdmin = false;

var_dump($isLoggedIn);
var_dump($isAdmin);
?>

Output:

bool(true)

bool(false)

Real-world use: Checking whether a user is logged in, whether a product is available, or whether a task is complete.

3.5 Array

An array stores multiple values in a single variable.

<?php
$fruits = ["Apple", "Banana", "Mango"];

echo $fruits[0];
echo "<br>";
echo $fruits[1];
echo "<br>";
echo $fruits[2];
?>

Output:

Apple

Banana

Mango

Array indexes begin at 0 in this example.

Real-world use: Product lists, student names, news categories, and collections of records.

3.6 Object

An object is an instance of a class. It can contain properties and methods.

<?php
class Student
{
    public $name = "Rahul";
}

$student = new Student();

echo $student->name;
?>

Output:

Rahul

Real-world use: Representing students, customers, products, invoices, and other entities in object-oriented applications.

3.7 Null

null represents a variable with no value.

<?php
$middleName = null;

var_dump($middleName);
?>

Output:

NULL

Real-world use: Representing an optional field that has not been provided or a value that has intentionally been cleared.

Summary of PHP Data Types

Data Type Example Description
String "Hello" Text
Integer 100 Whole number
Float 99.50 Decimal number
Boolean true True or false
Array ["A", "B"] Collection of values
Object new Student() Instance of a class
Null null No value

PHP also supports the resource type, which represents a special handle to an external resource. Beginners can study it after understanding the common data types.

4. What Are Comments in PHP?

Comments are notes written inside source code to explain how the program works.

PHP ignores comments during execution. They are useful for documenting code and making it easier for developers to understand and maintain.

PHP supports single-line comments and multiline comments.

4.1 Single-Line Comments

You can use // or # for a single-line comment.

<?php
// Store the user's name
$name = "Rahul";

// Display the name
echo $name;
?>

Output:

Rahul

The browser displays only Rahul. The comments are not shown as output.

4.2 Multiline Comments

Use /* to start a multiline comment and */ to end it.

<?php
/*
This program stores
and displays a student's name.
*/

$studentName = "Amit";

echo $studentName;
?>

Output:

Amit

Real-World Example: Product Calculation

<?php
// Store the product price
$price = 500;

// Store the product quantity
$quantity = 3;

// Calculate the total price
$total = $price * $quantity;

// Display the result
echo "Total Price: " . $total;
?>

Output:

Total Price: 1500

Real-world application: Comments help developers understand calculations, explain business rules, and maintain large projects such as CRM software, e-commerce platforms, and school management systems.

Avoid writing sensitive information such as passwords or API keys in comments because source code may be shared or exposed accidentally.

5. What Is echo in PHP?

echo is a PHP language construct used to output text, HTML, and values to the response generated by a PHP script.

It is widely used to display dynamic content on websites.

Example 1: Display Text

<?php
echo "Hello World!";
?>

Output:

Hello World!

Example 2: Display Multiple Values

<?php
$name = "Rahul";
$city = "Raipur";

echo $name;
echo "<br>";
echo $city;
?>

Output:

Rahul

Raipur

Example 3: Display HTML

<?php
echo "<h1>Welcome to My Website</h1>";
echo "<p>Learn PHP Programming.</p>";
?>

Output: The browser displays a heading and a paragraph.

Example 4: Combine Strings and Variables

<?php
$name = "Rahul";

echo "Welcome, " . $name . "!";
?>

Output:

Welcome, Rahul!

The dot (.) concatenation operator joins strings and values.

Real-World Example: Product Information

<?php
$productName = "Wireless Mouse";
$price = 799;

echo "<h2>" . $productName . "</h2>";
echo "<p>Price: ₹" . $price . "</p>";
?>

Output:

Wireless Mouse

Price: ₹799

Real-world application: echo can display product details, customer names, news headlines, dashboard information, and other dynamic website content.

6. What Is print in PHP?

print is another PHP language construct used to output text and values.

It behaves similarly to echo, but print returns the integer value 1, while echo does not return a value.

Example 1: Using print

<?php
print "Hello World!";
?>

Output:

Hello World!

Example 2: Display a Variable

<?php
$name = "Parmanand";

print $name;
?>

Output:

Parmanand

Example 3: Display a Message With HTML

<?php
print "<h2>Welcome to PHP</h2>";
print "<p>This is a beginner tutorial.</p>";
?>

Output: A heading and a paragraph appear in the browser.

Difference Between echo and print

echo print
Outputs text and values. Outputs text and values.
Does not return a value. Returns 1.
Can output multiple comma-separated arguments. Accepts one expression.
Commonly used for ordinary output. Also suitable for simple output.

For most beginner programs, both work well. You can use echo as your default output statement.

7. Combined Practical Example: Student Details

Now let's combine variables, constants, data types, comments, and echo in one program.

Create a file named student.php.

<?php
// Define the school name
define("SCHOOL_NAME", "ABC Public School");

// Store student details
$studentName = "Rahul Sharma";
$age = 18;
$percentage = 85.5;
$isPassed = true;

// Display student details
echo "<h1>" . SCHOOL_NAME . "</h1>";
echo "<h2>Student Details</h2>";

echo "Name: " . $studentName . "<br>";
echo "Age: " . $age . "<br>";
echo "Percentage: " . $percentage . "%<br>";

echo "Passed: ";
echo $isPassed ? "Yes" : "No";
?>

Expected Output

ABC Public School

Student Details

Name: Rahul Sharma

Age: 18

Percentage: 85.5%

Passed: Yes

Explanation

  • SCHOOL_NAME is a constant.

  • $studentName is a string.

  • $age is an integer.

  • $percentage is a float.

  • $isPassed is a boolean.

  • The comments explain the purpose of the code.

  • echo displays the details.

  • The ternary operator selects Yes or No based on the boolean value.

This example demonstrates how multiple PHP basics work together in a practical program.

8. Real-World Practical Example: Product Bill Calculator

Let's create a simple bill calculator similar to the calculation used in an e-commerce or billing application.

The program will calculate the total amount using a product price and quantity.

<?php
// Product information
$productName = "Keyboard";
$price = 650;
$quantity = 2;

// Calculate the total amount
$total = $price * $quantity;

// Display the bill
echo "<h2>Product Bill</h2>";
echo "Product: " . $productName . "<br>";
echo "Price: ₹" . $price . "<br>";
echo "Quantity: " . $quantity . "<br>";
echo "Total Amount: ₹" . $total;
?>

Output:

Product Bill

Product: Keyboard

Price: ₹650

Quantity: 2

Total Amount: ₹1300

How Does This Help in Real Projects?

This basic calculation can be extended into a billing application with:

  • Multiple products

  • Customer details

  • Discounts

  • GST calculations

  • Invoice generation

  • Database storage

  • PDF invoice downloads

For production billing software, financial calculations should use suitable decimal or integer-based monetary representations to avoid floating-point precision issues.

9. Best Practices for Writing PHP Code

Follow these practices while learning PHP:

  1. Use meaningful variable names such as $customerName instead of unclear names such as $x.

  2. Keep variable naming consistent throughout the project.

  3. Use constants for fixed configuration values.

  4. Add comments where they clarify complex logic.

  5. Use appropriate data types for the information being stored.

  6. Use echo for ordinary output and understand when other output functions are appropriate.

  7. Escape dynamic values before displaying untrusted text in HTML.

  8. Never store database passwords or secret API keys directly in public source code.

  9. Use consistent indentation to improve readability.

  10. Test your PHP programs using a local development environment before deployment.

These practices help you write code that is easier to read, debug, maintain, and extend.

10. Practice Exercises for Beginners

Complete the following exercises to strengthen your understanding of PHP basics.

Exercise 1: Variables

Create variables for your name, age, and city. Display each value on a separate line.

Exercise 2: Constants

Create a constant named COMPANY_NAME and display its value.

Exercise 3: Data Types

Create one variable each for a string, integer, float, boolean, and array. Display their values.

Exercise 4: Comments

Write a PHP program with single-line and multiline comments.

Exercise 5: echo and print

Display your name using echo and your city using print.

Exercise 6: Student Marks

Create variables for three subject marks and calculate their total.

Exercise 7: Product Bill

Create variables for product name, price, and quantity. Calculate and display the total bill.

Exercise 8: Business Information

Create a simple PHP page displaying a company name, services, and location.

Exercise 9: Discount Calculator

Create a variable for product price and discount percentage. Calculate the discounted price.

Exercise 10: Student Result

Create variables for student name and marks. Display a student report using PHP output statements.

11. Frequently Asked Questions About PHP Basics

What is a variable in PHP?

A variable stores a value that can be used or changed during program execution. PHP variable names begin with $.

What is a constant in PHP?

A constant is a named value that cannot be reassigned after it has been defined. You can create constants using define() or const.

What are the main data types in PHP?

Common PHP data types include string, integer, float, boolean, array, object, and null. PHP also supports resources.

What is the difference between echo and print in PHP?

Both output information. echo does not return a value and can accept multiple comma-separated arguments, while print accepts one expression and returns 1.

Are comments executed in PHP?

No. Comments are ignored by the PHP interpreter during execution. They help developers document and understand source code.

Can PHP variables store different types of values?

Yes. PHP is dynamically typed, so a variable can hold values of different types at different times. However, choosing appropriate types and maintaining consistent data handling is important.

Which editor is best for PHP beginners?

Visual Studio Code is a popular choice. Other PHP-compatible editors can also be used. A local PHP environment such as XAMPP is useful for running programs on your computer.

Why should I learn PHP basics before MySQL?

Variables, data types, operators, and output statements are foundational programming concepts. Understanding them makes it easier to work with database records, form submissions, and dynamic website content.

Conclusion

In this Core PHP Module 2 tutorial, we covered variables, constants, data types, comments, echo, and print with practical examples.

These concepts form the foundation of PHP programming. By practicing them regularly, you will be better prepared to build dynamic websites, process user input, calculate bills, display database information, and develop complete web applications.

Start by writing each example yourself. Change the values, observe the output, and complete the practice exercises before moving to the next module.