3w PHP Your Source for PHP Source Code

11Oct/100

Function to compare 2 string values

This function will compare 2 values,
make sure they are strings,
and make sure they are not empty

here is the code.

<?php
 
function str_compare($str1 , $str2)
{
	// we are converting the values to strings...
	// comment out the following 2 lines 
	// if you wish to check other value types.
	$str1 = (string)$str1;
	$str2 = (string)$str2;
 
	/// we are checking to see that both values are not empty.
	/// if one of the strings is empty, we return false.
	if (empty($str1) || empty($str2))
	{
		return false;
	}
	// the following line is the same like doing
	/*
	if($str1 === $str2)
	{
		$same = true;
	}
	else
	{
		$same = false;
	}
	*/
	$same = (($str1 === $str2)? true : false);
 
	return $same;
}
 
?>

enjoy learning, and please,
feel free to comment, or ask anything you want to know

Comments (0) Trackbacks (0)

No comments yet.


Leave a comment

(required)

No trackbacks yet.