问题描述
我是否需要在PHP中使用大括号if ... else语句?其他结构?
这有关系吗?什么是最佳实践?为什么?
提前感谢...
这似乎没有大括号:
if(ereg(" Win",getenv(" HTTP_USER_AGENT")))
$ visos =" Windows";
elseif((ereg(") ; Mac",getenv(" HTTP_USER_AGENT")))||(ereg(" PPC",
getenv(" HTTP_USER_AGENT"))))
$ visos =" Mac";
elseif(ereg(" Linux",getenv(" HTTP_USER_AGENT")))
$ visos =" Linux";
elseif(ereg(" FreeBSD",getenv(" HTTP_USER_AGENT")))
$ visos =" FreeBSD";
elseif (ereg(" SunOS",getenv(" HTTP_USER_AGENT"))))
$ visos =" SunOS";
elseif(ereg(" IRIX"), getenv(" HTTP_USER_AGENT")))
$ visos =" IRIX";
elseif(ereg(" BeOS",getenv(" HTTP_USER_AGENT"))) )
$ visos =&quo t; BeOS" ;;
elseif(ereg(" OS / 2",getenv(" HTTP_USER_AGENT")))
$ visos =" OS / 2" ;
elseif(ereg(" AIX",getenv(" HTTP_USER_AGENT")))
$ visos =" AIX";
else $ visos =" unknown";
这似乎与大括号一起使用:
if(isset($ _ SERVER) )
{
if(isset($ _ SERVER [" HTTP_X_FORWARDED_FOR"]))
{
$ visip = $ _SERVER [" HTTP_X_FORWARDED_FOR"];
}
elseif(isset($ _ SERVER [" HTTP_CLIENT_IP"]))
{
$ visip = $ _SERVER [" HTTP_CLIENT_IP"];
}
else
{
$ visip = $ _SERVER [" REMOTE_ADDR"];
}
}
其他
{
if(getenv(''HTTP_X_FORWARDED_FOR''))
{
$ visip = getenv(''HTTP_X_FORWARDED_FOR'' );
}
elseif(getenv(''HTTP_CLIENT_IP''))
{
$ visip = getenv(''HTTP_CLIENT_IP'');
}
其他
{
$ visip = getenv(''REMOTE_ADDR'');
}
}
Do I need to use curly brackets in PHP if .. else statements? other constructs?
Does it matter? What are Best Practices? Why?
thanks in advance...
This seems to work WITHOUT curly brackets:
if(ereg("Win", getenv("HTTP_USER_AGENT")))
$visos = "Windows";
elseif((ereg("Mac", getenv("HTTP_USER_AGENT"))) || (ereg("PPC",
getenv("HTTP_USER_AGENT"))))
$visos = "Mac";
elseif(ereg("Linux", getenv("HTTP_USER_AGENT")))
$visos = "Linux";
elseif(ereg("FreeBSD", getenv("HTTP_USER_AGENT")))
$visos = "FreeBSD";
elseif(ereg("SunOS", getenv("HTTP_USER_AGENT")))
$visos = "SunOS";
elseif(ereg("IRIX", getenv("HTTP_USER_AGENT")))
$visos = "IRIX";
elseif(ereg("BeOS", getenv("HTTP_USER_AGENT")))
$visos = "BeOS";
elseif(ereg("OS/2", getenv("HTTP_USER_AGENT")))
$visos = "OS/2";
elseif(ereg("AIX", getenv("HTTP_USER_AGENT")))
$visos = "AIX";
else $visos = "unknown";
And this seems to work WITH curly brackets:
if (isset($_SERVER))
{
if (isset($_SERVER["HTTP_X_FORWARDED_FOR"]))
{
$visip = $_SERVER["HTTP_X_FORWARDED_FOR"];
}
elseif (isset($_SERVER["HTTP_CLIENT_IP"]))
{
$visip = $_SERVER["HTTP_CLIENT_IP"];
}
else
{
$visip = $_SERVER["REMOTE_ADDR"];
}
}
else
{
if (getenv(''HTTP_X_FORWARDED_FOR''))
{
$visip = getenv(''HTTP_X_FORWARDED_FOR'');
}
elseif (getenv(''HTTP_CLIENT_IP''))
{
$visip = getenv(''HTTP_CLIENT_IP'');
}
else
{
$visip = getenv(''REMOTE_ADDR'');
}
}
推荐答案
这篇关于php中必须使用花括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!