简体   繁体   English

在定义变量后将变量设置为空(“”)的目的是什么?

[英]what is the purpose of setting variables to empty (“ ”) after defining them?

Im learning about validating form input data and there was this bit of code on w3schools that i don't understand. 我正在学习验证表单输入数据,w3schools上有一些我不理解的代码。 see below: 见下文:

<?php
// define variables and set to empty values
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";

//bla bla bla

i dont understand what "// define variables and set to empty values" means. 我不明白“ //定义变量并设置为空值”的意思。

Don't use this tutorial. 不要使用本教程。 I'm looking at this test_input() function and can't believe that someone would publish this code: 我正在看这个test_input()函数, test_input()不敢相信有人会发布以下代码:

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

See my answer here for more information on why this is a bad idea, and what to do instead: https://stackoverflow.com/a/7810880/362536 在这里查看我的答案以获取更多关于为什么这是一个坏主意以及如何做的更多信息: https : //stackoverflow.com/a/7810880/362536

Now, on to your actual question. 现在,讨论您的实际问题。 In many languages, you are required to declare what variables you are going to use before you use them. 在许多语言中,要求您在使用变量之前先声明要使用的变量。 PHP is not one of these languages. PHP不是这些语言之一。 Some people think that your code is easier to read if you know what all the variables are up front, and will define these variables as a matter of code style. 某些人认为,如果您知道所有变量都在前面,那么您的代码将更易于阅读,并将这些变量定义为代码样式。 That is, they will list variables and set them to some value. 也就是说,它们将列出变量并将其设置为某个值。 The code you have: 您拥有的代码:

$nameErr = $emailErr = $genderErr = $websiteErr = "";

... is functionally equivalent to: ...在功能上等同于:

$nameErr = '';
$emailErr = '';
$genderErr = '';
$websiteErr = '';

In PHP, you can chain your variable assignments, which is why these two are equivalent. 在PHP中,您可以链接变量分配,这就是为什么这两个等效的原因。

If you choose to declare your variables as a matter of style, you should not assign an empty string to them. 如果选择声明变量是出于样式问题,则不应为它们分配空字符串。 You should use null instead. 您应该改用null Suppose I have a variable that is conditionally assigned data from a user input field. 假设我有一个变量,该变量是有条件地从用户输入字段分配的数据。 If I use empty string '' to initialize variables, I have no way of knowing if the form field existed and was submitted or not. 如果我使用空字符串''初始化变量,则无法知道表单字段是否存在以及是否已提交。 If I use null , I can then use isset() to determine if the variable has an empty form field (empty string '' ) or null as its value. 如果使用null ,则可以使用isset()确定该变量是否具有一个空的表单字段(空字符串'' )或null作为其值。

You may very well decide that empty string initialization is right for your application. 您可以很好地确定空字符串初始化适合您的应用程序。 As John Conde says in the comments above, many folks use this so they can freely concatenate variables without worrying about what's in them, as even if they don't get an explicit value assigned, you already assigned an empty string. 就像约翰·康德在上面的评论中说的那样,许多人都在使用它,因此他们可以自由地连接变量而不必担心变量中的内容,即使没有分配显式值,您也已经分配了一个空字符串。 I would argue that there is rarely a time when you will work with data that always maintains a single purpose throughout it's lifetime, and that it's best to have data in the cleanest representation as possible. 我会争辩说,很少有时间使用在整个生命周期中始终保持单一目的的数据,并且最好以最清晰的表示形式存储数据。 If a variable is intended to be null , its value should indeed be null and not an empty string. 如果变量旨在为null ,则其值的确应该为null而不是空字符串。 What you choose is up to you and your specific circumstances. 您的选择取决于您和您的具体情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM