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!

Edited by CallMeDiz
Link to comment
Share on other sites

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");

Edited by CallMeDiz
Link to comment
Share on other sites

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.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.