PHP if 变量 = 1 新变量 = 其他内容
我想在 PHP 中进行测试,看看是否 form_id = "1"
然后 form_name = "Test form 1"
然后如果 form_id = "2"
例如,form_name
将为“常规查询”
。
这怎么可能是 PHP 中的一个。
我可以看到如何回显结果但不将其转换为新变量。
谢谢 罗伊
I want to do a test in PHP to see if form_id = "1"
then form_name = "Test form 1"
and then if the form_id = "2"
the form_name
would be "General Enquiries"
for example.
How can this be one in PHP.
I can see how to echo a result but not turn it into a new variable.
Thanks
Roy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个简单的 if 语句就可以做到这一点:
但是当您获得更多可能的表单 id 时,这会导致许多嵌套语句,switch 语句可以解决这个问题
要了解有关 switch 语句的更多信息,请访问 这里
条件值还有一个简短的表示法,将其视为带有简短形式的变量赋值的 if 语句。
在这种情况下,PHP 首先计算问号之前的表达式。如果为 true,则返回问号后面的值,否则返回冒号后面的值。您还可以嵌套这些:
来自稍微年长、更有经验的我的编辑:我通常避免嵌套这些表达式。它通常比仅使用 if 语句或将“内部”三元表达式放入其自己的变量中更难理解。
A simple if statement could do it:
But this makes for many nested statements when you get more possible form id's, a switch statement solves this problem
To learn more about the switch statement, go here
There is also a short notation for conditional values, see it as an if statement with a variable assignment in short form.
In this case, PHP first evaluates the expression before the question mark. If it is true, the value right behind the question mark is returned, otherwise, the value after the colon is returned. You can also nest these:
EDIT from a slightly older, slightly more experienced me: I usually avoid nesting these expressions. It's often harder to understand than just using if-statements, or putting the "inner" ternary expression into its own variable.
你的意思是这样吗?
You mean like this?