使用isset()返回多个变量(Returning Multiple Variables with isset())

编程入门 行业动态 更新时间:2024-10-08 00:32:46
使用isset()返回多个变量(Returning Multiple Variables with isset())

我有一些代码返回用户代理,通过一个函数运行它来解析它。 我之前使用的代码只返回解析函数中的一个变量(有三个:'platform''浏览器''版本'):

function my_get_user_agent($value) { $browser = parse_user_agent(); return isset($browser['platform']) ? $browser['platform'] : ''; }

虽然此代码可以返回用户代理的平台,但我需要将其附加到函数中返回所有三个变量。 我将代码的前半部分更改为我认为正确的代码:

return isset($browser['platform'], $browser['browser'], $browser['version'])? $browser['platform'] : '';

但是,我不确定我需要做些什么才能正确返回所有三个值。 建议?

I have a bit of code that returns the user agent, running it through a function to parse it. The code I have previously used returns only one variable from the parsing function (there are three: 'platform' 'browser' 'version'):

function my_get_user_agent($value) { $browser = parse_user_agent(); return isset($browser['platform']) ? $browser['platform'] : ''; }

While this code works to return the platform of the user agent, I need to append it to return all three variables in the function. I changed the first half of the code to what I assume is correct:

return isset($browser['platform'], $browser['browser'], $browser['version'])? $browser['platform'] : '';

I am unsure, however, as to what I need to do to properly return all three values. Suggestions?

最满意答案

你可以只返回整个数组:

return $browser;

然后稍后访问这些值:

$browser['platform']; $browser['browser']; $browser['version'];

再次阅读您的问题,您似乎想确保设置值。 你可以这样做:

foreach($browser as $value) { if(isset($value)) { $data[] = $value; } } return $data;

现在数据将包含平台,浏览器和版本。

You can just return the entire array:

return $browser;

Then access the values later:

$browser['platform']; $browser['browser']; $browser['version'];

Reading your question again, you seem to want to ensure the value are set. You can do this:

foreach($browser as $value) { if(isset($value)) { $data[] = $value; } } return $data;

Now data will contain platform, browser, and version.

更多推荐

本文发布于:2023-07-27 04:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1284909.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   变量   isset   Variables   Multiple

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!