如果变量等于值php [英] If variable equals value php

查看:99
本文介绍了如果变量等于值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表示Bear,2表示Cat,3表示Dog。

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"; 
}

您将值与 == ===

您可以使用 =

使用开关语句,您可以编写更少的代码来实现相同的结果,或者只是一堆如果 s没有 elseif s。

You could write less code to achieve the same result too, with a switch statement, or just a bunch of ifs without the elseifs.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆