I'am learning PHP with CodeIgniter, and it's a first time I come up to this code:
...
$dashboard = 'admin/dashboard';
$this->user_model->loggedin() == FALSE || redirect($dashboard); // <-- ???
...
What does it mean ? There is a line space after the 2nd line.
Is it something like a ternary operator ?
==== UPDATED DUE TO CLARIFY THE QUESTION ====
For exemple if I want to use IF operator, i would do it either like this:
if( $logged_in === TRUE ){
// do something
}
Or like this without { } braces for IF operator with one line in it:
if( $logged_in === TRUE )
// do something
// this line is not a part of 2 lines above
The question is, WHY there is nothing after that $this->user_model->loggedin() == FALSE || redirect($dashboard);
? Why the next line is empty and what does it really return? How can I check that ?
As you may know, the logical OR operator (||
) will check if either one of the boolean expressions is true.
Supposing we have two functions a
and b
that return boolean values and we want to check if one of them return true:
function a() {
echo "function a was called
";
return true;
}
function b() {
echo "function b was called
";
return false;
}
if (a() || b()) {
echo "hello
";
}
In this case, the output would be:
function a was called
hello
If we are checking if at least one of the expressions is true and the first one evaluates to true
, we do not need to check if the second one is also truthy, right? That's why we didn't have also seen function b was called
in the output of this example.
If we swap the returning values from a
and b
, so a
would return false
and b
would return true
, the two functions would be executed as it would have to check if the second call would return true in the if statement.
There is a similar case in your code. The redirect($dashboard)
will only be executed if the first condition ($this->user_model->loggedin() == FALSE
) is evaluated as false. This code could be written without changing functionality this way:
if (!($this->user_model->loggedin() == FALSE)) {
redirect($dashboard);
}
...or...
if ($this->user_model->loggedin() == TRUE) {
redirect($dashboard);
}
...or just...
if ($this->user_model->loggedin()) {
redirect($dashboard);
}
That's a Logical OR operator.
$a || $b TRUE if either $a or $b is TRUE.
For example
<?php
$a=TRUE; // Set this to FALSE and your function will not be executed.
$b=$a==FALSE || 10;
var_dump($b); // true because 10 || FALSE is true
?>
Note thing to note here is the precedence of operators and cases where the later part will not be executed
<?php
// --------------------
// foo() will never get called as those operators are short-circuit
$a = (false && foo());
$b = (true || foo());
$c = (false and foo());
$d = (true or foo());
?>
There is a line space after the 2nd line.
That's absolutely meaningless, it means nothing. You can pull the code from below and remove that blank line it wont change anything.
Edit
Based on your new explanation in your question as to what you really want to know, lets consider this example. Let's say you want to show a welcome message to someone only if their $login status is true. This is one way you can do so:
<?php
$login=FALSE;
$login==TRUE || myFunction();
function myFunction()
{
echo "Control has reached my function. Welcome You are logged in";
}
?>
Why this specific way of doing so and not a simple if else
? totally up to the programmer which syntax they use and which syntax they find most appropriate for certain code that they write. This can be done in a number of other ways too, not a compulsion to follow this style.
||
is a logical OR operator. If the expression on the left evaluates to true, the expression on the right will then be evaluated. In this case, if it is true that loggedin()
returns FALSE, the expression after the OR ||
will be evaluated: redirect($dashboard)
- otherwise it will not.
This is known as 'short-circuit' evaluation. The second expression is never evaluated if the first one returns false.
It looks like the intention of the code was to redirect the request if the login failed.
Re: Your question--
There is nothing on the following line because there is nothing more to be done. The expressions joined by the OR take care of everything that needs to be done. In your case, kind of a shorthand for
if(loggedin() == FALSE)
redirect ($dashboard);
Make sense?