Is there a more efficient way of doing this? I am simply catching and assigning vars to a lot of $POST vars. this seem monotonous to me..lol any advise is greatly appreciated. thanks :) sorry aboout the title..wasn't sure how to title this one. lol

if(isset($_POST["submit"]) ){
	if(isset($_POST["est_num"]) ){$est_num= $_POST["est_num"];}
	if(isset($_POST["first_name"]) ){$first_name= $_POST["first_name"];}
	if(isset($_POST["last_name"]) ){$last_name= $_POST["last_name"];}
	if(isset($_POST["address"]) ){$address= $_POST["address"];}
	if(isset($_POST["address_2"]) ){$address_2= $_POST["address_2"];}
	if(isset($_POST["city"]) ){$city= $_POST["city"];}
	if(isset($_POST["state"]) ){$state= $_POST["state"];}
	if(isset($_POST["zip"]) ){$zip= $_POST["zip"];}
	if(isset($_POST["phone"]) ){$phone= $_POST["phone"];}
}

Dani AI

Generated

Brief recap and practical takeaways: was manually assigning many $_POST fields; suggested looping to create variables dynamically and mentioned using list. Those shorten typing but introduce maintainability and safety problems. Dynamically creating variable names from untrusted input makes the code harder to audit and can lead to accidental collisions; ordered destructuring (list) assumes a stable numeric order, which POST data does not guarantee.

A safer, clearer pattern is: declare a whitelist of expected fields, validate/sanitize them in one pass, and keep results in a dedicated array (instead of auto-creating variables). This centralizes logic, gives sensible defaults, and makes it easy to validate types before use. Example workflow that works across PHP versions: define filters for each expected key, run filter_input_array(INPUT_POST, $filters), then map required entries to local variables or use the sanitized array directly. This avoids the pitfalls of variable variables and keeps all input handling in one place (and also simplifies unit testing).

Extra tips: trim inputs and enforce types (use validation filters for integers, regex for phones/zips). Never trust client-side checks; validate server-side before DB use and always use prepared statements for queries. For reference on input filtering and the risks of variable variables, see the PHP docs for filter_input_array and variable variables, and OWASP guidance on input validation:

Hi,

you may use the following codes:

//to retrieve all POST variables and value
foreach ($_POST as $key => $value){ 
	$$key = $value; 
}

simple enough..lol thanks for that, very useful and simple. But i couldnt think of it.. ha ha Much appreciated. :)

No problem :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.