Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP File Handling with PHP Basic File Handling Writing Files All at Once

Martin Park
Martin Park
12,792 Points

What are the variables $a and $b in the compareStrings function?

This video has totally confused me. Not only will my page not run as intended at the end but for the first time in a good while I have absolutely no idea what is happening here?

In the function compareStrings, 2 variables $a and $b are mentioned but what are these? They have to be passed into the function. Where do they come from? There is no other mention of them?

This video really gave me a sore head. Any help greatly appreciated.

<?php

$states = array_merge(

file('data/html/states.html', FILE_IGNORE_NEW_LINES),
file('data/html/territories.html', FILE_IGNORE_NEW_LINES),
file('data/html/armed_forces.html', FILE_IGNORE_NEW_LINES)

);

function compareStrings($a, $b){
  return strcasecmp(strip_tags($a), strip_tags($b)) 
}

usort($states, 'compareStrings');
file_put_contents('data/html/sorted.html', implode(PHP_EOL, $states));

echo '<select>';

include 'data/html/sorted.html';

echo '</select>';

4 Answers

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

Those two variables are passed from the usort array (the first value) to the usort callback (the second value) automatically for having something to compare in the callback function - as per (php usort in the manual)[http://php.net/manual/en/function.usort.php] (and do other searches on usort for additional explanation)

Try running the following code locally or in something like phpfiddle and see the values:

<?php
function compareStrings($a, $b){
  echo '$a is --> ' . $a . '<br>';
  echo '$b is --> ' . $b . '<br>';
  return strcasecmp(strip_tags($a), strip_tags($b));
}
$states =  array(2, 9, 1, 3, 5); 
usort($states, 'compareStrings');

?>

The output will be:

$a is --> 2
$b is --> 9
$a is --> 9
$b is --> 1
$a is --> 2
$b is --> 1
$a is --> 9
$b is --> 3
$a is --> 2
$b is --> 3
$a is --> 9
$b is --> 5
$a is --> 3
$b is --> 5

Does that help

Martin Park
Martin Park
12,792 Points

Thanks for taking the time to reply. I really appreciate it. I'm trying my best to understand this and I think I'm getting there but your example has brought up a few more questions.

So looking at your example, 2 and 9 are the first values taken from the array.

Looking at strcasecmp it states: Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal. I'm assuming in your example it compares the actual Integer value and not the length of the string? As technically these would be the same string length of 1?

So <0 is returned? Indicating that 2 is less than 9. Then the function continues to run through the array figuring out the correct order.

Going back to the example in the video, it seems strcasecmp sorts the strings in alphabetical order? However from what I've read and mentioned above strcasecmp should compare the length of the string? So this is a little confusing

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

I really didn't pay attention to your usage of strcasecmp and just focused on the $a and $b arguments (of course they can be any valid variable names).

According to the manual

"strcasecmp — Binary safe case-insensitive string comparison" - so not sure where you saw anything about string lengths?

Martin Park
Martin Park
12,792 Points

Example 2 gave me the impression it was string length:

https://www.w3schools.com/php/func_string_strcasecmp.asp

The only example in the manual is when the two strings are the same value so that doesn't really tell you what makes the string greater or lesser. No mention of it being alphabetical. Hence the confusion.

Jeremy Antoine
Jeremy Antoine
15,785 Points

I'n not sure if this will help, but you are missing a semicolon on your strip_tags line. That being said, I agree that this video is a bit harder to follow in that Alena does not overly explain things (Such as the use of the $a adn $b variables, for those of us who want to know the WHY, not just the HOW. I'm thinking that by now, she expects us to know how to look up the information on our own.