If either of two cookies exist do something, in PHP - php

I know that || or && need to be used but I can't work out the correct or best way to format this.
My code for one cookie:
if(isset($_COOKIE['mycookie'])) {
if($_COOKIE['mycookie']=="value1") {
// do some stuff
}
}
But I'd like to include another cookie in this routine where either one can be true for the "stuff" to work.
I'm just not sure how to format this. Is it something like this?
if(isset($_COOKIE['mycookie'] || ['mycookie2')) {
if($_COOKIE['mycookie']=="value1" || $COOKIE['mycookie2']=="value2") {
// do some stuff
}
}

You can write all in one if statement if you want like this:
(The OR statement in the isset() function is not going to work)
if ( (isset($_COOKIE['mycookie']) && $_COOKIE['mycookie'] == "value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") )

You need to do the || outside the function, to combine the results of all the calls.
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
// do some stuff
}

It will be:
if (isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2'])) {
if ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2") {
// do some stuff
}
}
Or even:
if ((isset($_COOKIE['mycookie']) || isset($_COOKIE['mycookie2') && ($_COOKIE['mycookie'] == "value1" || $_COOKIE['mycookie2'] == "value2")) {
// do some stuff
}
to avoid nested if.

Try this
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1")
|| 9isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] =="value2" )) {
// do some stuff
}

Try this. It puts all requirements in one if statement:
if( (isset($_COOKIE['mycookie'] && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2']=="value2") ) {
// do some stuff
}

You can use one if condition instead of nested if. If you required to validate both then
if(isset($_COOKIE['mycookie'], $_COOKIE['mycookie2']) && ($_COOKIE['mycookie'] == "value1" && $_COOKIE['mycookie2']=="value2")) {
// do some stuff
}
Or if you have to validate one of them then
if((isset($_COOKIE['mycookie']) && $_COOKIE['mycookie']=="value1") || (isset($_COOKIE['mycookie2']) && $_COOKIE['mycookie2'] == "value2") ) {
// do some stuff
}

Related

Laravel: Proper use of isset in If Statement

I have a conditional: if (isset($response['ad1'], $response['screen'])) { ... }
Now I want to change the conditional to be if either $response['ad1'] or $response['ad2'] is set and $response['screen'] is set.
Would that just be: if (isset(($response['ad1'] || $response['ad2']), $response['screen'])) { ... } ?
if ((isset($response['ad1']) || isset($response['ad2'])) && isset($response['screen']))
or
if (isset($response['screen']) && (isset($response['ad1']) || isset($response['ad2'])))
if ((isset($response['ad1']) || isset($response['ad2'])) && isset($response['screen']))
You can use this way
Try
if((isset($response['ad1']) || isset($response['ad2'])) && isset($response['screen']))
{
}

How do I check for 2 keywords in PHP

I want to check for my session. The code below works:
if ($_SESSION["rol"] != 'trainer') {
}
But this code doesnt work:
if ($_SESSION["rol"] != 'trainer' || 'commandant') {
}
It should check for both, because both have permission. What am I doing wrong?
Use this
if ($_SESSION["rol"] != 'trainer' || $_SESSION["rol"] != 'commandant') {
}
$role = ['trainer','commandant'];
if(!in_array($_SESSION['rol'],$role))
{
//do some stuff
}
I will probably do the following, where the isset ensures the key exists (and helps reduce warnings when the key is not available):
if (isset($_SESSION["rol"]) && ($_SESSION["rol"] != 'trainer' || $_SESSION["rol"] == 'commandant')) {
echo 'do some...';
}
array_key_exists is a nice alternative to using isset to check for keys:
if (array_key_exists('rol', $_SESSION) && ($_SESSION["rol"] != 'trainer' || $_SESSION["rol"] ==
'commandant')) {
echo 'do more...';
}
Hope that helps.
PS: #dexter solution with a in_array() will be better over time and easier to maintain.

If and elseif's not working

Sorry for the messy code but basically this code will only ever make $booktype = "ICT" and if I remove the "else" from elseif, it makes it equal ENGLISH.
Whats going wrong?
I've echoed POST_ Book room and that comes out fine.
if ($_POST['bookroom'] == "142" || "040" || "139"|| "104") {
$booktype = "ICT";
} elseif ($_POST['bookroom'] == "015" || "016" || "017" || "018" || "027" || "028") {
$booktype = "MATHS";
} elseif ($_POST['bookroom'] == "E03") {
$booktype = "MUSIC";
} elseif ($_POST['bookroom'] == "202" || "204" || "205" || "206" || "207") {
$booktype = "ENGLISH";
}
At the end of the day, you're not using 'or' correctly. To shorten your code, you can do your if-statement like this:
if(in_array($_POST['bookroom'], array("142","040","139","104"))) {
To properly use an "or" or "and" clause, you'd need to make the statement self-contained. That is,
if(this option is true || this option is true || this option is true)
if($a == 4 || $a == 5 || $a == 6)
You can't chain conditions like this. Change it to
$_POST['bookroom'] == "142" || $_POST['bookroom'] == "040" || [...]
Also, using a switch case with fall-throughs would be more readable here.
You can also change it to use in_array function:
From:
if ($_POST['bookroom'] == "142" || "040" || "139"|| "104"){
to
if (in_array($_POST['bookroom'], array("142","040","139","104"))){
You can use switch !
switch($_POST['bookroom']){
case "142":
...
break;
case "040":
...
break;
}

Using if (!empty) on multiple variables but displaying content if one isn't empty

I'm using the following
if (!empty($data['var_1'])
&& !empty($data['var_2'])
&& !empty($data['var_3'])
&& !empty($data['var_4'])
&& !empty($data['var_5'])
&& !empty($data['var_6'])
&& !empty($data['var_7'])
&& !empty($data['var_8'])
&& !empty($data['var_9'])) {
//BLOCK HERE
}
Basically, what I'm trying to achieve is if all of the variables are empty, hide the block. If 8 or less are empty, display the block.
Where am I going wrong?
You want || not &&. This will display the block only if they are all not empty. I think there is probably a nicer way to do this, though, like array_filter.
Well, you could just use a loop and an $isok variable:
$isok = false;
for($i=1; $i<10; $i++) {
if( !empty($data['var_'.$i])) {
$isok = true;
break; // no need to continue looping
}
}
if( $isok) {
// BLOCK HERE
}
This is easier to edit too, in case you change the var_ part or want a different range of numbers.
You can also try
$data = array_filter($data); // remove all empty value form array
if (count($data)) {
// do your thing
}
The code you wrote will display the block if ALL of the variables aren't empty. If you want it to be displayed when ANY of the variable isn't empty, use OR instead of AND by replacing the && by ||.
<?php
if (!empty($data['var_1']) || !empty($data['var_2']) || !empty($data['var_3']) || !empty($data['var_4']) || !empty($data['var_5']) || !empty($data['var_6']) || !empty($data['var_7']) || !empty($data['var_8']) || !empty($data['var_9'])) {
//BLOCK HERE
}
You can use array_values() for that:
if ( count(array_values($data)) ) {
//BLOCK HERE
}
Replace && (AND) with || (OR)
if (!empty($data['var_1'])
|| !empty($data['var_2'])
|| !empty($data['var_3'])
|| !empty($data['var_4'])
|| !empty($data['var_5'])
|| !empty($data['var_6'])
|| !empty($data['var_7'])
|| !empty($data['var_8'])
|| !empty($data['var_9'])) {
//BLOCK HERE
}
if (empty(array_values($data))) { /* will return you true if all variables are empty*/}

Shortest way to code this php multiple if condition

I am trying to do a query like:
If $_GET['page'] == 'items' AND $_GET['action'] == 'new' OR 'edit'
Here's what I have:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && $_GET['action'] == 'new' || isset($_GET['action']) && $_GET['action'] == 'edit') {
// This is what Im looking for
}
}
Is this correct, and is this the easiest way to make this query?
You could have done it like this as well:
if (isset($_GET['page']) && $_GET['page'] == 'items') {
if (isset($_GET['action']) && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
}
}
Your way is perfectly fine, although I would almost be tempted to do it the following way. The only reason I suggest this is that your code requires that both action and page are set. If action is not set then there isn't much point checking if the page is == 'items'.
if(isset($_GET['page']) && isset($_GET['action'])) {
if($_GET['page'] == 'items' && ($_GET['action'] == 'new' || $_GET['action'] == 'edit')) {
//do code here
}
}
You may also try in_array like:
if (isset($_GET['page']) && $_GET['page'] == 'items')
{
if ( !empty( $_GET['action'] ) && in_array( $_GET['action'], array( 'new', 'edit' ) )
{
// This is what Im looking for
}
}
That is one of possible solutions
if ( #$_GET['page'] == 'items' && in_array(#$_GET['action'], array('new','edit')))
Everything is ok, but also you can use function:
function paramIs($param, $values) {
$result = false;
foreach ((array)$values as $value) {
$result = $result || isset($_GET[$param]) && $_GET[$param] == $value;
}
return $result;
}
Usage:
if (paramIs('page', 'items') && paramIs('action', array('new', 'edit')))
{
// your code here
}
It will reduce the number of repetitions in your code and encapsulate logic in one place

Categories