PHP Beginner's ...LOGIN

PHP Beginner's Introduction to Superglobal Variables

In PHP, many predefined variables are superglobal variables, which means that they can be used within the scope of a script, and they can be accessed in functions or methods without executing global $variable;

The super global variables we will learn below

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

In this chapter we will explain several commonly used Super global variables, we will introduce the other variables in the next few chapters.

1. $GLOBALS

The local variable group can be accessed in the entire scope of a PHP script.

It is a group that contains Global combined array of all variables. The name of the variable is the key of the array

<?php 
	$x = 75; 
	$y = 25;
	 
	function addition() { 
		$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; 
	}
	 
	addition(); 	//调用函数
	echo $z; 
?>

2. $_SERVER

is a variable that contains information such as header, path, and script location ( script locations) and other information. The items in this array are created by the Web server.

There is no guarantee that every server will provide all items; the server may ignore some, or provide some that are not here

Listed items

12.png

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

3. $_REQUEST

$_REQUEST is used to collect data submitted by HTML forms

<html>
<body>
<form method="post" action=""
ype="text" name="name">
	<input type="submit">
</form>

<?php 
	$name = $_REQUEST['name']; 
	echo $name; 
?>

</body>
</html>

4. $_POST and $_GET

are widely used to obtain form data

PHP $_POST is widely used to collect form data. Specify this attribute of the HTML form tag: "method="post".

The following example displays a form with an input field (input) and a submit button (submit). When the user clicks When the "Submit" button submits the form data, the form data will be sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to process the form data if you want other PHP files to process it. For this data, you can modify the specified script file name. Then, we can use the super global variable $_POST to collect the input field data in the form:

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

<?php 
	$name = $_REQUEST['name']; 
	echo $name; 
?>

</body>
</html>

PHP $_GET is also widely used To collect form data, specify this attribute in the HTML form tag: "method="get".

$_GET can also collect data sent in the URL.

Tip: If you want to learn more about $_POST and $_GET, please visit our PHP Forms chapter


Next Section
<?php $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y']; } addition(); //调用函数 echo $z; ?>
submitReset Code
ChapterCourseware