The usual practice when checking for the return value of functions is to run the function and store the value in a variable, and then test that variable. Here is an example of that process using the strstr() function.
$string = 'abcde';
$var = strstr($string, 'f');
if ( false !== $var ) {
var_dump($var);
} else {
var_dump($var);
}
This code will output "bool(false)" as that was the return value of the strstr() function.
There is another way to write the same code within a single if statement. The following example assigns the variable and checks the return value in a single line of code.
$string = 'abcde';
if ( false !== $var = strstr($string, 'f') ) {
var_dump($var);
} else {
var_dump($var);
}
This code will output "bool(false)" again as this has the same function as the previous example.
There is one problem with assigning variables in this way, and this is that you can only do one. If you try to assign more than one variable at a time you will find that everything but the final variable will turn out to be the outcome of the boolean comparison. Take the following example, which uses the same $string variable as the other examples.
if ( false !== $var1 = strstr($string, 'a')
&& false !== $var2 = strstr($string, 'b')
&& false !== $var3 = strstr($string, 'c') ) {
var_dump($var1, $var2, $var3);
}
The output of this code will be as follows:
bool(true) bool(true) string(3) "cde"
The first two comparisons come out as true and so $var1 and $var2 are assigned boolean values of true. The final comparison is the only one that is assigned the expected value. This might be a bug in PHP, but as this is a non-standard way of assigning variables I don't think many people have come across it.
Comments
... $var1 = (strstr($string, 'a') && false !== $var2 = strstr($string, 'b') && false !== $var3 = strstr($string, 'c'))
... $var2 = (strstr($string, 'b') && false !== $var3 = strstr($string, 'c'))
to get expected behaviour all one needs is enclosing assignments within ().if ( false !== ($var1 = strstr($string, 'a')) && false !== ($var2 = strstr($string, 'b')) && false !== ($var3 = strstr($string, 'c'))) { var_dump($var1, $var2, $var3);
}
also by doing that - in theory - you're helping compiler (for tiny speedup) so it doesn't need to find out operator precedence, just eval from insidemost statement(s).This works too:if( !($val = $arg) )
{ $val = 'default';
}
A gotcha worth highlighting, assigning a 0 is of course a falsy, so you can work around it as follows where NULL is returned when we really want to exit, and not on 0.
while ( NULL !== ( $n = $this->getSomeIntIndex( )) )
{
// do something with $n
}
Hi, I need help with below code
How to create variable with if multiple statement in php and print
$selected_payment_option = 'if($payment_mode == "cod"){ echo "Cash on Delivery ";}
if($payment_mode == "stripe"){ echo "Stripe";}
if($payment_mode == "bank"){ echo "Bank";}';
and want to print as
$selected_payment_option
Sam,
For fixed payment types, you may be better off to assign an array with the options, with the code as key, and display name as value. That array could be filled from a database query on payment types allowed, which would then pick up when you add a new option, you don't then have to re-code so much.
$userPaymentSel = $_POST['user_payment_selection']; // Assuming user selected payment type is received here
$paymentTypes = array(
'cod'=>'Cash on Delivery',
'stripe'=>'Stripe',
'bank'=>'Bank'
);
$selected_payment_option = $paymentTypes[$userPaymentSel];
That has no input validation etc, but might be a start.