How to pass many (over 100) variables into PHP

Asked
Active3 hr before
Viewed126 times

8 Answers

variables
90%

Standard method of passing data using POST requests where you have them available as part of $_POST array is perfectly fine. This method has very high upper limit of amount of data that can be sent.,I know about the conventional $_POST methods of getting variables into PHP. However, is that still the most effective solution of passing variables if the number of variables is so high? ,You can use xml variable that post through ajax request such as MVC concept. First you set a mount of xml variabel. Second you post this xml variable as a text. View PHP post in through ajax request and model php will recieve xml text data and decompose it with xml query to get the same amount of variable.,Basically, my point here is that even with a slow connection, HTTP and your server are well capable of handling that many fields. I'm not sure how long it would take for PHP to parse all of them (you'd have to benchmark it on your server), but I imagine the impact will be negligible.

This kind of load is certainly manageable depending on how much data you expect each field to contain, or rather, the maximum amount of data you determined each field can contain. In PHP, the maximum size of the body of an HTTP POST request (that's the part that contains the form encoded values) is determined by the ini value post_max_size. It has a default of 2MB, but you can change this in your php.ini:

post_max_size = 10 M # megabytes

Or in your .htaccess:

php_value post_max_size 10 M

From a user's standpoint, I'd say that 100 fields would be a pretty daunting sight. If at all possible, it might be nicer to separate your form into friendlier and smaller steps that walk the user through the process of filling out the form. If you would rather not split the form into steps, at least look into saving the state of the form with javascript. Note that in the W3C recommends 5MB of storage space for localStorage, so this should be plenty of space to store all of your fields. Also look at this fallback that uses cookies, but be weary. Cookies have more limits than localStorage. I've read that cookies are limited to 4KB each and 20 cookies per domain. You might want to distribute your stored form fields into several cookies, say 10 form fields in 10 cookies. You can store multiple input values in a cookie using encodeURIComponent():

var inputs = document.forms[0].getElementsByTagName('input')
i = 0,
   date = new Date(),
   expires;

// Expires date (1 day in future)
date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
expires = date.toGMTString();

for (var cookieNumber = 0; cookieNumber < 10; cookieNumber++) {
   var cookie = [];
   for (; i < (cookienumber * 10 + 10); i++) {
      cookie.append(encodeURIComponent(inputs[i].name) + '=' + encodeURIComponent(inputs[i].value));
   }

   document.cookie = 'savedForm' + cookieNumber + '=' + cookie.join('&') + '; expires =' + expires;
}
load more v
88%

Currently in my code the user inputs information into over 100 input texts and then this information is passed into PHP using the $_POST method. However, I don't think this will fare too well with many servers. ,I know about the conventional $_POST methods of getting variables into PHP. However, is that still the most effective solution of passing variables if the number of variables is so high? ,Standard method of passing data using POST requests where you have them available as part of $_POST array is perfectly fine. This method has very high upper limit of amount of data that can be sent.,For all the people saying it would simply be a UI disaster, that's something that's been consulted many times. However, the submit page is essentially to be the heart of the website and there are reasons why the user would not be dissuaded by the high amount of inputs- in fact the default number is 20 but the list can be expanded to 100 inputs which is an option implemented FOR the user.

This kind of load is certainly manageable depending on how much data you expect each field to contain, or rather, the maximum amount of data you determined each field can contain. In PHP, the maximum size of the body of an HTTP POST request (that's the part that contains the form encoded values) is determined by the ini value post_max_size. It has a default of 2MB, but you can change this in your php.ini:

post_max_size = 10 M # megabytes

Or in your .htaccess:

php_value post_max_size 10 M

From a user's standpoint, I'd say that 100 fields would be a pretty daunting sight. If at all possible, it might be nicer to separate your form into friendlier and smaller steps that walk the user through the process of filling out the form. If you would rather not split the form into steps, at least look into saving the state of the form with javascript. Note that in the W3C recommends 5MB of storage space for localStorage, so this should be plenty of space to store all of your fields. Also look at this fallback that uses cookies, but be weary. Cookies have more limits than localStorage. I've read that cookies are limited to 4KB each and 20 cookies per domain. You might want to distribute your stored form fields into several cookies, say 10 form fields in 10 cookies. You can store multiple input values in a cookie using encodeURIComponent():

var inputs = document.forms[0].getElementsByTagName('input')
i = 0,
   date = new Date(),
   expires;

// Expires date (1 day in future)
date.setTime(date.getTime() + (24 * 60 * 60 * 1000));
expires = date.toGMTString();

for (var cookieNumber = 0; cookieNumber < 10; cookieNumber++) {
   var cookie = [];
   for (; i < (cookienumber * 10 + 10); i++) {
      cookie.append(encodeURIComponent(inputs[i].name) + '=' + encodeURIComponent(inputs[i].value));
   }

   document.cookie = 'savedForm' + cookieNumber + '=' + cookie.join('&') + '; expires =' + expires;
}
load more v
72%

<
? php
$a = "Hello ";
$b = $a.
"World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a. = "World!"; // now $a contains "Hello World!"
?
>
load more v
65%

You can pass a variable by reference to a function so the function can modify the variable. The syntax is as follows: ,Note: There is no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. , References returned from functions, i.e.: , No other expressions should be passed by reference, as the result is undefined. For example, the following examples of passing by reference are invalid:

<
? phpfunction foo( & $var) {
   $var++;
}
$a = 5;
foo($a); // $a is 6 here?>
load more v
75%

The PHP language has evolved significantly over the course of its 20 year history. Familiarizing oneself with its subtleties is a worthwhile endeavor, as it will help ensure that the software you produce is more scalable, robust, and maintainable.,PHP makes it relatively easy to build a web-based system, which is much of the reason for its popularity. But its ease of use notwithstanding, PHP has evolved into quite a sophisticated language, with many nuances and subtleties that can bite developers, leading to hours of hair-pulling debugging. This article highlights ten of the more common mistakes that PHP developers need to beware of.,PHP makes it relatively easy to build a web-based system, which is much of the reason for its popularity. But its ease of use notwithstanding, PHP has evolved into quite a sophisticated language with many frameworks, nuances, and subtleties that can bite developers, leading to hours of hair-pulling debugging. This article highlights ten of the more common mistakes that PHP developers need to beware of.,PHP’s ease of use can lull developers into a false sense of comfort, leaving themselves vulnerable to lengthy PHP debugging due to some of the nuances and idiosyncrasies of the language. This can result in PHP not working and problems such as those described herein.

Not sure how to use foreach loops in PHP? Using references in foreach loops can be useful if you want to operate on each element in the array that you are iterating over. For example:

$arr = array(1, 2, 3, 4);
foreach($arr as & $value) {
   $value = $value * 2;
}
// $arr is now array(2, 4, 6, 8)
load more v
40%

In PHP, a variable starts with the $ sign, followed by the name of the variable:,A variable starts with the $ sign, followed by the name of the variable,Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.,The PHP echo statement is often used to output data to the screen.

Creating (Declaring) PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:

$
load more v
22%

This works very well, my issue is that I will need to pass 100+ variables to the python script. I don't want this exec line to become extremely long and unmanageable. I have also explored passing a php array instead of a variable with the following code:,or in short, assuming $phpVariablesToPass is an array filled with your values:,Store your PHP variables within a temporary text file then use python to read that file. ,is the cleanest and most explicit way to achive this. This is the way most people would recommend, if you are sure you even need to reassign a string to a bool variable.

I am using the following code to initiate a python script and pass a php variable to it.

$tmp = exec("python path/to/pythonfile.py $myVariable $mySecondVariable", $output);

This works very well, my issue is that I will need to pass 100+ variables to the python script. I don't want this exec line to become extremely long and unmanageable. I have also explored passing a php array instead of a variable with the following code:

$checked = array(
   "key1" => "1"
   "key2" => "1"
   "key3" => "1"
);
$checkedJson = json_encode($checked);
$tmp = exec("python path/to/pythonfile.py $myVariable $checkedJson", $output);
load more v
60%

Early PHP was not intended to be a new programming language, and grew organically, with Lerdorf noting in retrospect: "I don't know how to stop it, there was never any intent to write a programming language [...] I have absolutely no idea how to write a programming language, I just kept adding the next logical step on the way."[20] A development team began to form and, after months of work and beta testing, officially released PHP/FI 2 in November 1997. ,Text-oriented programming languages,W3Techs reports that, as of April 2021[update], "PHP is used by 79.2% of all the websites whose server-side programming language we know."[15] ,"Attributes", often referred to as "annotations" in other programming languages, were added in PHP 8, which allow metadata to be added to classes.[67]

<!--include /text/header.html-->

<!--getenv HTTP_USER_AGENT-->
<!--if substr $exec_result Mozilla-->
Hey, you are using Netscape!<p>
   <!--endif-->

   <!--sql database select * from table where user='$username'-->
   <!--ifless $numentries 1-->
   Sorry, that record does not exist
<p>
   <!--endif exit-->
   Welcome
   <!--$user-->!
<p>
   You have
   <!--$index:0--> credits left in your account.
<p>

   <!--include /text/footer.html-->
load more v

Other "variables-undefined" queries related to "How to pass many (over 100) variables into PHP"