0% found this document useful (0 votes)
7K views21 pages

PHP Uniti

Uploaded by

Tejam Fandat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views21 pages

PHP Uniti

Uploaded by

Tejam Fandat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

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:
Local variable declared inside the function is: 45
Local variable declared inside the function is: 45

<?p <?php hp

<?php
function local_var()
{
$num = 45; //local variable
echo "Local variable declared inside the function is: ". $num;
}
local_var();
?>
• Output:
Local variable declared inside the function is:45
<?php
function mytest()
{
$lang = "PHP";
echo "Web development language: " .$lang;
}
mytest();
//using $lang (local variable) outside the function will generate an error
echo $lang;
?>
• 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
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.
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:
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:
<?php
function static_var()
{
static $num1 = 3; //static variable
$num2 = 6; //Non-static variable
//increment in non-static variable
$num1++;
//increment in static variable
$num2++;
echo "Static: " .$num1 ."</br>";
echo "Non-static: " .$num2 ."</br>";
}

//first function call


static_var();

//second function call


static_var();
?>
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 $num2 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.
1.
<?php
$x = "abc";
$$x = 200;
echo $x."<br/>";
echo $$x."<br/>";
echo $abc;
?>
• Output
abc
200
200
PHP Data Types

• PHP data types are used to hold different types of data or values. PHP
supports 8 primitive data types that can be categorized further in 3
types:
1.Scalar Types (predefined)
2.Compound Types (user-defined)
3.Special Types
• PHP Data Types: Scalar Types
• It holds only single value. There are 4 scalar data types in PHP.
1. boolean
2. integer
3. float
4. string
• PHP Data Types: Compound Types
• It can hold multiple values.
• There are 2 compound data types in PHP.
1. array
2. Object

• PHP Data Types: Special Types


There are 2 special data types in PHP.
resource
NULL
• PHP Boolean
• Booleans are the simplest data type works like switch. It holds only two values: TRUE
(1) or FALSE (0). It is often used with conditional statements. If the condition is correct, it
returns TRUE otherwise FALSE.
1.<?php
2. if (TRUE)
3. echo "This condition is TRUE.";
4. if (FALSE)
5. echo "This condition is FALSE.";
6.?>
• PHP Integer
• Integer means numeric data with a negative or positive sign. It holds only whole
numbers, i.e., numbers without fractional part or decimal points.
• Rules for integer:
• An integer can be either positive or negative.
• An integer must not contain decimal point.
• Integer can be decimal (base 10), octal (base 8), or hexadecimal (base 16).
• The range of an integer must be lie between 2,147,483,648 and 2,147,483,647 i.e.,
-2^31 to 2^31.
• PHP Float
• A floating-point number is a number with a decimal point. Unlike integer, it
can hold numbers with a fractional or decimal point, including a negative or
positive sign.

PHP String
• A string is a non-numeric data type. It holds letters or any alphabets,
numbers, and even special characters.
• String values must be enclosed either within single quotes or in double
quotes. But both are treated differently. To clarify this, see the example
below:
• Notice: Undefined variable: name in D:\xampp\htdocs\program\
p3.php on line 6
• Variable inside the function:
Web development language: PHP

Web development language: PHP


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

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

You might also like