$GLOBALS in PHP

learnWebCoding
16 Mar 201610:59

Summary

TLDRIn this tutorial, Durgesh Sahni introduces PHP's super global variables, explaining how they are used to access form values across different pages. He covers key super globals like `$_GET`, `$_POST`, `$_SERVER`, `$_COOKIE`, and `$_SESSION`, demonstrating how they function in PHP to retrieve and manipulate data. The video also includes practical examples, such as accessing and modifying variable scopes with the `global` keyword and understanding variable access within functions. By the end of the tutorial, viewers will grasp the foundational concepts of super global variables and their usage in PHP development.

Takeaways

  • 😀 Super Global Variables in PHP allow access to values across different forms and scopes in a stateless protocol like HTTP.
  • 😀 The 'GET' and 'POST' methods are commonly used for passing values between forms in PHP, with 'GET' being the default method.
  • 😀 To access form values using the 'GET' or 'POST' method, PHP provides the super global variables $_GET and $_POST.
  • 😀 The $_SERVER super global provides access to server-related information, which can be accessed with the '$_SERVER' variable.
  • 😀 Cookies and sessions in PHP can be accessed and managed using their respective super global variables: $_COOKIE and $_SESSION.
  • 😀 PHP functions may require the use of the 'global' keyword to access variables outside the function's scope.
  • 😀 Variables declared outside a function may not be directly accessible inside it unless the 'global' keyword is used.
  • 😀 Using the '$GLOBALS' super global allows access to any global variable from anywhere within the script, including inside functions.
  • 😀 PHP functions have local scopes, meaning variables declared within them cannot be accessed outside unless explicitly returned or handled by super global variables.
  • 😀 To access a global variable inside a function, one must use '$GLOBALS' or declare the variable as 'global' within the function.
  • 😀 The scope of a PHP variable determines its accessibility. Variables outside the function cannot be accessed directly unless specified using super global variables.

Q & A

  • What are super global variables in PHP?

    -Super global variables in PHP are built-in global arrays that are accessible from any part of the script, including inside functions. They are used to collect data from forms, server variables, cookies, and sessions.

  • Why do we need super global variables in PHP?

    -Super global variables are essential because they allow data to be accessed globally throughout a PHP script, making it possible to retrieve and manipulate information from different sources like forms, URLs, and server settings.

  • What is the difference between $_GET and $_POST methods in PHP?

    -The `$_GET` method is used to collect form data sent via the URL (query string), while `$_POST` is used to collect form data sent through HTTP POST, which is more secure as the data is not visible in the URL.

  • How does PHP handle variable scope with respect to super global variables?

    -Super global variables in PHP are accessible from anywhere in the script, including inside functions, without needing to pass them explicitly. However, other non-super global variables are only accessible within their defined scope.

  • What error occurs when trying to access a non-global variable inside a function in PHP?

    -If a non-global variable is accessed inside a function without declaring it as global, PHP will throw an 'undefined variable' error, as the variable is out of the function's scope.

  • How can we access a global variable inside a PHP function?

    -To access a global variable inside a PHP function, you need to use the `global` keyword followed by the variable name or access it using the `GLOBALS` array.

  • What is the purpose of the `$_SERVER` super global in PHP?

    -The `$_SERVER` super global contains information about headers, paths, and script locations. It can be used to get details like the server's hostname, script name, or request method.

  • What happens if a global variable is not declared inside a function when using the `global` keyword?

    -If a global variable is not declared inside the function using the `global` keyword, PHP will not be able to access it, resulting in an 'undefined variable' error.

  • What is the role of the `$_COOKIE` and `$_SESSION` super globals?

    -The `$_COOKIE` super global is used to retrieve values from cookies set on the user's browser, while the `$_SESSION` super global is used to store session data across page requests.

  • How can a variable be accessed globally if it is declared inside a function?

    -If a variable is declared inside a function and you want to access it globally, you can use the `global` keyword or store it in the `GLOBALS` array to make it available outside the function.

Outlines

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Mindmap

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Keywords

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Highlights

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora

Transcripts

plate

Esta sección está disponible solo para usuarios con suscripción. Por favor, mejora tu plan para acceder a esta parte.

Mejorar ahora
Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
PHPSuper GlobalVariablesTutorialGET MethodPOST MethodWeb DevelopmentPHP FunctionsSession VariablesCookie HandlingProgramming Basics
¿Necesitas un resumen en inglés?