Question:

Is there a function in PHP to compare variables?

by  |  earlier

0 LIKES UnLike

Is there a default function that would allow me to compare two (or more) variables?

For example

$var1 = blah;

$var2 = blaah;

Is there a default function that would 'compare' those and allow me to add a function to highlight the differences?

 Tags:

   Report

3 ANSWERS


  1. Why not make your own


  2. <?php

    if($var1==$var2)

    {

    echo "match"

    }

    else

    {

    echo "no match";

    }

    ?>

    Its as simple as the above to see if two variables match

    OR

    <?php

    $email  = 'name@example.com';

    $domain = strstr($email, '@');

    echo $domain; // prints @example.com

    $user = strstr($email, '@', true); // As of PHP 5.3.0

    echo $user; // prints name

    ?>


  3. There are functions that do some rudimentary comparison of strings.  Those are meant to be strings, right?

    similar_text()

    levenshtein()

    They don't modify the strings in anyway, so if you want to visually represent the differences between two strings, you'd have to implement that yourself.  It would be quite easy to do, though.  Simply compare the strings character by character, and add formatting where the two are not characters are different.

Question Stats

Latest activity: earlier.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.