I’m trying to take two strings and print them out in alphabetical order. For example, I am taking their first and last name. If first name <= last name (ex. adam apple), then print out “First name = Adam Last name = Apple” else print out Last name = whatever the last name is, and first name = whatever the first name is. It’s coming from Post data, here is my code.
if (isset($_POST["name"])){
$fullname=$_POST["name"];
if (!empty($fullname)){
$fullnameArray = explode(" ", $fullname);
if ($fullnameArray[0] <= $fullnameArray[1]){
echo "First name: " . $fullnameArray[0];
echo "<br/>";
echo "Last Name: " . $fullnameArray[1];
}else{
echo "Last name: " . $fullnameArray[1];
echo "<br/>";
echo "First Name: " . $fullnameArray[0];
}
echo "<br/>";
}else{
echo "Please enter your first and last name.";
echo "<br/>";
}
}
Thanks.
You want
if (strcmp($fullnameArray[0], $fullnameArray[1]) < 0).See the PHP.net description of strcmp. If you care about mixed case, you may want
strcasecmpinstead.