如何为IF语句创建快捷方式?

编程入门 行业动态 更新时间:2024-10-11 19:14:47
本文介绍了如何为IF语句创建快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

是否有一个速记版本,检查是否有许多变量具有相同的值?

is there a shorthand version of checking if numerous variables have the same value please ?

而不是: -

if ($a="valid") {do stuff;} if ($b="valid") {do stuff;} if ($c="valid") {do stuff;} if ($d="valid") {do stuff;}

是这样的: -

if ($a or $b or $c or $d = "valid") {do stuff;}

推荐答案

John Conde将数组放在一起然后使用 in_array 的解决方案是一个很好的解决方案。如果你想坚持基本的字符串比较,有点像你的示例代码,那么你可以这样做:

John Conde's solution of putting together an array and then using in_array is a good solution. In case you want to stick with basic string comparisons, somewhat like in your example code, then you could do:

if ($a == 'valid' or $b == 'valid' or $c == 'valid' or $d == 'valid') { // DO STUFF }

基于我从你的例子中注意到的事情,要记住一些事情代码:在PHP中进行相等性比较时,使用 == 而不是 = 。单个 = 用于分配,而双 == 用于比较。

Something to keep in mind, based on what I noticed from your example code: When doing comparisons for equality in PHP, use == and not =. The single = is for assignment, while the double == is for comparison.

更多推荐

如何为IF语句创建快捷方式?

本文发布于:2023-11-07 02:12:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1565277.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:快捷方式   何为   语句

发布评论

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

>www.elefans.com

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