I wrote a simple function in php and passing arguments to uppercase the letters based on passing array index value but I'm getting this error

Warning: stripos() expects at least 2 parameters

what I'm doing wrong can anyone suggest me.

i'm newbie to php just starts learning.

 function doCapital($string, $array)
     $stringArray = explode(",", $string); 

     for( $i=0; $i<count($stringArray); $i++)
             $stringArray[$i] = $stringArray[$i].ucfirst();
             echo $stringArray[$i];

     return implode(" ",$stringArray);

 echo doCapital('abcd', [1,2]);

2 Answers

jameson2012 On Best Solutions

Apologies, on re-reading my last answer I realise that it did seem very unfriendly - I bashed out a quick answer and didn't read it back. What I was trying to say is that with errors like this the quickest solutions to go to the php manual and check the required parameters - which in this case is a needle and a haystack (i.e something to search, and something to search in).

You will likely find the same error here $stringArray[$i] = $stringArray[$i].ucfirst(); as ucfirst requires a string to be passed - here you're using it like jQuery so php thinks you are trying to concatenate a string, it should say ucfirst($stringArray[$i])

you also can't explode with a comma unless your string contains them, so in the example you have you would receive the same string back, I think you mean to use something like str_split

also I would reiterate that I think you need to use in_array for what you're trying to achieve, like this:

function doCapital($string, $array)
    $stringArray = str_split($string); 

    foreach($stringArray as $key => $value)
        //see if the key exists in the array of characters to change the case for 
        //and update if it does
            $stringArray[$key] = ucfirst($value);//thinking about it I might just use strtoupper since there's only one letter anyway - I'm not sure that there's any real performance benefit either way
    return implode("",$stringArray);

 echo doCapital('abcd', [1,2]); //outputs aBCd
Rakesh Kumar Jakhar On

stripos - Find the position of the first occurrence of a case-insensitive substring in a string

You are missing the second parameter, The correct syntax to use the function stripos is

stripos ($haystack ,$needle);


$haystack -> The string in which you are searching

$needle -> The substring

For example :

$findme    = 'x';
$mystring1 = 'xyz';
$pos1 = stripos($mystring1, $findme);
if ($pos1 !== false) {
   echo "We found '$findme' in '$mystring1' at position $pos1";