How to get POST array value in PHP

This article will introduce the basic usage of PHP’s post array to get a value from an input form. The article will also highlight some important points about how to use post arrays, what they are used for, and where you can find more information about them.

PHP provides an array data type for storing collections of values. The POST variable is a form input that gets sent to your PHP code by the web browser, and it allows you to store multiple values passed in from the HTML form.

How to get POST array value in PHP

An Introduction To POST Arrays in PHP.

A POST array stores data that was sent to the server by a form.

Both these methods place limitations on what can be sent to the server, GET requests are limited to 8-bit ASCII and browsers may prevent more than 2kb of data from being submitted in a single transaction.

POST arrays can store extremely large amounts of data without any limitations. Imagine you have an HTML form that asks people for their entire resume, in this case, it would make sense to save the data submitted by the form to a POST array.

How are POST arrays created?

Creating a new PHP post array is different than creating a normal array because you must tell PHP which special variable name it should use to store the post data.

There are many special variables that PHP uses, but we’re only concerned with $_POST. $_POST contains all values submitted via the HTTP POST method. $_REQUEST – Contains all values submitted via any method (GET, POST, COOKIE, etc).

As mentioned earlier GET requests can’t send very much data at once and as such, they shouldn’t be used for sending large amounts of information (like user uploads) or large forms (like user resumes).

How to get POST array value in PHP

To get all post-type data we have to use the $_POST superglobal variable. You can retrieve the value of a specific key using $_POST['key'] syntax but if the key does not exist then this returns an empty string.

If you want to check that a given key exists or not, use the isset() function, for example:

if (isset($_POST['key'])){
	echo 'Key exists';
}else {
	echo 'Key does NOT exists';
}

If you want to know whether the given POST array have any content, use empty() function, for example:

if (empty($_POST['key'])){
	echo 'No content';
}else {
	echo 'Content exists';
}

Retrieve post array values

If you want to pass an array into post variable you can do this like below-

<form action="demo.php" method="post">
	Cooking <input type="checkbox" name="hobbies[]" value="1"><br/>
	Singing <input type="checkbox" name="hobbies[]" value="2"><br/>
	Playing <input type="checkbox" name="hobbies[]" value="3"><br/>
	Swimming <input type="checkbox" name="hobbies[]" value="4"><br/>
	
	<button type="submit">Submit</button>
</form>

We now want to see which hobby the user has selected.

<?php
if( isset($_POST['hobbies']) ){
	$hobbies = $_POST['hobbies'];
	
	
	foreach($hobbies as $hobby) {

	   echo $hobby . "<br>";

	}
}
?>

If the user selects Cooking and Playing, then it will be showing 1, 3.

Final Word

This blog post has been a quick guide to understanding how you can get an array value in PHP. We hope that this information will help you solve your problems and provide a deeper knowledge of the language, but if it doesn’t please feel free to contact us with any questions or concerns.

About Ashis Biswas

A web developer who has a love for creativity and enjoys experimenting with the various techniques in both web designing and web development. If you would like to be kept up to date with his post, you can follow him.

Leave a Comment