- Php if this or that then
- I’m a coder. Welcome to my blog. Here are some of the records on my job.
- Home
- Categories
- PHP if this and that or that, how?
- Related Articles
- Javascript this and that with reminders
- Regex: I want this AND that and that & hellip; in any order
- The most effective scheme for this OR, or This And That
- JS, & ldquo; objects & rdquo;, this and that. Coming from python that makes me really confused. (intentional word game)
- Expect this or that, how to include an OU in unit tests
- How to POST data to php from JavaScript, and then return to that same JavaScript function
- Should I use an object for this JS / PHP interaction? Is that correct?
- How to implement a PHP form processing logic that is reusable?
- cascade async.js in node.js: how to use bind and that?
- how to call the php script with ajax that does not need an answer
- How do I automatically alert my php / mysql web application that a PayPal transaction has gone through?
- How to explain to my colleague that Linq to Sql is not identical to Entity Framework and that EF is not & ldquo; Shitty & rdquo;
- How to fix this code that causes & ldquo; calculated value not used & rdquo; Warning?
- How to apply the rewrite rules in php page links so that when converting page links to the rewrite rule
- PHP if. else. elseif Statements
- PHP Conditional Statements
- PHP — The if Statement
- Syntax
- Example
- PHP — The if. else Statement
- Syntax
- Example
- PHP — The if. elseif. else Statement
- Syntax
- Example
- PHP — The switch Statement
Php if this or that then
worth reading for people learning about php and programming: (adding extras to get highlighted code)
?php>
about the following example in this page manual:
Example#1 Logical operators illustrated
.
// «||» has a greater precedence than «or»
$e = false || true ; // $e will be assigned to (false || true) which is true
$f = false or true ; // $f will be assigned to false
var_dump ( $e , $f );
// «&&» has a greater precedence than «and»
$g = true && false ; // $g will be assigned to (true && false) which is false
$h = true and false ; // $h will be assigned to true
var_dump ( $g , $h );
?>
_______________________________________________end of my quote.
If necessary, I wanted to give further explanation on this and say that when we write:
$f = false or true; // $f will be assigned to false
the explanation:
«||» has a greater precedence than «or»
its true. But a more acurate one would be
«||» has greater precedence than «or» and than «=», whereas «or» doesnt have greater precedence than » default»>$f = false or true ;
If you find it hard to remember operators precedence you can always use parenthesys — «(» and «)». And even if you get to learn it remember that being a good programmer is not showing you can do code with fewer words. The point of being a good programmer is writting code that is easy to understand (comment your code when necessary!), easy to maintain and with high efficiency, among other things.
Evaluation of logical expressions is stopped as soon as the result is known.
If you don’t want this, you can replace the and-operator by min() and the or-operator by max().
c ( a ( false ) and b ( true ) ); // Output: Expression false.
c ( min ( a ( false ), b ( true ) ) ); // Output: Expression is false.
c ( a ( true ) or b ( true ) ); // Output: Expression true.
c ( max ( a ( true ), b ( true ) ) ); // Output: Expression is true.
?>
This way, values aren’t automaticaly converted to boolean like it would be done when using and or or. Therefore, if you aren’t sure the values are already boolean, you have to convert them ‘by hand’:
c ( min ( (bool) a ( false ), (bool) b ( true ) ) );
?>
This works similar to javascripts short-curcuit assignments and setting defaults. (e.g. var a = getParm() || ‘a default’;)
( $a = $_GET [ ‘var’ ]) || ( $a = ‘a default’ );
?>
$a gets assigned $_GET[‘var’] if there’s anything in it or it will fallback to ‘a default’
Parentheses are required, otherwise you’ll end up with $a being a boolean.
> > your_function () or return «whatever» ;
> ?>
doesn’t work because return is not an expression, it’s a statement. if return was a function it’d work fine. :/?php
This has been mentioned before, but just in case you missed it:
//If you’re trying to gat ‘Jack’ from:
$jack = false or ‘Jack’ ;
// Try:
$jack = false or $jack = ‘Jack’ ;
//The other option is:
$jack = false ? false : ‘Jack’ ;
?>
$test = true and false; —> $test === true
$test = (true and false); —> $test === false
$test = true && false; —> $test === false
NOTE: this is due to the first line actually being
due to «&&» having a higher precedence than «=» while «and» has a lower one
If you want to use the ‘||’ operator to set a default value, like this:
$a = $fruit || ‘apple’ ; //if $fruit evaluates to FALSE, then $a will be set to TRUE (because (bool)’apple’ == TRUE)
?>
instead, you have to use the ‘?:’ operator:
$a = ( $fruit ? $fruit : ‘apple’ ); //if $fruit evaluates to FALSE, then $a will be set to ‘apple’
?>
But $fruit will be evaluated twice, which is not desirable. For example fruit() will be called twice:
function fruit ( $confirm ) if( $confirm )
return ‘banana’ ;
>
$a = ( fruit ( 1 ) ? fruit ( 1 ) : ‘apple’ ); //fruit() will be called twice!
?>
But since «since PHP 5.3, it is possible to leave out the middle part of the ternary operator» (http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary), now you can code like this:
$a = ( $fruit ? : ‘apple’ ); //this will evaluate $fruit only once, and if it evaluates to FALSE, then $a will be set to ‘apple’
?>
But remember that a non-empty string ‘0’ evaluates to FALSE!
$fruit = ‘1’ ;
$a = ( $fruit ? : ‘apple’ ); //this line will set $a to ‘1’
$fruit = ‘0’ ;
$a = ( $fruit ? : ‘apple’ ); //this line will set $a to ‘apple’, not ‘0’!
?>
To assign default value in variable assignation, the simpliest solution to me is:
$v = my_function () or $v = «default» ;
?>
It works because, first, $v is assigned the return value from my_function(), then this value is evaluated as a part of a logical operation:
* if the left side is false, null, 0, or an empty string, the right side must be evaluated and, again, because ‘or’ has low precedence, $v is assigned the string «default»
* if the left side is none of the previously mentioned values, the logical operation ends and $v keeps the return value from my_function()
This is almost the same as the solution from [phpnet at zc dot webhop dot net], except that his solution (parenthesis and double pipe) doesn’t take advantage of the «or» low precedence.
NOTE: «» (the empty string) is evaluated as a FALSE logical operand, so make sure that the empty string is not an acceptable value from my_function(). If you need to consider the empty string as an acceptable return value, you must go the classical «if» way.
In PHP, the || operator only ever returns a boolean. For a chainable assignment operator, use the ?: «Elvis» operator.
JavaScript:
let a = false;
let b = false;
let c = true;
let d = false;
let e = a || b || c || d;
// e === c
$a = false ;
$b = false ;
$c = true ;
$d = false ;
$e = $a ?: $b ?: $c ?: $d ;
// $e === $c
?>
Credit to @egst and others for the insight. This is merely a rewording for (formerly) lost JavaScript devs like myself.
$res |= true ;
var_dump ( $res );
?>
does not/no longer returns a boolean (php 5.6) instead it returns int 0 or 1?php
I’m a coder. Welcome to my blog. Here are some of the records on my job.
Home
Categories
PHP if this and that or that, how?
I need help figuring out how to make this IF work. It basically checks IF first condition AND either of (second or third condition) are true.
You should have double equal(comparison operator) signs when trying to see IF a,b and c are equal to 1,2 and 3 respectfully.
Also, in order to use proper syntax it would be better to write the IF in lowercase letters and remove the semicolon at the end of the if statement.
Related Articles
Javascript this and that with reminders
When using var that = this for callbacks, why does the callback function have to be function() instead of that.myFn ? For example: var obj = < foo: function(fn) < console.log('--foo--', this); fn(); >, bar: function() < console.log('--b
Regex: I want this AND that and that & hellip; in any order
I’m not even sure if this is possible or not, but here’s what I’d like. String: «NS306 FEBRUARY 20078/9/201013B1-9-1Low31 AUGUST 19870» I have a text box where I type in the search parameters and they are space delimited. Because of this, I want
The most effective scheme for this OR, or This And That
I’m trying to write an XML schema that allows XML to be expressed in the following ways: pets can containt both cat and dog elements:
JS, & ldquo; objects & rdquo;, this and that. Coming from python that makes me really confused. (intentional word game)
Heres a bit of cleaned up code from a picture gallery im writing to learn js. I create the gallery as an object but at some point i lose track of what «this» points to. It doesnt make sense to me what happens at the bottom (look at the comments)
Expect this or that, how to include an OU in unit tests
I’m using buster.js and the «expect» assertions. I want the assertion to succeed in at least one of: expect(req.body.data._id).toBe(‘foo bar baz’); or expect(req.body.data.id).toBe(‘foo bar baz’); How can I do it?Having a look at the relevant do
How to POST data to php from JavaScript, and then return to that same JavaScript function
Should I use an object for this JS / PHP interaction? Is that correct?
Part of a site I am building uses a javascript ajax request to hit a PHP script that retrieves, parses, sorts and paginates and returns results from an XML file. I have done this in a pretty straight forward fashion on the PHP side with variables POS
How to implement a PHP form processing logic that is reusable?
I am new to PHP and I have a lots of forms in PHP. The forms make use of the standard HTML Input fields and need to be validated on the serverside. How can I implement this, so that I do not have to write lots of boilerplate HTML over and over again,
cascade async.js in node.js: how to use bind and that?
I’m learning node.js coming from a PHP background with a limited JavaScript level. I think I got over now the change of mindset implied by the asynchronous approach. And I love it. But, as many others before me, I quickly understood the concrete mean
how to call the php script with ajax that does not need an answer
I’m trying to implement a timer e.g. time spent online. This will be correct to the nearest minute. Every 60 seconds it will add +1 to the minutes column for the user in the db table. var timer = 60000; setInterval(function(),timer); will
How do I automatically alert my php / mysql web application that a PayPal transaction has gone through?
I have a client who will be selling access to an online service on their website. They would like to integrate a PayPal Buy Now button into the site. no problems there. However, they want their customers to have instant access to the online service
How to explain to my colleague that Linq to Sql is not identical to Entity Framework and that EF is not & ldquo; Shitty & rdquo;
This is a bit of a weird question. But I’m tired of having a certain type of discussions with a colleague of mine. Could someone PLEASE (I’m desparate) give me some technical backup on this? I’m trying to ignore his harsh language, but find that very
How to fix this code that causes & ldquo; calculated value not used & rdquo; Warning?
I have an array of doubles and need to do a calculation on that array and then find the min and max value that results from that calculation. Here is basically what I have: double * array; double result; double myMin; double myMax; // Assume array is
How to apply the rewrite rules in php page links so that when converting page links to the rewrite rule
I am using the apche mod_rewrite applying some reules in .htaccess file, Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / RewriteCond %
PHP if. else. elseif Statements
Conditional statements are used to perform different actions based on different conditions.
PHP Conditional Statements
Very often when you write code, you want to perform different actions for different conditions. You can use conditional statements in your code to do this.
In PHP we have the following conditional statements:
- if statement — executes some code if one condition is true
- if. else statement — executes some code if a condition is true and another code if that condition is false
- if. elseif. else statement — executes different codes for more than two conditions
- switch statement — selects one of many blocks of code to be executed
PHP — The if Statement
The if statement executes some code if one condition is true.
Syntax
Example
Output «Have a good day!» if the current time (HOUR) is less than 20:
PHP — The if. else Statement
The if. else statement executes some code if a condition is true and another code if that condition is false.
Syntax
if (condition) code to be executed if condition is true;
> else code to be executed if condition is false;
>
Example
Output «Have a good day!» if the current time is less than 20, and «Have a good night!» otherwise:
if ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The if. elseif. else Statement
The if. elseif. else statement executes different codes for more than two conditions.
Syntax
if (condition) code to be executed if this condition is true;
> elseif (condition) code to be executed if first condition is false and this condition is true;
> else code to be executed if all conditions are false;
>
Example
Output «Have a good morning!» if the current time is less than 10, and «Have a good day!» if the current time is less than 20. Otherwise it will output «Have a good night!»:
if ($t < "10") echo "Have a good morning!";
> elseif ($t < "20") echo "Have a good day!";
> else echo «Have a good night!»;
>
?>
PHP — The switch Statement
The switch statement will be explained in the next chapter.