Shanmugavel S
Shanmugavel S

Reputation: 1

I want to know what's happening in this script?? What is the difference?

In CodeIgniter

 $confirm=$this->video->videoupdate(any values);// Here i am just updating my database
 if($confirm)
 echo "<script>window.location='index';</script>";


 $this->video->videoupdate(any values);// Here i am just updating my database
 echo "<script>window.location='index';</script>";

Can u explain me detail guys...

is it compulsory to check this condition?

Upvotes: 0

Views: 85

Answers (3)

Ross
Ross

Reputation: 17967

// example 1
$confirm=$this->video->videoupdate('any values');
if($confirm)
{
    echo "window.location='index';";
}

// example 2
$this->video->videoupdate('any values');
echo "window.location='index';";

Your videoupdate method will return a value. Generally you return true or false, but can also return data. In example one you are assigning the result of the statement to $confirm.

if $confirm is true then the condition will be executed. Note that unless $confirm is explicitly set to false, any value will evaluate to true, so the condition will always be true.

A better option would be to do:

if($confirm==true) 
{ 
    // redirect
}
else
{
   // something else has happened
} 

This logic can be used to control the flow of an application in the result of an error, for instance, or the failure of a query.

In the second example, the echo statement will occur regardless of outcome, which may be intended, but could result in unexpected behaviour - was the query successful or not at that point in the script.

Upvotes: 0

Shakti Singh
Shakti Singh

Reputation: 86386

In first case script redirect if the record is successfully updated.

in second case does not matter what happen with the record it will always redirect.

Upvotes: 0

Matt Asbury
Matt Asbury

Reputation: 5662

In the first example you are setting a variable $confirm which (I assume) will either be true or false based on whether the update succeeds and then redirecting if it does. In the second example you are redirecting regardless of whether the update succeeds or not.

Upvotes: 6

Related Questions