A PHP Error was encountered

Severity: 8192

Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated

Filename: libraries/Filtered_db.php

Line Number: 23

Write a php program to compare between things that are not integers
Q:

Write a php program to compare between things that are not integers

-2

Different datatype comparison in PHP

Write a php program to compare between things that are not integers.

Suppose the strings are -

$str1 = "00004";
$str2 = "008";
$str3 = "00007-STR";

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

The comparison operators work for strings as well as numbers. PHP has automatic type conversions that can lead to counter intuitive results when the strings are interpretable as numbers. If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.

In the given example, we have taken three stings and perform comparison between them using comparison operator -

<?php 
	$str1 = "00004";
	$str2 = "008";
	$str3 = "00007-STR";
	
	if($str2 < $str1) {
		echo "$str1 is less than $str2<br/>";
	}
	if($str3 < $str2) {
		echo "$str3 is less than $str2<br/>";
	}
	if($str1 < $str3) {
		echo "$str1 is less than $str3<br/>";
	}
?>

Output of the above code

00007-STR is less than 008
00004 is less than 00007-STR

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now