How to use || in function [duplicate] - php

This question already has answers here:
How do i make shortcuts for IF statements?
(5 answers)
Closed 8 years ago.
I have a function that checks user permissions and I want it to do this:
if(verify("O") || ("M"))
so that it checks if the user has the permission "O" or "M" but it does not work

You're close. This should work:
if(verify("O") || verify("M"))
If your function verify() only accepts one input. You need to call it again with the second input. Now, if either verify("O") or verify("M") returns true, your IF statement will return true.

Related

What does these ... mean in this example? [duplicate]

This question already has answers here:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass

Checking in IF statement [duplicate]

This question already has answers here:
Order in conditional statements [duplicate]
(2 answers)
Closed 5 years ago.
What is the difference between the following two statements?
Suppose we have the following variable.
$someVariable = false;
Statements;
if($someVariable === false)
and
if(false === $someVariable)
There is no difference you can use it any way you want.You are just checking/comparing 2 values whether they are equal or not.
In simple words it's just like Is 1+2=3 or 2+1=3??

AND/OR not working in if/else statement [duplicate]

This question already has answers here:
PHP If statement always firing as true
(2 answers)
Closed 5 years ago.
I'm having issues with AND/OR in an if/else PHP statement. This is one of the codes in particular:
if(is_page_template('page-home.php') || ('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}
If it's like this - with || / OR , it loads every паге template with the first class_trans option and not just the 2 templates specified, and if you put AND instead of OR, then it loads just the first one ('page-home.php') with the first class_trans and doesn't load the second one ('zaadvokati.php') with it. I suppose that, for some reason, it doesn't recognize the second template, but I don't know why.
Can you help?
Thanks!
You need to reiterate the function in the condition. try this:
if(is_page_template('page-home.php') || is_page_template('zaadvokati.php')) {
$class_trans = 'class="trans-color standard-menu"';
}else{
$class_trans = 'class="not-page-home standard-menu"';
}

Performace: multiple conditions in one IF or nested IF? [duplicate]

This question already has answers here:
Does PHP have short-circuit evaluation?
(8 answers)
Closed 6 years ago.
(PHP)
for example, i have functions,which execute heavy queries and return TRUE or FALSE.
is there any performance difference:
if ( cond1() && cond2() && cond3() && cond4())
or
if (cond1()){
if (cond2()){
}
}
because, in the last case, if cond1() is false, then it doesnt execute the other conditions.. is same for the first example?
No this has no performance differences.
In this example when condition 1 fails, it doesn't execute the second condition
if ( cond1() && cond2() && cond3() && cond4())

Check if a string is a part of another string (PHP) [duplicate]

This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'd like to check if a string is equal to another string. By equal, I mean that I want to check if the second string includes the first one in order to make mysql join's in a dynamic way. (I hope I'm clear, sorry for my english...)
I've seen some functions as strcmp() but it only checks if it is purely equal.
It's the same as "$var1 === $var2".
Is there a function which can do that ? Or could you give me some leads to do that ?
if (strpos($a,'are') !== false) {
echo 'true';
}
How do I check if a string contains a specific word in PHP?

Categories