Jump to content

Cant Figure Out A Function That Displays Instances Of Characters.


CallMeDiz

Recommended Posts

Hey guys i'm trying to make a function that returns how many times a certain character is in a certain string with the first parameter being a string and the second being a character.

 

for example if i wanted to see how many of the characters o are in halloween, so I could call it like this.

$numberOfO = numberofchars( "halloweeno", "o"); // should return 2

 

Replys would be much appreciated!

functions confuse the hell out of me

 

EDIT:

 

Figured it out, wow I feel dumb.

 

 

function numberofchars($mystring,$mychar)

{

print substr_count($mystring, $mychar);

}

 

 

$numberOfO =numberofchars( "halloweeno", "o");

functions confuse the hell out of me

 

EDIT:

 

Figured it out, wow I feel dumb.

 

 

function numberofchars($mystring,$mychar)

{

print substr_count($mystring, $mychar);

}

 

 

$numberOfO =numberofchars( "halloweeno", "o");

 

You just created a function that does exactly what an existing function already does. That is pointless. You could just do . . .

print substr_count($mystring, $mychar);

. . . instead of making a function.

The reason to build your own function is to build a repeatable process to do something that doesn't already exist.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.