0% found this document useful (0 votes)
22 views12 pages

Unit 1 (4th)

The document discusses different types of variables in PHP including local variables, global variables, static variables, and superglobal variables. It provides examples of declaring and using each variable type. Local variables are only accessible within a function while global variables require the global keyword. Static variables retain their value between function calls and superglobal variables can be accessed from any scope.

Uploaded by

isha gupta
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)
22 views12 pages

Unit 1 (4th)

The document discusses different types of variables in PHP including local variables, global variables, static variables, and superglobal variables. It provides examples of declaring and using each variable type. Local variables are only accessible within a function while global variables require the global keyword. Static variables retain their value between function calls and superglobal variables can be accessed from any scope.

Uploaded by

isha gupta
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/ 12

Unit I

(4 One)
th

Mr. Harjender Singh


Asstt. Professor
PHP variables and Constants
• - variables are containers which is used to store information.
• We can store any type of value in a variable.
• A variable can be changed or removed at any time.
• In PHP, variables name begin with a $ sign followed by
name.
• while declaring a variable we need not specify its data type.
• Eg-
• $rollno = 20
• $price = 26.80
• $name = “Maharaja Surajmal Institute”;
Examples
 
Note: When you assign a text value to a variable, put quotes around the value.

• <html>
• <body>

• <?php
• $txt = "Hello world!";
• $x = 5;
• $y = 10.5;

• echo $txt;
• echo "<br>";
• echo $x;
• echo "<br>";
• echo $y;
• ?>

• </body>
• </html>
Example
• <html>
• <body>
• <?php
• $x = 5;
• $y = 4;
• echo $x + $y;
• ?>
• </body>
• </html>
PHP variable naming conventions
• Variable start with a $ sign.
• Variable name only start with a letter and underscore.
(_) i.e. the variable name should be alphanumeric.
• It cannot start with a number.
• It is a case sensitive which implies that the variable num
in lower case is different from variable NUM in upper
case.
• Don’t use predefined constant name. Eg. PHP_VERSION,
PHP_OS etc.
• Don’t use keyword.
Types of Variables –.
• there are 04 types of variables
• Local Variable – the variable which is declared inside a function has a local scope. Its value
remains valid just with in the function.
• <html>
• <body>

• <?php
• function myTest() {
• $x = 5; // local scope
• echo "<p>Variable x inside function is: $x</p>";
• }
• myTest();

• // using x outside the function will generate an error


• echo "<p>Variable x outside function is: $x</p>";
• ?>

• </body>
• </html>
Global Scope (variable) Keyword –

• The variable which is declared outside a


function has a global scope.
• Its accessibility is just outside the function.
Note – we cannot access the global variable
directly into function.
• For accessing them in separate file, class or
function we use global keyword.
• Also if we want to access the data inside a
function we use GLOBAL keyword within the
function.
Example
• <html>
• <body>

• <?php
• $x = 5;
• $y = 10;

• function myTest() {
• global $x, $y;
• $y = $x + $y;
• }

• myTest(); // run function


• echo $y; // output the new value for variable $y
• ?>

• </body>
• </html>
Static Variable
• Normally, when a function is
completed/executed, all of its variables are
deleted. However, sometimes we want a local
variable NOT to be deleted. We need it for a
further job.
• To do this, use the static keyword, when we
first declare the variable:
Example
• <html>
• <body>

• <?php
• function myTest() {
• static $x = 0;
• echo $x;
• $x++;
• }

• myTest();
• echo "<br>";
• myTest();
• echo "<br>";
• myTest();
• ?>

• </body>
• </html>
SuperGlobal Variable
• Super global are built in variable that are always
available in all scopes.
• It can be accessed them from any function, class or file
without using global keyword.
• PHP super global variable is used to connect PHP with
HTML forms. Some super global variables are –
• $_GLOBAL $_GET $_SESSION
• $_SERVER $_FILES $_COOKIE
• $_REQUEST $_ENV
• $_POST $_COOKIES
• Note : these are always written in upper case letter.
Example
• <html>
• <body>

• <?php
• $a=10;
• $b=20;
• function disp()
• {
• Echo $GLOBALS['a']."<br>";
• Echo $GLOBALS['b']."<br>";
• $GLOBALS['z'] = $GLOBALS['a'] + $GLOBALS['b'];

• }
• disp();
• //echo $a."<br>";
• echo "The Sum is = ".$z;
• ?>

• </body>
• </html>

You might also like