PHP built-in variables and functions

From Knowledge Kitchen
Jump to navigation Jump to search


Built-in functions

PHP has many built-in functions. Knowledge of only a few of them will go a long way.

Useful string functions

Useful array functions

Useful MySQL database-related functions

Error reporting functions

HTTP header function

  • header("Location:somepage.php") - sends a custom HTTP header that tells the browser that the page it has requested has moved to a new location. This causes the browser to request the file at the new location instead. This is called a browser redirect.

A full list of built-in PHP functions

This example script generates a web page that contains a list of all the functions built into the PHP API.

<!doctype html>
<html>
	<head>
		<title>Built-in PHP Functions</title>
		<meta charset="utf-8" />
	</head>
	<body>
		<h1>List of all Built-in PHP Functions</h1>

		<pre>

<?php

//this example shows a list of all the built-in function names that exist in the PHP language

$x = get_defined_functions(); //gets an array of all the built-in function names in PHP
print_r($x); //print out the contents of the array

/*
echo "Hello world!"; //the old-school way of printing something
print("Hello world!"); //exactly the same as echo, but a more modern way of doing a function call
*/

?>

		</pre>
	</body>
</html>

Built-in variables

PHP's built-in variables are most useful when passing data between a client, such as a web browser, and the server. PHP comes included with what is essentially a simple web framework.

$_GET variable

This document shows how to put data into PHP's built-in $_GET variable. The $_GET variable stores any data sent to it via an HTTP GET request as an associative array.

Save this into a file named php_built_in_get_variable.php

<!doctype html>
<html>
	<head>
		<title>PHP's Built-In $_GET Variables</title>
		<meta charset="UTF-8" />
	</head>
	<body>
		<div class="container">

 			<h1>PHP's Built-in $_GET Variable</h1>
 			<p>Enter something in the query string of the URL, and you'll see it show up as an element in this built-in variable.  For example:
 				<ul>
 					<li>
 						<a href="php_built_in_get_variable.php?keywords=slippers+men&handy=dandy">
 							Men's Slippery Slippers
 						</a>
 					</li>
 					<li>
 						<a href="php_built_in_get_variable.php?dog=furry&keywords=clogs+women">
 							Women's Clugging Clogs
 						</a>
 					</li>
 					<li>
 						<a href="php_built_in_get_variable.php?moo=cow&keywords=sandals+infants&albatross=dental_floss">
 							Sandwood Sandals for Infants
 						</a>
 					</li>
 				</ul>
 			</p>

			<h2>All data in the built-in $_GET variable:</h2>
			<p>The print_r() function in PHP is useful for debugging.  It outputs the raw data stored within an array in a very inelegant way</p>
			<pre><?php print_r($_GET); ?></pre>

			<h2>Some data from the built-in $_GET variable:</h2>
			<p>The following is the value corresponding to the key 'keywords' in PHP's built-in $_GET <a href="http://cs.nyu.edu/~amos/download/index.php?title=Associative_Arrays_in_PHP">associative array</a>:</p>
			<pre><?php print($_GET['keywords'); ?></pre>

		</div>
	</body>
</html>

$_POST variable

This document shows how to put data into PHP's built-in $_POST variable. The $_POST variable stores data as an associative array.

Save this into a file named php_built_in_post_variable.php

<!doctype html>
<html>
	<head>
		<title>PHP's Built-In $_POST Variables</title>
		<meta charset="UTF-8" />
	</head>
	<body>
		<div class="container">

		<h1>PHP's Built-in $_POST Variable</h1>
		<p>For example, if the user submits a form via the POST method, the data they enter into the form will appear in this variable</p>
		<form action="php_built_in_post_variable.php" method="POST">
			<label>Enter your first name:</label>
			<input type="text" name="first_name" />
			<br />
			<label>Select your state</label>
			<select name="state">
				<option>-- select your state --</option>
				<option value="NY">New York</option>
				<option value="NJ">New Jersey</option>
				<option value="CT">Connecticut</option>
			</select>
			<input type="submit" value="Go!" />
		</form>

		<h2>The State you selected</h2>
		<?php 
		//a associative array of state acronyms and their corresponding names
		$states = array(
			"NY" => "New York",
			"NJ" => "New Jersey",
			"CT" => "Connecticut"
		);

		//get the state the user selected in the form and save in a variable
		$state = $_POST['state'];

		//figure whether the user selected a state or not yet
		if (empty($state)) {
			//output a default message if no state selected
			echo "<p>You have not selected a state yet.</p>";
		}
		else {
			//output the nice name of the state the user selected
			echo "<p>" . $states[$state] . "</p>";		
		}

		?>

		<h2>All data in the built-in $_POST variable:</h2>
		<pre><?php print_r($_POST); ?></pre>
			
		</div>
	</body>
</html>

$_SERVER variable

This document shows how to put data into PHP's built-in $_SERVER variable. The $_SERVER variable stores data as an associative array.

Save this into a file named php_built_in_server_variable.php

<!doctype html>
<html>
	<head>
		<title>PHP's Built-In $_SERVER Variable</title>
		<meta charset="UTF-8" />
	</head>
	<body>
		<div class="container">

		<h1>PHP's Built-In $_SERVER Variables</h1>

		<h2>Browser a.k.a. User Agent</h2>
		<p>This the identifier of the browser you're using to view this page.  This variable would be important to you if you were doing browser detection!</p>
		<p><?php echo $_SERVER['HTTP_USER_AGENT']; ?></p>

		<h2>Script URI</h2>
		<p>This is the address you can plug into a web browser to see this page</p>
		<p><?php echo $_SERVER['SCRIPT_URI']; ?></p>

		<h2>Script URL</h2>
		<p>This is the path to this script from the web root</p>
		<p><?php echo $_SERVER['SCRIPT_URL']; ?></p>

		<h2>HOST NAME</h2>
		<p>This is the domain name</p>
		<p><?php echo $_SERVER['HTTP_HOST']; ?></p>


		<h2>All data in the built-in $_SERVER variable:</h2>
		<pre><?php print_r($_SERVER); ?></pre>
			
		</div>
	</body>
</html>


What links here