开发者

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>";开发者_StackOverflow

Can u explain me detail guys...

is it compulsory to check this condition?


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.


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.


// 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜