Developer
PHP
PHP TUTORIALPHP IntroductionPHP InstallationPHP Hello WorldPHP Basic SyntaxPHP VariablesPHP ConstantsPHP OutputPHP Data TypesPHP OperatorsPHP ConditionalsPHP Shorthand ConditionalsPHP LoopsPHP Loop Control StructuresPHP FunctionsPHP StringsPHP ArraysPHP Superglobal VariablesPHP in HTMLPHP RegexRegex IntroductionRegex PCRE SyntaxPHP PREG FunctionsPHP FormsPHP Forms IntroductionPHP Forms CreatingPHP Forms SecurityPHP Forms ValidationPHP Forms Required InputsPHP Forms StickyPHP Forms Advanced ValidationPHP Forms Finishing

PHP Variables

Variables are the storages

Variables are used to store information to referenced and manipulated. They can be labeled with a meaningful names to be understand easily by the readers and ourselves.

The data stored in variables can be changed.

PHP Variable Naming

Rules for PHP Variables

  • A variable name must start with $ sign followed by the name of the.
  • A variable name must start with a letter or underscore.
  • A variable name cannot start with a number.
  • A variable name cannot be $this. ($this is a special variable which cannot be assigned)
  • A variable name can contain letters, numbers and characters between 127-255 ASCII after the first character.
  • Variable names are case sensitive.

<?php

$name = 'Hyvor'; 	// valid
$_name = 'Hyvor';	// valid - starts with an underscore
$näme = 'Hyvor';  // valid - 'ä' is (Extended) ASCII 228

$1name = 'Hyvor';	// invalid - cannot start with a number
$this = 'Hyvor'; 	// invalid - cannot assign value to $this


PHP Declaring Variables

In programming, creating a variable is called as declaring. But, in PHP, unlike other programming languages, there is no keyword to declare a variable.

Variables are declared when you assign a value to them
Unlike other programming languages, you do not have to define data types for variables.

Note: text values should be wrapped with " (double-quotes) or ' (single-quotes). (We will discuss more about this in later tutorials.)

When declaring a variable:

  • The variable name must be on the left.
  • Then, The = sign as the assignment operator.
  • Next, the value to be assigned.
  • Finally, ; sign to say statement is finished.

PHP Declaring Variables Example


<?php
$month = "May";
$day = 22;

After running this part of the script:

  • The text value, May is stored in the $month variable. (Note that " - double-quotes are not saved)
  • The numeric value, 22 is stored in the $day variable.

PHP Reassigning Values to Variables

A variable stores the value of it's most recent assignment.

The output of the below code is "World" because it is the value of the last assignment of $text.

PHP Reassigning Variables Example


<?php
$text = 'Hello';
$text = 'World';

echo $text;

Run Example ››

PHP Variable Scope

Scope refers to the visibility of variables. In other words, which part of the program can use it.

In PHP, variable scope goes with functions. Before learning variable scope you need understand the basis of functions. For now, keep in mind that, function is a combination of instructions. We will learn more about functions later.

There are three types of scopes in PHP.

  • Global Scope
  • Local Scope
  • Static Scope

PHP Global Scope

A variable declared in the main flow of the code (not inside a function) has a global scope. These variables can't be used inside functions.

PHP Global Scope Example


<?php
$number = 10; // global scope

echo $number; // outputs 10

function num() {
	echo $number; // $number is undefined here
}

Run Example ››

PHP Local Scope

A variable declared inside a function has a local scope. This variable is unique to this function and can only be accessed within the function. You can define different variables with the same name in different function.

These variables are deleted when the function finishes execution.

PHP Local Scope Example


<?php

function hello() {
	$txt = 'Hello World'; // local scope
 	echo $txt; // $txt can be used here
} 
hello();

// $txt cannot be used outside

Run Example ››

PHP Global Variables in Local Scope

There are two ways to access global variables within a function.

  • Using global keyword
  • Using $GLOBALS array

1. PHP global Keyword

You can use global keyword inside a function to access global variables. Before using variables, add global keyword followed by comma-separated variable names you need to use.

PHP Global Keyword Example


<?php
$x = 'Hyvor';
$y = 'Developer';

function websiteName() {
	global $x, $y; 
	echo $x, $y;
}

websiteName(); // outputs HyvorDevloper

Run Example ››

If you remove global $x, $y; part from the code, it will not work because $x and $y variables are not defined within the function websiteName().

2. PHP $GLOBALS Array

PHP saves all the global variables in PHP-defined $GLOBALS array. The array key contains the variable name and array value contains the variable value. The previous example can be written as following.

PHP $GLOBALS Array Example


<?php
$x = 'Hyvor';
$y = 'Developer';

function websiteName() {
	echo $GLOBALS['x'], $GLOBALS['y'];
}

websiteName(); // outputs HyvorDevloper

Run Example ››

PHP Static Scope

As we discussed earlier, local scope variables are deleted after the end of the execution of the function. But, sometimes we need to keep the variable alive.

To use this feature, add static keyword before the variable, when declaring it.

PHP Static Variable Example


<?php
function test() {
	static $number = 0; // declare static variable
	echo $number . '<br>'; // echo number with line break
	$number = $number + 5; // add five to $number
}

test();
test();
test();

Run Example ››

Each time the function is called, $number variable has the value of the last assignment.

Advertisement
0
261