0

PLS. I am very NEW to PHP so pls. understand. OK so here it goes. . What I want to know first is that "Can a function and an if statement be in one code?" second for the code below, just take a look at where the NOTE is, what should i type in the if statement to make it say " ARE THE SAME" if the values of the parameters $first and $second are the same and say " ARE NOT THE SAME" if not.

    <?php
    function verify($first,$second)
    {
    echo $first . " and " . $second ;
    }
    echo
    verify ("1","2");

    if ($first === $second) /* **NOTE: I know this is wrong cause its not working** */
    echo " ARE THE SAME";
    else
    echo " ARE NOT THE SAME";
    ?>

I think thats about it. Can anyone help?

Jo E.
  • 7,822
  • 14
  • 58
  • 94
  • 1
    It's nice to know that you are interested in PHP :-) However, please *start with a book/tutorial* and follow along with it! –  Jun 01 '12 at 04:01
  • Google is your friend, as is http://www.php.net/manual/en/ – Joshua Jun 01 '12 at 04:02
  • manual != tutorial (although having a *reference* handy is always good) –  Jun 01 '12 at 04:02
  • 1
    any tips for a good reference, tutorial book? I'm actually using "Learning PHP, MySQL, Javascript - Robin Nixon" TNX! ps The PHP MANUAL is a PAIN TO READ SORRY :| – Jo E. Jun 01 '12 at 04:15
  • @user1429811 Learn to love the manual. It is the source of truth – Phil Jun 01 '12 at 04:17
  • wow. your comment actually inspired me to read the manual LOL! ok ill start using the manual from now on.. – Jo E. Jun 01 '12 at 04:24

3 Answers3

2

Your basic problem is one of scope. Outside verify(), $first and $second are not defined. You could simply include the rest of your code inside the function body which would do what you want, eg

function verify($first,$second) {
    echo $first . " and " . $second ;

    if ($first === $second) {
        echo " ARE THE SAME";
    } else {
        echo " ARE NOT THE SAME";
    }
}

verify("1", "2");

... however functions that write to the output buffer are rarely a good idea. Instead, encapsulate the logic inside your function and leave the display to procedural code, eg

function verify($a, $b) {
    return $a === $b;
}

$first = '1';
$second = '2';
echo $first, ' and ', $second;
if (verify($first, $second)) {
    echo ' are the same';
} else {
    echo ' are not the same';
}
Phil
  • 157,677
  • 23
  • 242
  • 245
  • Ok now i get it! tnx for the help. The second sample is more complicated but if its more advisable then ill go with that. THANKS AGAIN, REALLY HELPED! – Jo E. Jun 01 '12 at 04:13
0

If you look at comparison operators you'll see that === will only be true, if both entered vars are of the same type. Usually, if a function returns true you can still use if($returncode == 1) and it would be true, whereas if($returncode === 1) would be false. You'd have to use if($returncode === true to trigger the if statement.

Also, you'll have to put the second if statement into the function, since the variables which you declare with functions are local, meaning it will only be accessible within the function.

Ahatius
  • 4,777
  • 11
  • 49
  • 79
0
<?php
    function verify($first,$second)
    {
    if($first===$second){
     return "Same";
    } else {
     return "Different";
    }
    }
    echo verify ("1","2");
    ?>
Mohit Bumb
  • 2,466
  • 5
  • 33
  • 52