If variable equals value php(如果变量等于值 php)
问题描述
我正在尝试在数据插入 MySQL 查询之前进行检查.这是代码;
I am trying to do a check before the data inserts into the MySQL query. Here is the code;
$userid = ($vbulletin->userinfo['userid']);
$sql3 = mysql_query("SELECT * FROM table WHERE ID='$_POST[hiddenID]'");
while ($row = mysql_fetch_array($sql3)){
$toon = $row['toonname'];
$laff = $row['tlaff'];
$type = $row['ttype'];
if ($type == 1){
$type == "Bear";
} elseif ($type == 2){
$type == "Cat";
} elseif ($type == 3){
$type == "Dog";
}
}
但是,这不起作用.基本上,每种类型的表"中都有不同的值.1 表示熊,2 表示猫,3 表示狗.
However, this isn't working. Basically, there are different values in the 'table' for each type. 1 means Bear, 2 means Cat, and 3 means Dog.
感谢能帮我发现我脚本中的问题的人!
Thanks to whomever can help see a problem in my script!
推荐答案
你是在比较,而不是在分配:
You are comparing, not assigning:
if ($type == 1){
$type = "Bear";
}
您将值与 ==
或 ===
进行比较.
You compare values with ==
or ===
.
您使用 =
分配值.
您也可以编写更少的代码来获得相同的结果,使用 switch
语句,或者只是一堆没有 elseif
if>s.
You could write less code to achieve the same result too, with a switch
statement, or just a bunch of if
s without the elseif
s.
if ($type == 1) $type = "Bear";
if ($type == 2) $type = "Cat";
if ($type == 3) $type = "Dog";
我会为它创建一个函数,如下所示:
I would make a function for it, like this:
function get_species($type) {
switch ($type):
case 1: return 'Bear';
case 2: return 'Cat';
case 3: return 'Dog';
default: return 'Jeff Atwood';
endswitch;
}
$type = get_species($row['ttype']);
这篇关于如果变量等于值 php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如果变量等于值 php
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01