PHP:explode() 中未定义的偏移量
我有这个:
list($firstname, $lastname) = explode(' ', $queryString);
有时 $lastname 没有被定义,并且我在那里遇到未定义的偏移错误。
我猜是因为它找不到任何可以放入 $lastname 的内容。
在explode()之后我有:
if(!$lastname) { $lastname = $firstname; }
所以我的问题是如果未定义$lastname,我如何将它定义为$firstname(如果你只写了'Adam'而不是'Adam Thompson',那么应该定义姓氏,所以它是'Adam Adam')
现在它为我做到了这一点,但我收到了偏移错误
I have this:
list($firstname, $lastname) = explode(' ', $queryString);
Sometiems $lastname does not gets defined, and it's there i am getting undefined offset error.
Because it can not find anything to put in $lastname, i guess.
After the explode() i have:
if(!$lastname) { $lastname = $firstname; }
So my question is how can i define it as the $firstname if $lastname is not defined (if you wrote only 'Adam' and not 'Adam Thompson', the lastname should be defined so it is 'Adam Adam')
It does this for me now, but I am receiving the offset error
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
explode()
中的2
确保最多有 2 个值,而array_pad()
确保至少 2 个值。如果没有空格字符,则
$lastname
为null
。您可以用它来决定下一步要做什么。 小更新:对于这种特定情况,您可以使用一个小技巧,
这将一步完成所有操作。它应该可以工作,因为
$firstname
)$queryString == $firstname
。这就是现在用于填充数组最多 2 个值的值(这正好是一个,因为我们已经有一个值)$queryString
填充,因为我们已经有 2 个值至少为了可读性我更喜欢第一个更明显的解决方案。
The
2
inexplode()
ensures, that there are at most 2 values andarray_pad()
ensures, that there are at least 2 values. If there is no space character,
$lastname
isnull
. This you can use to decide what comes nextLittle update: For this specific case you can use a little trick
This will do all that in one step. It should work, because
$firstname
)$queryString == $firstname
. Thats now the value that is used to fill the array up to 2 values (which is exactly one, because one value we already have)$queryString
, because we already have 2 valuesAt least for readability I would prefer the first more obvious solution.
尝试附加一个空格:
之后不必更改任何内容。
Try appending a space:
shouldn't have to change a thing after that.
您收到的不是错误,而是通知。
虽然这是可以接受的,因为 PHP 是一种动态语言,但您可以使用
isset( )
:更新
根据评论,
list()
是通知的罪魁祸首。在这种情况下,当explode()
未产生适当数量的参数时,我不建议使用list()
。如果必须,请通过 brady 或 使用时未定义的偏移量来回答phpexplode()可以工作。尽管在我看来它相当丑。我相信如果您执行以下操作,您的代码会更加直观:
You're not getting an Error, but a Notice.
Although this is acceptable since PHP is a dynamic language, you can prevent it with
isset()
:UPDATE
Per the comments,
list()
is the culprit for the Notice. In which case, I wouldn't recommend the use oflist()
whenexplode()
doesn't yield the appropriate number of parameters.If you must, the answer by brady or undefined offset when using php explode() can work. Although it's pretty ugly in my opinion. I believe your code would be much more intuitive if you just did the following:
我今天刚遇到这个。我的解决方案不是上面的,
(没有效果)我的如下:
而不是这样做:
I just ran into this today. my solution was not the above,
(which had no effect) mine was the following:
instead of doing:
我不清楚为什么会这样,但通知会消失。首先,使用这段代码,我得到了未定义的偏移通知:
但是,使用这段代码,我没有:
另请注意,在 $dateToChange 后附加“-”或“--”,我将得到偏移通知。在我的带有四个变量的示例中,需要三个破折号才能消失。 $junk 包含两个破折号(一个是分隔符)。
I'm not clear why this works, but the notice will go away. First, with this code I get the undefined offset notice:
However, with this code, I don't:
Also note, with '-' or '--' appended to $dateToChange, I will get the offset notice. It takes three dashes for it to go away in my example with four variables. $junk contains the two dashes (one being a separator).