Hiya,

Which of these IF CONDITION codes are valid ?
And can you rank the valid ones according to your choice by giving reasons to your choices ?

Thanks!

$conn = mysqli_connect("localhost","root","","buzz");

$sql = "INSERT into users (username,email) VALUES (?,?)";

if($stmt = mysqli_prepare($conn,$sql))
{
    mysqli_stmt_bind_param($stmt,"ss",$_POST[username'],$_POST['email']);

    if(!mysqli_stmt_execute($stmt)) //FIRST CHOICE

        if(mysqli_stmt_execute($stmt)==FALSE) //IS THIS VALID ? RECKON VALID. SECOND CHOICE
    if(mysqli_stmt_execute($stmt)===FALSE) //IS THIS VALID ? RECKON VALID. BUT POINTLESS.

        if(mysqli_stmt_execute($stmt)!=TRUE) //IS THIS VALID ? 5TH CHOICE
        if(!mysqli_stmt_execute($stmt)!==TRUE) //IS THIS VALID ? RECKON VALID. BUT POINTLESS.

        if(mysqli_stmt_execute($stmt)==NULL) //IS THIS VALID ?
    if(!mysqli_stmt_execute($stmt)===NULL) //IS THIS VALID ?

        if(mysqli_stmt_execute($stmt)==0) //IS THIS VALID ? RECKON INVALID.
    if(!mysqli_stmt_execute($stmt)===0) //IS THIS VALID ? RECKON INVALID.

        if(mysqli_stmt_execute($stmt)<1) //IS THIS VALID ?

    if(mysqli_stmt_execute($stmt)!=1) //IS THIS VALID ?
        if(mysqli_stmt_execute($stmt)!==1) //IS THIS VALID ?

        {
        echo 'Something went wrong! Please notify webmaster following error!';
                die('Error 2!');
    }
    else
    {
        die('Thank you for your submission!)';
    }
}
else
{
    echo 'Something went wrong! Please notify webmaster following error!';
        die('Error 1!');
}

Q1: Which of my choices are incorrect and why ?
Q2. Out of the valid IF CONDITIONS, can you rank them according to most valid on top and least valid on bottom ? This should teach me best practice.

@dani

Why my code looks messed-up above ?

Why my code looks messed-up above ?

Sorry about that. I've gone ahead and fixed it. In the future, please add a blank line above and a blank line below the lines with the starting and ending ticks.

if(mysqli_stmt_execute($stmt)===FALSE) is actually more proper than if(mysqli_stmt_execute($stmt)==FALSE). However, in this particular case, because mysqli_stmt_execute() always returns either true or false, and never any other values, technically both will work.

You see, PHP is a loosely typed language, and sometimes functions return 0 or NULL, and sometimes they explicitly return FALSE. Per php.net documentation, mysqli_stmt_execute only returns true on success or false on failure. However, an example of a function where this gets a little dicey is strpos which returns the numeric position of the first occurrence of a substring in a string, or false if not found. That means if the substring is found at the beginning of the string, it will return 0, but if the substring was not found, it will return false. However, in PHP, FALSE == 0. There is even a warning on that page, you can see.

If you want to test for something specifically being false, you can do === false, or specifically not being false, you can do !==. If you just do double equals, ==, then the expression will evaluate to true if function_name() returns 0 or null, for example.

You see, NULL == FALSE and 0 == FALSE, but NULL !== FALSE and 0 !== FALSE.

I hope this made a little bit of sense to you. I know it can be a little confusing.

@dani

Yes, I knew on some functions it means this while at other functions it means that. Hence, I asked you what it particularly means on the mysqli_stmt_execute().
Thanks for brnging up the str_pos() function. Something to keep in mind of what can and can not be used on it to compare.

As mentioned, mysqli_stmt_execute() always returns either true or false, so the most elegant way to test is with === true, === false, !== true, or !== false.

You could alternatively compare to 0, null, etc. with a double equals and it will technically still work, but it would be confusing code.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.