2
function StringCount($searchstring, $findstring)
        {
            return (strpos($searchstring, $findstring) === false ? 0 :  count(split($findstring, $searchstring)) - 1);
        }

it returns number of ocourances of substring in string, but why not just use count?

What means === false ? 0 : i mean how this called its not if or case is there way to call this type of writing?

4 Answers 4

1

http://www.php.net/manual/en/language.operators.comparison.php - about === http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary - about (expr1) ? (expr2) : (expr3)

But, i think it is better to use substr_count() ( http://www.php.net/manual/en/function.substr-count.php ) in this function

Sign up to request clarification or add additional context in comments.

Comments

1

This is a type of ternary operator (meaning it takes 3 operands), and is a short form of the if then else clause.

a ? b : c can be expanded as:

if(a)
{
   b
}
else
{
   c
}

So in essence it is something like this:

$strPos;
if (($searchstring, $findstring) === false) 
{
   $strPos=0 
}
else
{
   $strPos=count(split($findstring, $searchstring))
}

return strpos($strPos-1);

5 Comments

this explaination is more complete if wrapped in a function, since the whole thing either returns b or c also called a trinary hook.
1. "Ternary". In fact, you'll very often hear this called the ternary operator, as there are few/no other common ones in C'ish languages.
x ? y:zand if(x) { y } else { y } are not exactly the same thing. Yes, it's easier to grasp for beginners when explained this way but no, it's not the same ;-)
2. b and c are expressions, not statements, and the whole result is almost always assigned to something. (If it's not assigned, many would argue the operator is being abused and should be rewritten as an if/else statement.) More correct would be to say that x = (a ? b : c) is equivalent to if (a) x = b; else x = c;.
$previous_comment =~ s:assigned:assigned/returned/passed:g;
0

Because strpos returns Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "".

A good example is that StringCount("stackoverflow","stack") will return 0 if

function StringCount($searchstring, $findstring)
{
  return (strpos($searchstring, $findstring) == false ? 0 : count(split($findstring, $searchstring)) - 1);
}

Comments

0

It's a ternary condition

If strpos($searchstring, $findstring) is false, then 0, else count(split($findstring, $searchstring)) - 1

So if $findstring is NOT found in $searchstring, return 0

The reason you need 3 = for that false statement is strpos returns an integer of where the needle was found in the haystack. Buy using === you get the boolean.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.