php - 如何使我的 if 条件更简单?
问题描述
我试图找到一种更短的方法来编写这个 if 条件:
if($template == "documents"){
$slug = $template;
$parent = "products";
} else {
$slug = $slug;
$parent = $parent;
}
这是我的方法:
$slug = ($template == "documents") ? $template : $slug;
$parent = ($template == "documents") ? "products" : $parent;
我觉得这段代码可以减少更多。但是我不知道怎么做。
解决方案
else 条件的代码似乎没用,因为您再次使用相同的值设置相同的变量。您只能使用 if 条件:
if ($template == "documents") {
$slug = $template;
$parent = "products";
}
希望它可以帮助你。
推荐阅读
- git - 我应该如何阅读 git fetch 返回的消息?
- javascript - 无操作的 HTML 表单(+ CSP 规则无内联 javascript)
- c# - CS0103 当前上下文中不存在名称“请求”
- r - 如何统一ggplot中的小数位数?
- python - 将一组加权和转换为 python 的优雅方式
- android - 权限默认对话框未显示
- javascript - 在滚动上转换 translate3d
- makefile - make/Makefile:用于“禁用”来自目标的依赖项的 CLI 选项
- excel - 将 XML 数据批量导出到 Excel 电子表格
- apache-spark - Spark如何按顺序加载有序parquet分区文件?