0% found this document useful (0 votes)
2K views

PHP Variables

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2K views

PHP Variables

Uploaded by

jaindisha731
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

PHP Variables

In PHP, a variable is declared using a $ sign followed by the variable name. Here, some
important points to know about variables:

o As PHP is a loosely typed language, so we do not need to declare the data


types of the variables. It automatically analyzes the values and makes
conversions to its correct datatype.
o After declaring a variable, it can be reused throughout the code.
o Assignment Operator (=) is used to assign the value to a variable.

Syntax of declaring a variable in PHP is given below:

1. $variablename=value;

Rules for declaring PHP variable:

o A variable must start with a dollar ($) sign, followed by the variable name.
o It can only contain alpha-numeric character and underscore (A-z, 0-9, _).
o A variable name must start with a letter or underscore (_) character.
o A PHP variable name cannot contain spaces.
o One thing to be kept in mind that the variable name cannot start with a
number or special symbols.
o PHP variables are case-sensitive, so $name and $NAME both are treated as
different variable.

PHP Variable: Declaring string, integer, and float


Let's see the example to store string, integer, and float values in PHP variables.

File: variable1.php

1. <?php
2. $str="hello string";
3. $x=200;
4. $y=44.6;
5. echo "string is: $str <br/>";
6. echo "integer is: $x <br/>";
7. echo "float is: $y <br/>";
8. ?>

Output:

string is: hello string


integer is: 200
float is: 44.6

PHP Variable: Sum of two variables


File: variable2.php

1. <?php
2. $x=5;
3. $y=6;
4. $z=$x+$y;
5. echo $z;
6. ?>

Output:

11

PHP Variable: case sensitive


In PHP, variable names are case sensitive. So variable name "color" is different from Color,
COLOR, COLor etc.

File: variable3.php

1. <?php
2. $color="red";
3. echo "My car is " . $color . "<br>";
4. echo "My house is " . $COLOR . "<br>";
5. echo "My boat is " . $coLOR . "<br>";
6. ?>
Output:

My car is red
Notice: Undefined variable: COLOR in C:\wamp\www\variable.php on line 4
My house is
Notice: Undefined variable: coLOR in C:\wamp\www\variable.php on line 5
My boat is

PHP Variable: Rules


PHP variables must start with letter or underscore only.

PHP variable can't be start with numbers and special symbols.

File: variablevalid.php

1. <?php
2. $a="hello";//letter (valid)
3. $_b="hello";//underscore (valid)
4.
5. echo "$a <br/> $_b";
6. ?>

Output:

hello
hello

File: variableinvalid.php

1. <?php
2. $4c="hello";//number (invalid)
3. $*d="hello";//special symbol (invalid)
4.
5. echo "$4c <br/> $*d";
6. ?>

Output:

Parse error: syntax error, unexpected '4' (T_LNUMBER), expecting variable


(T_VARIABLE)
or '$' in C:\wamp\www\variableinvalid.php on line 2
PHP: Loosely typed language
PHP is a loosely typed language, it means PHP automatically converts the variable to its correct
data type.

PHP Variable Scope


The scope of a variable is defined as its range in the program under which it
can be accessed. In other words, "The scope of a variable is the portion of
the program within which it is defined and can be accessed."

PHP has three types of variable scopes:

1. Local variable
2. Global variable
3. Static variable

Local variable
The variables that are declared within a function are called local variables for
that function. These local variables have their scope only in that particular
function in which they are declared. This means that these variables cannot
be accessed outside the function, as they have local scope.

A variable declaration outside the function with the same name is completely
different from the variable declared inside the function. Let's understand the
local variables with the help of an example:

File: local_variable1.php

1. <?php
2. function local_var()
3. {
4. $num = 45; //local variable
5. echo "Local variable declared inside the function is: ". $num;
6. }
7. local_var();
8. ?>

Output:
Local variable declared inside the function is: 45
File: local_variable2.php

1. <?php
2. function mytest()
3. {
4. $lang = "PHP";
5. echo "Web development language: " .$lang;
6. }
7. mytest();
8. //using $lang (local variable) outside the function will generate an error
9. echo $lang;
10.?>

Output:

Web development language: PHP


Notice: Undefined variable: lang in D:\xampp\htdocs\program\p3.php on line 28

Global variable
The global variables are the variables that are declared outside the function.
These variables can be accessed anywhere in the program. To access the
global variable within a function, use the GLOBAL keyword before the
variable. However, these variables can be directly accessed or used outside
the function without any keyword. Therefore there is no need to use any
keyword to access a global variable outside the function.

Let's understand the global variables with the help of an example:

Example:
File: global_variable1.php

1. <?php
2. $name = "Sanaya Sharma"; //Global Variable
3. function global_var()
4. {
5. global $name;
6. echo "Variable inside the function: ". $name;
7. echo "</br>";
8. }
9. global_var();
10. echo "Variable outside the function: ". $name;
11.?>

Output:

Variable inside the function: Sanaya Sharma


Variable outside the function: Sanaya Sharma

Note: Without using the global keyword, if you try to access a global variable inside the
function, it will generate an error that the variable is undefined.

Example:
File: global_variable2.php

1. <?php
2. $name = "Sanaya Sharma"; //global variable
3. function global_var()
4. {
5. echo "Variable inside the function: ". $name;
6. echo "</br>";
7. }
8. global_var();
9. ?>

Output:

Notice: Undefined variable: name in D:\xampp\htdocs\program\p3.php on line 6


Variable inside the function:

Using $GLOBALS instead of global


Another way to use the global variable inside the function is predefined
$GLOBALS array.

Example:

File: global_variable3.php

1. <?php
2. $num1 = 5; //global variable
3. $num2 = 13; //global variable
4. function global_var()
5. {
6. $sum = $GLOBALS['num1'] + $GLOBALS['num2'];
7. echo "Sum of global variables is: " .$sum;
8. }
9. global_var();
10.?>

Output:

Sum of global variables is: 18

If two variables, local and global, have the same name, then the local
variable has higher priority than the global variable inside the function.

Example:

File: global_variable2.php

1. <?php
2. $x = 5;
3. function mytest()
4. {
5. $x = 7;
6. echo "value of x: " .$x;
7. }
8. mytest();
9. ?>

Output:

Value of x: 7

Static variable
It is a feature of PHP to delete the variable, once it completes its execution
and memory is freed. Sometimes we need to store a variable even after
completion of function execution. Therefore, another important feature of
variable scoping is static variable. We use the static keyword before the
variable to define a variable, and this variable is called as static variable.

Static variables exist only in a local function, but it does not free its memory
after the program execution leaves the scope. Understand it with the help of
an example:

Example:
File: static_variable.php

1. <?php
2. function static_var()
3. {
4. static $num1 = 3; //static variable
5. $num2 = 6; //Non-static variable
6. //increment in non-static variable
7. $num1++;
8. //increment in static variable
9. $num2++;
10. echo "Static: " .$num1 ."</br>";
11. echo "Non-static: " .$num2 ."</br>";
12. }
13.
14.//first function call
15. static_var();
16.
17. //second function call
18. static_var();
19.?>

Output:

Static: 4
Non-static: 7
Static: 5
Non-static: 7
You have to notice that $num1 regularly increments after each function call,
whereas $num2 does not. This is why because $num1 is not a static variable,
so it freed its memory after the execution of each function call.

PHP $ and $$ Variables


The $var (single dollar) is a normal variable with the name var that stores
any value like string, integer, float, etc.

The $$var (double dollar) is a reference variable that stores the value of the
$variable inside it.

To understand the difference better, let's see some examples.

Example 1

1. <?php
2. $x = "abc";
3. $$x = 200;
4. echo $x."<br/>";
5. echo $$x."<br/>";
6. echo $abc;
7. ?>

Output:

In the above example, we have assigned a value to the variable x as abc.


Value of reference variable $$x is assigned as 200.

Now we have printed the values $x, $$x and $abc.


Example2

1. <?php
2. $x="U.P";
3. $$x="Lucknow";
4. echo $x. "<br>";
5. echo $$x. "<br>";
6. echo "Capital of $x is " . $$x;
7. ?>

Output:

In the above example, we have assigned a value to the variable x as U.P.


Value of reference variable $$x is assigned as Lucknow.

Now we have printed the values $x, $$x and a string.

Example3

1. <?php
2. $name="Cat";
3. ${$name}="Dog";
4. ${${$name}}="Monkey";
5. echo $name. "<br>";
6. echo ${$name}. "<br>";
7. echo $Cat. "<br>";
8. echo ${${$name}}. "<br>";
9. echo $Dog. "<br>";
10. ?>
Output:

In the above example, we have assigned a value to the variable name Cat.
Value of reference variable ${$name} is assigned as Dog and ${$
{$name}} as Monkey.

Now we have printed the values as $name, ${$name}, $Cat, ${$


{$name}} and $Dog.

Dynamic Variable:
In programming, a dynamic variable is a variable whose address is
determined when the program is run. In contrast, a static
variable has memory reserved for it at compilation time.

A variable of a variable takes a value of a variable and threads which is the name of a
variable. This is new feature of using variables and by using double dollar signs. This
technique is called a dynamic variable in PHP. Those variables you can use a dynamically
generated variable of variable as well as OOP Concept.

Example1

<?php
$b = 'hello';
$$b = 'Sharad Gupta';
echo $b.${$b};
?>

Output
Variable Manipulation Functions
The following functions operate on PHP variables. There are functions for
getting and setting the type of a variable, as well as various ways to encode
and decode variables for storage.

bool define(string var_name, mixed value[, int case_sensitive])

Define a constant value

int defined(string constant_name)

Test if a constant is defined


double doubleval(mixed var)

Get the double-precision value of a variable

string getenv(string varname)

Get the value of an environment variable

string gettype(mixed var)

Return the type of the variable

int intval(mixed var [, int base])

Get the integer value of a variable using the optional base for the
conversion

bool is_array(mixed var)

Return true if variable is an array

bool is_double(mixed var)

Return true if variable is a double

bool is_float(mixed var)

An alias for is_double( )

bool is_int(mixed var)

An alias for is_long( )

bool is_integer(mixed var)

An alias for is_long( )

bool is_long(mixed var)


Return true if variable is a long (integer)

bool is_object(mixed var)

Return true if variable is an object

bool is_real(mixed var)

An alias for is_double( )

bool is_string(mixed var)

Return true if variable is a string

string pack(string format,


mixed arg1, mixed arg2, ...)

Take one or more arguments and pack them into a binary string
according to the format argument

bool is_real(mixed var)

An alias for is_double( )

bool is_string(mixed
var)

Return true if variable is a string

void putenv(string
setting)

Set the value of an environment variable

string ...

You might also like