变量名变量?循环说变量?

时间:2017-03-09 14:35:52

标签: javascript function variables

所以我有一大堆代码......我希望通过将它缩小到几行而不是十二行来提高它的效率。我的想法是使用循环的变量按顺序调用每个变量,因为代码每次只重复不同的数字。有什么方法可行吗?

var usetext1 = getText("text1");
var usetext2 = getText("text2");
var usetext3 = getText("text3");
var usetext4 = getText("text4");
var usetext5 = getText("text5");
var usetext6 = getText("text6");
usetext1 = usetext1.toUpperCase();
usetext2 = usetext2.toLowerCase();
usetext3 = usetext3.toLowerCase();
usetext4 = usetext4.toLowerCase();
usetext5 = usetext5.toLowerCase();
usetext6 = usetext6.toLowerCase();

缩小为:

for (var i=2;i<6;i++){
var usetext[i]=getText("text[i]");
usetext[i]=usetext[i].toLowerCase();

1 个答案:

答案 0 :(得分:1)

您可以使用Template Literals将值存储到<?php $conn=mysqli_connect("localhost","mydatabase","mypassword") or die("Could not connect:".mysqli_error($conn)); mysqli_select_db($conn , 'mydatabase') or die ('Database will not open'); if(isset($_POST['register'])){ $fname = ($_POST['FirstName']); $lname = ($_POST['LastName']); $dob = ($_POST['dob']); $school = ($_POST['SchoolName']); $username = ($_POST['Username']); $pass = ($_POST['Pass']); $query = "INSERT INTO users (FirstName,LastName,dob,SchoolName,Username,Pass)VALUES('$FirstName','$LastName','$Username','$dob', '$SchoolName','$Pass',)"; $result = mysqli_query($conn,$query); } ?>

array