Global vs Suparglobals in PHP

The range of the availability or scope of variables are called global of superglobals.

Global

A variable declared can be global using the keyword global. Placing global keyword in the front of a variable change it to as global scope. No it is accessible within a function or within a include file if it is not within file of function. Placing 'global' keyword in front of the and already existing variable tells PHP to use the variable having the name.



<?php
// DEFINE A VARIBLE NAMED $one
$one = 'ONE';
// DEFINE A ANOTHERE VARIABLE NAME $two
$two = 'TWO';

// CREATE A FUNCTION WIT HTHE NAME OF THEST
function test()
{
// USING GLOBAL KEYWORD FIND THE BOTH VARIABLES AVAILBLE IN THIS PROGRAM
global $one, $two;

// PRINT A STRING USIGN THE GLOBAL SCOPE VARIABLE CONCATED WITH THE STRING
echo "Red function has " . $one . " and Green function has " . $two . " Parameters";

}
?>

Superglobals

Superglobals are PHP built in variables or members That have already have global scope they don't have need to add global keyword. There are some superglobals are listed below.




$GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, $_ENV

Fibonacci Series using PHP


Fibonacci Series print the values of the value of current and previous value of the natural numbers it is look like 0 1 1 2 3 5 8 13 it is the linear sequence of the integers these all values follow the the mathematical logic is Fn+1 = Fn + Fn-1



<?php
// DEFINE $i for INITILIZATION
$i = 1;
// DEFINE $a FOR THE STARTING LOOP VALUE
$a = 0;
// DEFINE $b FOR SECON HELPER VALUE
$b = 1;

// START LOOP TO PRINT VALUE
// CONDITION WILL PRINT ONLY 20 VALUES
// LOOP WILL RUN  ONLY 20 TIMES
while($i<=20)
{
// LOOP WILL PRINT THE VALUES HERE
// CONCATINATION SPACE TO DIFFERENCIATE THE VALUES
echo $a . " ";  // 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
// $c IS HELPER VARIABLE STORE THE THIS AND NEXT VALUE
$c = $a + $b; // 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
// ASSIGN NEXT VALUE TO THE $a
$a = $b; // 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765
// ASSIGN THE VALUE OF $C(SUM OF THIS AND NEXT VALUE) TO $B
$b = $c; // 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946
// INCREMENT FOR LOOP
$i++;  // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
}

?>

How to print Fabonacci Series using looping statement in PHP ?

Fabonacci Series print the values of the value of current and previous value of the natural numbers it is look like 0 1 1 2 3 5 8 13 it is the linear sequence of the integers these all values follow the the mathematical logic is Fn+1 = Fn + Fn-1




// DEFINE $i for INITILIZATION
$i = 1;
// DEFINE $a FOR THE STARTING LOOP VALUE
$a = 0;
// DEFINE $b FOR SECON HELPER VALUE
$b = 1;

// START LOOP TO PRINT VALUE
// CONDITION WILL PRINT ONLY 20 VALUES
// LOOP WILL RUN  ONLY 20 TIMES
while($i<=20)
{
// LOOP WILL PRINT THE VALUES HERE
// CONCATINATION SPACE TO DIFFERENCIATE THE VALUES
echo $a . " ";  // 0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
// $c IS HELPER VARIABLE STORE THE THIS AND NEXT VALUE
$c = $a + $b; // 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946

// ASSIGN NEXT VALUE TO THE $a
$a = $b; // 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

// ASSIGN THE VALUE OF $C(SUM OF THIS AND NEXT VALUE) TO $B
$b = $c; // 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946

// INCREMENT FOR LOOP
$i++;  // 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
}

Students Tech Life