Review
Mick has suggested a solution with fewer lines of code. I would like to address some points about the existing code.
Useless conditional

Inside the else
block 0
is assigned to $wordsArray[$word]
, then it checks to see if $wordsArray[$word]
(which is 0
) is within $wordsArray
- that will always be true.
else{
array_push($array,$word);
$counter =0;
$wordsArray[$word] = $counter;
if(in_array($wordsArray[$word],$wordsArray))
{
$wordsArray[$word] +=1;
}
}
It could simply assign 1 to $wordsArray[$word]
.
Simpler array push

In the aforementioned code block, $word
is pushed into $array
:
array_push($array,$word);
When adding a single element to an array, the same can be achieved by assigning the next available key (which can be omitted):
$array[] = $word;
Post-increment operator
Also in both the if
And else
blocks there are lines to add one to the counts:
$wordsArray[$word] +=1;
Those could be simplified using the post-increment operator ++
$wordsArray[$word]++;
Variable names
Names like $array
aren't very descriptive of what they contain. A better name might be $uniqueWords
.
Function names
Many PHP programmers follow standards outlined in PHP Standards Recommendations like PSR-1 and PSR-12. PSR-1 states:
Method names MUST be declared in camelCase()
.
That should apply to function names as well. So idiomatic PHP code would use a name like countString
instead of CountString
Spacing
PSR-12 also describes spacing in various sections. For example, see PSR-12 section 4.7:
When making a method or function call, there MUST NOT be a space between the method or function name and the
opening parenthesis, there MUST NOT be a space after the opening
parenthesis, and there MUST NOT be a space before the closing
parenthesis. In the argument list, there MUST NOT be a space before
each comma, and there MUST be one space after each comma.
And also PSR-12 section 5:
The general style rules for control structures are as follows:
- There MUST be one space after the control structure keyword
- There MUST NOT be a space after the opening parenthesis
- There MUST NOT be a space before the closing parenthesis
- There MUST be one space between the closing parenthesis and the opening brace
- The structure body MUST be indented once
So there should be a space after a foreach
:
foreach($textsplit as $word){
Should be:
foreach ($textsplit as $word) {
And similarly for if
- like
if(in_array($wordsArray[$word],$wordsArray))
{
Should be:
if (in_array($wordsArray[$word], $wordsArray)) {
Also:
All binary arithmetic, comparison, assignment, bitwise, logical, string, and type operators MUST be preceded and followed by at least one space.
So Instead of this within the else
block:
$counter =0;
Add a space after the =
:
$counter = 0;
And as mentioned earlier that variable is only used once after it is used so it can be eliminated.
Splitting string using regular expression
It is fine to use regular expressions but since it is just splitting on a single space can be done faster using explode()
- e.g.
$textsplit = preg_split("/\s/",$string);
can be simply:
$textsplit = explode(' ', $string);
Exception handling
The code echoes the exception:
catch(Exception $ex){
echo "It has occurred the next error ".$ex;
}
Notice that this is calling the Exception::__toString()
method, which may show more details than desired - e.g. the file, line, stack trace, etc. It may be appropriate to call the getMessage()
method, or else not show any details to the user and instead show a more generic message.