简体   繁体   English

这个特定的StrPos如何在PHP中工作?

[英]How does this particular StrPos work in PHP?

I have the following code. 我有以下代码。 I'm wondering if it's working like I think it is: 我想知道它是否像我认为的那样工作:

//Gift Card Redemption
        if(strpos($_order->getDiscountDescription(), 'Gift Card') !== false){
                $order .= 'RGC1*1*'.$_order->getDiscountDescription().'*****';
                $order .= "\r\n";
        }  

How I think it works: look for 'Gift Card' in $_order->getDiscountDescription() , and if it's not false, do something. 我认为它是如何工作的:在$_order->getDiscountDescription()查找“礼品卡”,如果它不是false,请执行一些操作。 I don't follow what that something is, though. 我不明白那是什么。 Any ideas on that? 有什么想法吗?

This basically checks to see if the string literal ' Gift Card ' is contained in the string returned by $_order->getDiscountDescription() (ie presuming it returns a string...). 这基本上是检查字符串文字' Gift Card '是否包含在$_order->getDiscountDescription()返回的字符串中(即假定它返回了一个字符串...)。 The use of the operator !== and operand false is used because the position might be 0, meaning the start of the string. 使用运算符!==和操作数false的原因是位置可能为0,表示字符串的开头。 Refer to the warning on the documentation for strpos() : 请参考文档中关于strpos()的警告:

Warning This function may return Boolean FALSE , but may also return a non-Boolean value which evaluates to FALSE . 警告此函数可能返回布尔FALSE ,但也可能返回非布尔值,其值为FALSE Please read the section on Booleans for more information. 请阅读布尔值部分以获取更多信息。 Use the === operator for testing the return value of this function. 使用===运算符测试此函数的返回值。

When that condition is true (ie the order description contains 'Gift Card') then the variable $order gets appended with the string literal RGC1*1* followed by the return value from the call to $_order->getDiscountDescription() , 5 asterisk characters, a carriage return character (ie \\r ) and a new line character (ie \\n ). 当该条件为真时(即订单描述中包含“礼品卡”),则变量$order附加字符串文字RGC1*1*后跟调用$_order->getDiscountDescription()的返回值,即5字符,回车符(即\\r )和换行符(即\\n )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM