PHP – variables, data types, constants, and syntax basics

Learn the basics of PHP: variables, data types, constants, scopes, and concatenation. Also, find out when to use single quotes and when to use double quotes.

Instant results from learning PHP
Chapter
6 Lessons

About This Chapter

PHP – variables, data types, constants, and syntax basics

In this chapter, you will learn the fundamental elements of PHP, which are the foundation for creating dynamic web applications. We will cover variables, data types, constants, variable scope, and the difference between single quotes and double quotes. You will also learn how concatenation works in PHP.

Variables in PHP

Variables in PHP start with the $ sign. They can store different values and change their type during program execution.

<?php
$name = "John";
$age = 25;
$isActive = true;

Data types

  • string – text
  • int – integers
  • float – floating-point numbers
  • bool – boolean values (true/false)
  • array – arrays
  • object – objects
  • null – no value

Constants in PHP

Constants are values that cannot be changed during program execution. We create them using define() or const.

<?php
define("SITE_NAME", "MyWebsite");
const VERSION = "1.0";

Variable scope

Variables can have different scopes:

  • Global – available throughout the script
  • Local – available only inside a function
  • Static – remember their value between function calls
<?php
function counter() {
    static $i = 0;
    $i++;
    return $i;
}

echo counter(); // 1
echo counter(); // 2

Single quotes vs. double quotes

In PHP, there is a difference between using single quotes (' ') and double quotes (" "):

  • Single quotes: the text is interpreted literally.
  • Double quotes: allow variable interpolation.
<?php
$name = "John";
echo 'Hello $name'; // Hello $name
echo "Hello $name"; // Hello John

Concatenation

Concatenation means joining strings using the . operator.

<?php
$firstName = "John";
$lastName = "Smith";
echo $firstName . " " . $lastName; // John Smith

Best practices

  1. Name variables and constants descriptively ($userAge, MAX_USERS).
  2. Use const instead of define() when defining constants in application code.
  3. Avoid mixing single and double quotes unnecessarily.

FAQ

Do variables in PHP have a fixed type?

No, PHP is a dynamically typed language, so a variable’s type can change during execution.

When should I use single quotes and when double quotes?

Use single quotes when you want to display text literally. Double quotes are better when you need variable interpolation.

Can a constant in PHP be overwritten?

No, constants in PHP have immutable values – once defined, they cannot be overwritten.

Chapter Lessons

Variables and Data Types in PHP – Complete Beginner's Guide

Master PHP variables and data types with this comprehensive guide. Learn about strings, integers, arrays, objects, and best practices for variable naming.

Lesson 1

Constants in PHP - Complete Beginner's Guide

Learn how to create and use constants in PHP. Complete guide covering const keyword, define() function, class constants, and best practices.

Lesson 2

Operators in PHP - Arithmetic, Comparison, and Logic

Master PHP operators with this comprehensive tutorial. Learn arithmetic, comparison, and logical operators with practical examples and best practices.

Lesson 3

Difference Between Single Quotes and Double Quotes in PHP

Learn the difference between single and double quotes in PHP. Understand string interpolation, escape sequences, and when to use each type.

Lesson 4

Concatenation, Interpolation, and String Manipulation in PHP

Learn how to concatenate strings and use interpolation in PHP. Master string manipulation with practical examples and best practices.

Lesson 5

Math Functions in PHP: round, ceil, floor, rand

Master PHP math functions including round, ceil, floor, and rand. Learn how to perform calculations and generate random numbers with examples.

Lesson 6