这个php字符串加入让我抓狂!

chc*_*ist 0 php codeigniter

我想在$ _POST前加一个"0"

$currency = $_POST['Currency']; // lets say 900
$currency = "0".$currency;
echo $currency;
Run Code Online (Sandbox Code Playgroud)

它应该已经返回0900但它返回900.

有任何想法吗?

编辑

这是完整的功能

function validate(){

        $ref = $this->input->post('Ref');
        $shop = $this->input->post('Shop');
        $amount = $this->input->post('Amount')*1000;
        //$currency = $this->input->post('Currency');
            //$currency = $_POST['Currency']; // lets say 900
            //$currency = "0".$currency;
        $currency = str_pad($_POST['Currency'],4,'0',STR_PAD_LEFT);

        $query = $this->db->query("SELECT * FROM shop_validation WHERE merchant_ref = '$ref' ");
        if($query->num_rows() > 0) {

            $row = $query->row_array();

            $posts = "";

            foreach ($_POST as $name => $value) {
                $posts .= $name." / ".$value;
            }

            $this->db->query("INSERT INTO transactions (shop,amount,currency,posts) VALUES ('$shop','$amount','$currency','$posts')");


            if($row['merchant_ref'] != $ref)
            {
                echo "[NOTOK]";
                return;
            }

            if($row['merchant_id'] != $shop)
            {
                echo "[NOTOK]";
                return;
            }

            if(trim($row['amount']) != $amount)
            {
                echo "[NOTOK]";
                return;
            }

            if($row['currency_code'] != $currency)
            {
                echo "[NOTOK]";
                return;
            }

            echo "[OK]";

        }


    }
Run Code Online (Sandbox Code Playgroud)

编辑

此脚本在Codeigniter框架上运行

Jua*_*tés 6

如果您想要的是确保输入具有一定数量的数字,并且前导零,我在前一段时间写了一个提示,它正是这样做的:

<?php
$variable =  sprintf("%04d",$_POST['Currency']);
?>
Run Code Online (Sandbox Code Playgroud)

这将回显前导零,直到$variable长度为4个字符.这里有些例子:

如果$_POST['Currency']值为'3',它将回显'0003'

如果$_POST['Currency']值为'103',它将回显'0103'

如果$_POST['Currency']值为'3103'则会回显'3103'

即使字符数超过4(在您的情况下)也是好的,因为它只是忽略该函数而不在其前面添加任何内容.希望它有帮助:)