BLOBALS PHP - learnit

Home Top Ad

Post Top Ad

Monday, May 25, 2020

BLOBALS PHP

Definition of PHP Global Variable

What is PHP

PHP is a server side scripting language. that is used to develop Static websites or Dynamic websites or Web applications. PHP stands for Hypertext Pre-processor, that earlier stood for Personal Home Pages.


PHP scripts can only be interpreted on a server that has PHP installed.

The client computers accessing the PHP scripts require a web browser only.


Why use PHP?

Definition of PHP Global Variable

You have obviously heard of a number of programming languages out there; you may be wondering why we would want to use PHP as our poison for the web programming. Below are some of the compelling reasons.

  -PHP is open source and free.


  -Short learning curve compared to other languages such as JSP, ASP etc.


  -Large community document


  -Most web hosting servers support PHP by default unlike other languages such as ASP that need IIS. This makes PHP a cost effective choice.


  -PHP is regular updated to keep abreast with the latest technology trends.


  - Other benefit that you get with PHP is that it’s a server side scripting language; this means you only need to install it on the server and client computers requesting for resources from the server do not need to have PHP installed; only a web browser would be enough.


  -PHP has in built support for working hand in hand with MySQL; this doesn’t mean you can’t use PHP with other database management systems. You can still use PHP with


    +Postgres

    +Oracle

    +MS SQL Server

    +ODBC etc.


  • PHP is cross platform; this means you can deploy your application on a number of different operating systems such as windows, Linux, Mac OS etc.



In any programming language, global variables are those variables, which are declared outside the method or functions, also they can be declared inside of the functions as well. Global variable is just like any other variable but the difference is that this scope is global in application. If we make any variable global then we can access that variable from our whole application, which means inside or outside of the script as well. Global variable functions the same way everywhere, as the name, suggests they are global for other resources. In the coming section, we will discuss more this PHP Global Variable in detail.


Syntax

$variable


As you can see here we just need to define the variable name but for this, we use ‘$’ symbol. After this, we can assign any value to the variable we want. For better understanding, we will see one practice syntax which can be used in the program while coding:


Example

$mywebsiteVar="reanits.com"

How Global Variable work in PHP?

As now we know that global variables is declared globally to use anywhere in the application. This variable like any other variable in PHP. In addition, these variables can be accessed from inside or outside of the function as well. As we have discussed earlier also that we just declare them like other variables, but in order to access them we need to follow some standard defined by the PHP. So here, we will discuss how to use them inside the function and how to declare them.


Global Array

In PHP we use an array to access this global variable. Like any other programming language, it maintains the history of the global variable in an array. If we want to access any particular element or variable from the array then we have to pass the exact name of the variable in order to access them. Let’s see the syntax for this see below


Syntax

$GLOBALS[‘Variable_Name’]

As you can see in the above lines of code we are using ‘$GLOBALS’ keyword to access them followed by the square bracket. Inside this bracket


Example

$GLOBALS['total']

2) Access them inside the function in PHP

IF we want to access the global variable inside any function or method then we can use the ‘global’ keyword. After mentioning the variable name with this we can use them throughout the function without the global keyword as well.


Syntax

Global @id,@student,@Gender,@DOB
Echo @Student;

Here are some points which needs to be taken into consideration while using the global keyword in PHP


-IF you want to define a global variable you can use ‘$’ followed by the name of your variable.


-This variable can be accessed inside or outside of the functions well but to access them we have to ‘global’ keyword.


-We access the global variable inside nay thing by using the global array which maintains all the variables defined in the application.


-But keep in mind that the variable should be unique otherwise it will override like any other programming language.


-To access the variable directly from the array we have to use ‘$GLOBALS’ keyword followed by the variable name like we access an array by its index


-The advantage of using a global variable is that we may require logged in user names everywhere in the application so by the use of it we can store them into the global variable and that can we easily accessed while application. Which will save memory and provide code optimization also.


Example

<html>
<head>
<title>GLOBALS </title>
</head>
<body>
<?php
$a=40;
$b=70;
function sum()
{
$GLOBALS['total']=$GLOBALS['a']+ $GLOBALS['b'];
}
sum();
echo $total;
?>
</body>
</html>

Note: This is a 'super global', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.

Please Watching My Video is Below

No comments:

Post a Comment

Post Top Ad