/    Sign up×
Community /Pin to ProfileBookmark

What is this code doing?

I came across some sample PHP code, and can’t figure out the syntax. It’s a function that calculates what percentage of a given string is made up of vowels. Ie, “motion” would return a value of [B]50%[/B] (6 letters, of which 3 are vowels. 50%).

[code=php]
function percentVowels_callback($word) {
$word = strtolower($word);
$chars = count_chars($word);
$numVowels = 0;
foreach (array(“a”, “e”, “i”, “o”, “u”) as $vowel) {
$numVowels += $chars[ord($vowel)];
}
return $numVowels / strlen($word);
}
[/code]

Specifically, what is THIS bit:

[code=php] $numVowels += $chars[ord($vowel)];[/code]

Obviously [B]$numVowels[/B] is an integer, and as the loop is being executed, its value keeps increasing, counting the number of vowels in the string. That part I understand. What on earth is [b]$chars[ord($vowel)][/b]?

Remembering that [b]$chars[/b] is an integer, each execution of the loop would look like this:

[code=php]$numVowels += $chars[ord(‘a’)]
$numVowels += $chars[ord(‘e’)]
$numVowels += $chars[ord(‘i’)]
$numVowels += $chars[ord(‘o’)]
$numVowels += $chars[ord(‘u’)][/code]

Let’s say [B]$chars[/B] has a value of 7. Each iteration of the loop now looks like this:

[code=php]$numVowels += 7[ord(‘a’)]
$numVowels += 7[ord(‘e’)]
$numVowels += 7[ord(‘i’)]
$numVowels += 7[ord(‘o’)]
$numVowels += 7[ord(‘u’)][/code]

What on earth is going on? I have never seen PHP code that looks like that.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@aj_nscMay 17.2009 — Well, you just have to check php.net for two functions:

count_chars - returns an array with the byte-value as key and the frequency of every byte as value

AND

ord - returns the byte value of the given character in the loop (check the table of ASCII characters here)

For the record, I didn't have any knowledge of this stuff prior to the question being posted, I just checked php.net and figured it out....hopefully I got it right.

So basically, the byte value of "e" would be 101, and if you passed the string "what is this code doing to get the vowels?", then the array $chars would have the key "101" being equal to the value "3" - the letter "e" came up three times in the example string. Therefore, every iteration through the loop, it adds the value of the number of times the vowel is present to $numVowels.....hope this helps.
Copy linkTweet thisAlerts:
@callumdauthorMay 17.2009 — Thanks aj, part of the problem was me thinking that [B]count_chars[/B] returned an integer, not an array.

Thanks for the effort you put in on this, I will check out php.net.
Copy linkTweet thisAlerts:
@NogDogMay 17.2009 — Note that php.net has a useful feature in its configuration/processing: you can simply enter a function name as the URL "file name", and it will (99% of the time) take you to the manual page for that function, e.g.: [url=http://php.net/count_chars]http://php.net/count_chars[/url]. ?
×

Success!

Help @callumd spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...