Skip to main content
infervour.com

Back to all posts

How to Declare And Use Variables In PHP?

Published on
6 min read
How to Declare And Use Variables In PHP? image

Best PHP Programming Books to Buy in October 2025

1 PHP & MySQL: Server-side Web Development

PHP & MySQL: Server-side Web Development

BUY & SAVE
$29.21 $45.00
Save 35%
PHP & MySQL: Server-side Web Development
2 Programming PHP: Creating Dynamic Web Pages

Programming PHP: Creating Dynamic Web Pages

BUY & SAVE
$36.49 $65.99
Save 45%
Programming PHP: Creating Dynamic Web Pages
3 PHP Crash Course: The Complete, Modern, Hands-On Guide

PHP Crash Course: The Complete, Modern, Hands-On Guide

BUY & SAVE
$53.53 $69.99
Save 24%
PHP Crash Course: The Complete, Modern, Hands-On Guide
4 Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL

BUY & SAVE
$66.29 $95.00
Save 30%
Front-End Back-End Development with HTML, CSS, JavaScript, jQuery, PHP, and MySQL
5 Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP

Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP

BUY & SAVE
$35.00
Full Stack Web Development For Beginners: Learn Ecommerce Web Development Using HTML5, CSS3, Bootstrap, JavaScript, MySQL, and PHP
6 Murach's PHP and MySQL (4th Edition)

Murach's PHP and MySQL (4th Edition)

BUY & SAVE
$42.38
Murach's PHP and MySQL (4th Edition)
7 Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)

BUY & SAVE
$39.03 $65.99
Save 41%
Learning PHP, MySQL & JavaScript: A Step-by-Step Guide to Creating Dynamic Websites (Learning PHP, MYSQL, Javascript, CSS & HTML5)
+
ONE MORE?

In PHP, variables are used to store and manipulate data. To declare a variable in PHP, you use the dollar sign ($) followed by the variable name. The variable name must start with a letter or underscore, and it can contain letters, numbers, and underscores.

Once you declare a variable, you can assign a value to it using the assignment operator (=). PHP is a dynamically typed language, meaning you don't have to explicitly declare the variable type. It is determined based on the value assigned to the variable.

For example, to declare and assign a value to a variable:

$name = "John Doe"; $age = 25; $isStudent = true;

Here, $name is a string variable storing the name "John Doe", $age is an integer variable with the value 25, and $isStudent is a boolean variable set to true.

You can also change the value of a variable later in your code by simply assigning a new value to it:

$age = 26;

Variables can be used in conjunction with strings using the concatenation operator (.) to build dynamic strings:

echo "My name is " . $name . " and I am " . $age . " years old.";

This would output: "My name is John Doe and I am 26 years old."

Variables can also be used in mathematical operations:

$total = $price * $quantity;

Here, the variable $total would store the result of multiplying the values of $price and $quantity.

It's important to note that PHP is a loosely typed language, so you can change the type of a variable by assigning a new value to it, even if it's of a different data type.

How to pass variables between PHP pages?

There are several ways to pass variables between PHP pages:

  1. Using URL parameters: You can pass variables through the URL by appending them as parameters. For example, to pass a variable name with the value John, you can use the URL page2.php?name=John. Then in your PHP code, you can access this variable using the $_GET superglobal like $_GET['name'].
  2. Using sessions: Sessions allow you to store variables that can be accessed across multiple pages. To use sessions, you need to start a session using session_start() at the beginning of each page. Then, you can store variables in $_SESSION superglobal array, like $_SESSION['name'] = 'John'. You can access this variable on other pages by starting the session again and retrieving the variable like $_SESSION['name'].
  3. Using cookies: Cookies can be used to store and retrieve variables between pages. You can set a cookie using the setcookie() function, like setcookie('name', 'John', time()+3600);. Then, on subsequent pages, you can access the variable through $_COOKIE, like $_COOKIE['name'].
  4. Using hidden or form fields: You can pass variables between pages by using hidden fields in HTML forms. Set the value of the hidden field to the variable you want to pass, and when the form is submitted, the variable value will be available in the PHP code on the next page using $_POST or $_GET depending on the form method.

These methods provide different ways to pass variables between PHP pages, depending on the specific use case and requirements.

How to define and use class variables in PHP?

To define and use class variables in PHP, you need to follow these steps:

  1. Class Variable Declaration: Define the class variables within the class using the var keyword or public, protected, or private access modifiers.

class MyClass { var $variable1; // or public $variable1; var $variable2; // or protected $variable2; var $variable3; // or private $variable3; }

  1. Accessing Class Variables: You can access class variables using the object of the class or from within the class using the $this keyword.

$obj = new MyClass(); $obj->variable1 = "Value 1"; echo $obj->variable1;

  1. Using Class Variables within Class Methods: You can use class variables within class methods as well.

class MyClass { var $myVar = "Hello";

function printVar() {
    echo $this->myVar;
}

}

$obj = new MyClass(); $obj->printVar(); // Output: Hello

  1. Modifying Class Variables: You can modify the class variables using the object of the class.

$obj = new MyClass(); $obj->myVar = "World"; $obj->printVar(); // Output: World

It's important to note that class variables are shared among all objects of the same class.

How to extract variable values from a URL query string in PHP?

In PHP, you can use the parse_str() function to extract variable values from a URL query string. The parse_str() function parses a query string into variables and their values.

Here's an example of how to use parse_str() to extract variable values from a URL query string:

// Example URL with query string $url = 'https://example.com/?name=John&age=30&location=London';

// Extract query string from the URL $queryString = parse_url($url, PHP_URL_QUERY);

// Parse query string into variables and their values parse_str($queryString, $variables);

// Extract individual variable values $name = $variables['name']; $age = $variables['age']; $location = $variables['location'];

// Use the extracted variable values echo "Name: " . $name . ""; echo "Age: " . $age . ""; echo "Location: " . $location . "";

Output:

Name: John Age: 30 Location: London

In this example, the parse_url() function is used to extract the query string from the URL. Then, the parse_str() function is used to parse the query string into variables and their values. Finally, the individual variable values can be accessed and used as needed.

How to declare and use constant variables within a class in PHP?

To declare and use constant variables within a class in PHP, you can follow these steps:

  1. Define the class using the class keyword:

class ClassName { // Class code here }

  1. Declare the constant variable(s) using the const keyword followed by the constant name, the assignment operator, and the constant value. Constants are conventionally written in uppercase letters:

class ClassName { const CONSTANT_NAME = value; }

  1. Access the constant variable within the class using the self keyword followed by the :: scope resolution operator and the constant name:

class ClassName { const CONSTANT_NAME = value;

public function someMethod() {
    echo self::CONSTANT\_NAME;
}

}

Here's an example demonstrating the usage of a constant variable within a PHP class:

class Circle { const PI = 3.14159;

public function calculateArea($radius) {
    return self::PI \* $radius \* $radius;
}

}

$circle = new Circle(); echo $circle->calculateArea(5); // Output: 78.53975

In the above example, the constant variable PI is declared within the Circle class. It is then accessed within the calculateArea method using self::PI and used for calculating the area of a circle.