PHP Free Tutorial

Variables in PHP

In PHP, Programming by Baqir Ali

What is a variable?

A variable is a place in memory where we save our data. A variable should have a name and a data type. PHP supports different types of variable types such as boolean, integers, floating points, strings, arrays, objects, resources, and NULL.

In PHP we don’t need to define types so it automatically determines the variable type by the context it is being used.

All variable names should is preceded by a dollar sign ($) and it must begin with a letter or underscore. For example $salary, $radius, $<variable-name>.

Example 1: No need to define data type it calculates itself.

<?php
$num1 = 20;
$num2 = 10;
$result = $num1 + $num2;
$output = $num1 . " + " . $num2 . " = " . $result;
//echo($result);
echo($output);
?>

Output:

20 + 10 = 30

In the above example dot(.) is a concatenation sign.

Example 2: Variables + usage of html with php

<?php
$d1 = 20;
$d2 = 10;
echo("Value of d2 = "  . $d2 
	. "<br/>Value of d1 = " . $d1 
	. "<hr/>"); 
echo "Value of d2 = "  . $d2 
	. "<br/>Value of d1 = " . $d1 
	. "<hr/>"; 
echo "Value of d2 = ", $d2, "<br/>Value of d1 = ",
$d1, "<hr/>"; 
print("Value of d2 = "  . $d2 
	. "<br/>Value of d1 = " . $d1 
	. "<hr/>"); 
print "Value of d2 = "  . $d2 
	. "<br/>Value of d1 = " . $d1 
	. "<hr/>"; 
?>

Output:

Variable in PHP

Variable Scope in PHP

Superglobal Variable: Variables that are predefined arrays, including $_POSt and $_GET. They are accessible from anywhere on the page.

Global Variable: Such variables are accessible throughout the defined script.

Function Variable: Variables that are defined within the function and have scope only within that function.

Constants in PHP

Constants are like variables but not variables, once the value is defined you can not change the value again. Constants are define using define() function and convention are all in upper case letters. They can be accessed from anywhere on the page.

Structure:

define(‘CONSTANT_NAME’,values);

Example 3: using constant and trying to change the value

<?php
	$taxRate = 0.05;
	define("TaxRate", 0.05);
	define("TaxRate", 5000);
	$taxRate *= 1000;

	$salary = 10000;

	$TaxRate *= 1000;
	$tax = $salary * TaxRate;


	echo("Salary = $salary<br/>
		Tax = $tax<br/>
		Tax Rate = ". TaxRate);

	?>

Warning: Constant TaxRate already defined in C:\xampp\htdocs\CodeModes\07_constants.php on line 4

Warning: Undefined variable $TaxRate in C:\xampp\htdocs\CodeModes\07_constants.php on line 9
Salary = 10000
Tax = 500
Tax Rate = 0.05

It generates error as we try to change the value of constant.

References in PHP

References are variables that store address of another variable not the value. We assign address to any variable by using & operator before the source variable.

<?php
$num1 = 20;
//$num2 = $num1;
$num2 = &$num1;
//& reference  / address operator
echo("Start<br/>
	Num1 = $num1<br/>
	Num2 = $num2<br/><hr/>");
$num1 = 40;
echo("After changing num1<br/>
	Num1 = $num1<br/>
	Num2 = $num2<br/><hr/>");
$num2 = 140;
echo("After changing num2<br/>
	Num1 = $num1<br/>
	Num2 = $num2<br/><hr/>");
?>

Start
Num1 = 20
Num2 = 20


After changing num1
Num1 = 40
Num2 = 40


After changing num2
Num1 = 140
Num2 = 140

Support us by sharing this post