Defining variables in PHP
PHP Server Side Scripting Language Program variables in HTML webpage are used to store and process program data type values in the current PHP script. Variables in PHP can be used anywhere in the entire script. You can define variables in PHP using the dollar symbol ($). Where the name of the user defined PHP declared variable is given after the dollar symbol.

So let’s understand the process of defining program variables in a PHP script.
Syntax of defining variable in PHP script.
$variable_name = value;
Php Variable Declaration Rules.
- PHP $ symbol – Dollar symbol in PHP script indicates that a variable declaration has been done with the dollar symbol.
- PHP variable_name – Here is the name of the PHP variable, it should start with a character or underscore symbol, followed by any combination of characters, numerics, or underscore symbol.
- Variable value – Here is the value to be given to the variable for the PHP script, this variable value can be any numeric, string, boolean, array, etc. data type format.
PHP variable declaration example.
<?php
$message = “Welcome, To Vcanhelpsu!”; // php String variable declaration
$decimal = 01; // php decimal name Integer variable declaration
$is_value = true; // php Boolean variable declaration
?>
Rules for naming variables in PHP scripting.
The name of any declared variable in PHP script should start with character (a-z, A-Z) or underscore (_) symbol.
Character, numeric, and underscore etc. can be used in the name of variable in PHP script. But always remember that variable in PHP script should never start with numeric integer.
Variable names in PHP script are case-sensitive in nature, which means both $value and $Value variables are treated as separate variables by PHP script.
Valid variable names in PHP script.
$stu_name = “Ajit”;
$Emp_name = “Bhavishi”;
$_operator = “Harry”;
$salary = 10000.789;
Some Invalid variable names in PHP script.
$7type = “computer”; // never Starts with a number
$!3code = “new”; // it Contains special characters and number (other than “!3”)
Data types of variables in PHP script.
In PHP web development scripts, you get support for different types of data types, and when a value is provided to a variable in PHP script, the data type of the variable is automatically defined in the PHP script first.
Data types of variables in PHP script.
php string variable – In PHP web development scripts, string variables are declared as a sequence of characters enclosed in double quotes.
$info = “Hi, Vcanhelpsu!”;
Integer – In PHP web development script, complete numeric value is represented as a value (positive or negative) without decimal points.
$age = 25;
php Float (or double) – In floating variable declaration in PHP web development scripts, variables can be declared with numbers and decimal points.
$salary = 700.88;
php Boolean – In PHP web development script, Boolean variable is used to declare true or false value.
$test_value = true;
php Array – In PHP web development script, array data type variable stores and processes data and information in continuous order in a particular block.
$language = [“Html”, “Php”, “Css”];
Php Object – In PHP web development script, variable is declared by creating object data type in class format.
class Employee {
public $emp name;
public $emp_age;
}
$employee1 = new Employee();
$employee1->emp_name = “Sidd”;
$employee1->emp_age = 27;
php NULL – This is done by defining and declaring a variable as null variable in a particular script in PHP web development scripts.
$none_value = NULL;
Displaying variable values in PHP web development scripts.
To output the declared variable value in PHP web development scripts to the console screen, web developers use echo or print statements.
<?php
$message = “Hi, Vcanhelpsu!”;
echo $message; // result is – Hi, Vcanhelpsu!
?>
If you want to display multiple variables at once in PHP web development scripts, or want to display text along with variables. Then you can do it like this in PHP web development scripts.
<?php
$emp_name = “Anil”;
$emp_age = 40;
echo “\n Employee Name – $emp_name, \n Employee Age – $emp_age”; // the result is – employee Name – Anil, employee Age – 40
?>
Alternatively, in PHP web development scripts, you can use concatenation (dot operator) to combine strings.
<?php
$course_name = “Php”;
$course_price = 999;
echo “\n Course Name – ” . $course_name. ” \n Course price – ” . $course_price; // the result is – course_Name – php, course price- 999
?>
Variable scope in PHP web development scripts.
Variables declared in PHP web development scripts can have as many different scopes as needed.
- PHP global – Global variables in PHP web development scripts are scoped as global variables by declaring them outside the function, and these variables can be accessed from anywhere in the PHP script.
- PHP local – Variables declared inside a function in PHP web development scripts are scoped as local variables, and they are accessed only from inside that program function.
- PHP superglobals – PHP web development scripts have built-in global variables like $_GET, $_POST, $_SESSION, etc., which can always be accessed within the php program.
Example of global and local scope in PHP web development scripts.
<?php
$global_variable = “\n this is a global variable example”;
function testFunction() {
$local_variable = “\n this is a local variable example”;
echo $local_variable; // result – this is a local variable example
}
testFunction();
echo $global_variable; // result – this is a global variable example
// echo $local_variable; // result Error – Undefined variable
?>
Variables in PHP web development scripts.
PHP allows the use of variable names in web development scripts, which means you can display a variable by naming it after another variable.
Variables in PHP Variable name example.
<?php
$varValue = “vcanhelpsu”;
$$varValue = “vcandevelop”; // here we create variable name $vcandevelop
echo $vcanhelpsu; // the result – vcandevelop
?>
Here in this example above, the value of $varValue (“vcanhelpsu”) is used as the name of a new variable, $vanhelpsu. Which is then assigned the value “vcandevelop”.
Unset variables in php.
In PHP web development scripts, you can remove a variable from memory using the unset() function.
<?php
$firm_name = “vcanhelpsu”;
unset($firm_name); // unset Removes the variable $firm_name
echo $firm_name; // result Error – it used to Undefined variable
?>
Variable summary in PHP web development scripts.
In PHP web development scripts, variables are defined and declared by prefixing the name with a dollar symbol ($).
PHP web development scripts can store and process variables of different data types (string, integer, float, array, etc.).
When you provide a variable to a function in PHP web development scripts, PHP automatically defines its data type.
Variables in PHP web development scripts have many different scopes. Such as, local variables, global variables, and superglobal variables in nature.
In PHP web development scripts, you can use echo or print statements to display the variable value.