frame

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In Register

PHP Superglobals

PHP Superglobals

$GLOBAL - Used to access global variables from anywhere in the PHP script

<!DOCTYPE html>
<html>
<body>


<?php 
$x = 75;
$y = 25; 


function addition() {
  $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}


addition();
echo $z;
?>


</body>
</html>


$_SERVER - Holds information about headers, paths, and script locations

<!DOCTYPE html>
<html>
<body>


<?php 
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>


</body>
</html>


$_REQUEST - Used to collect data after submitting an HTML form

<!DOCTYPE html>
<html>
<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = htmlspecialchars($_REQUEST['fname']); 
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>


</body>
</html>


$_POST - Used to collect form data after submitting an HTML form. Also used to pass variables

<!DOCTYPE html>
<html>
<body>


<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  Name: <input type="text" name="fname">
  <input type="submit">
</form>


<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // collect value of input field
  $name = $_POST['fname']; 
  if (empty($name)) {
    echo "Name is empty";
  } else {
    echo $name;
  }
}
?>


</body>
</html>

$_GET - Collect data sent in the URL

<!DOCTYPE html>
<html>
<body>


<a href="test_get.php?subject=PHP&web=W3schools.com">Test $GET</a>


</body>
</html>


Tagged:
Sign In or Register to comment.

Developer

| make vanilla clean and light
@ 2016 Your Sitename, All rights reserved. Material Design is a design language developed by Google. Material Design makes more liberal use of grid-based layouts and responsive animations and transitions.
Powered by VanillaForums, Designed by ThemeSteam

Contact us

info@example.com
support@example.com
(800) 3032120

Get In Touch