Inverse conditions - php

I'm trying to rephrase the a conditional to drop a { .. } statement (to make the code less indented).
Currently I have:
while($something){
if((strcasecmp($str1, $str2) === 0)
|| (isset($arr[0]) && strcasecmp($str3, $str4) === 0)){
// a lot of code here...
break;
}
}
With the inversed IF condition it should look like:
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (empty($arr[0]) && strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}
But it doesn't work. My code and the break statement get executed when they shouldn't.
What am I doing wrong here.

Here
(empty($arr[0]) && strcasecmp($str3, $str4) !== 0))
The && must be ||.
I for myself would keep the first variant, because it's slightly more straight-forward.
Update: OK, as I thought about it the break; makes me wonder. You want to get rid of one intendation?
if ($something
&& ((strcasecmp($str1, $str2) === 0) || (isset($arr[0]) && strcasecmp($str3, $str4)) === 0)
){
// a lot of code here...
}
And know some micro-optimization :) (!strcasecmp() means, that they are equal, except maybe the case)
if ($something && (!strcasecmp($str1, $str2) || (isset($arr[0]) && !strcasecmp($str3, $str4))) {
// a lot of code here...
}
I hope the paranthesis matches.

This should work
while($something){
if((strcasecmp($str1, $str2) !== 0)
&& (!isset($arr[0]) || strcasecmp($str3, $str4) !== 0))
continue;
// a lot of code here...
break;
}

Related

PHP if-Statement with multiple conditions

How would the valid syntax be for the following if-statement?
if ($properties->offering_type === 'y' || $properties->offering_type === 'p' && $properties->sold != 'y') {
// echo something
} else {
}
I want to echo something when offering_type is either y or p and sold is not y
&& has higher precedence than ||, so your condition is interpreted as
if ($properties->offering_type === 'y' ||
($properties->offering_type === 'p' && $properties->sold != 'y')) {
You need to add parentheses to group the || together.
if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
&& $properties->sold != 'y') {
<?php
if ( ($properties->offering_type === 'y' || $properties->offering_type === 'p') && ($properties->sold != 'y') ) {
// echo something
}
else {
}
Please note that you are using === which means the type should also be the same. And you are not doing that for the sold property (!=).

php if OR how to?

I am trying to do if statement with OR but it wont work
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com','website2.com') {
die("...");
}else{
<content>
}
The above code says unexpected syntax ','
Another one I tried is
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] !== 'website1.com' OR $refData['host'] !== 'website2.com') {
die("...");
}else{
<content>
}
The above returns ... even though requested from the correct page
Also when there is no OR in the code with one it works fine but not when trying to add the second.
What am I doing wrong?
UPDATE: Couldn't get this working no reason why so just set CURLOPT_REFERER on website2 as website1 and and kept it as
if($refData['host'] !== 'website1') {
thank you everyone
Consider using in_array.
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
$array_data = ["website1.com","website2.com"];
if(in_array($refData, $array_data))
{
echo "Do Something!";
}
else
{
echo "Failed!";
}
Replace !== to != and use && or OR = || depending of your need
$ref = $_SERVER['HTTP_REFERER'];
$refData = parse_url($ref);
if($refData['host'] != 'website1.com' || $refData['host'] != 'website2.com') {
die("...");
}else{
echo 'hi testing';
//run your php code here
}
You problem is that you are checking if both are not identical
`!=` ( Not equal)
`!==` (Not identical)
&& example $x && $y True if both $x and $y are true
|| example $x || $y True if either $x or $y is true
||
Is the OR operator in PHP.
But here, you need an AND operator &&
Like this :
if($refData['host'] !== 'website1.com' && $refData['host'] !== 'website2.com')

undefined index but i can't understand why

Here is my code
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help.
Thanks.
When you do && it will evaluate all conditions until something is false. When you do || it will evaluate all conditions until something is true. Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error'] didn't exist.
You probably want to do this, notice the brackets around your two errors.
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as.
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said:
The && operator will evaluate all conditions until any one of them is false.
The || operator will evaluate all conditions until any one of the is true.
A solution:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || in brackets. because || condition checks up to last piece of code to find out a 'true' value. by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}

My function doesn't work on || tags

I've written a function to make menu roll down if the page is the same as I declare.
The function looks like this
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current === "index?p=config" || "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}
It works perfectly if I only declare 1 page
if ($current === "index?p=config")
but not more. How to solve that solution? And is there a way to declare all websites between || tags in one variable instead of writing them like I did?
Replace this
if ($current === "index?p=config" || "index?p=maintenance")
with
if ($current === "index?p=config" || $current === "index?p=maintenance")
otherwise PHP doesn't know what should be equal to index?p=maintenance
You can use your approach if you set both sides of the equality check every time:
if ($current === "index?p=config" || $current === "index?p=maintenance") { ...
Perhaps a more "readable" solution:
if (in_array($current, array( 'index?p=config', 'index?p=maintenance' )) { ...
Another option would be to use a switch statement with a default.
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
switch($current) {
case "index?p=config":
case "index?p=maintenance":
echo "class=\"nav-top-item suballowed current\" ";
break;
default:
echo "class=\"nav-top-item suballowed\" ";
}
}
Here is the correct code
function menu_current()
{
$current = basename($_SERVER['REQUEST_URI']);
if ($current == "index?p=config" || $current == "index?p=maintenance")
echo "class=\"nav-top-item suballowed current\" ";
else
echo "class=\"nav-top-item suballowed\" ";
}

why strpos doesn't work with ">" and other chracters

I want to check whether special characters such as "<" ">" or the double quote itself is found in a string. But my function always return true.
Thank you
This is the code:
$name = "<h1><dfdafdfds";
function correctName($name){
if (strlen($name) < 5 || (strpos($name, "<")===true) ||
(strpos($name, ">")===true) || (strpos($name, "\"")===true)){
return false;
}else{
return true;
}
}
Strpos either returns false or an integer value such as 5. It does NOT return true.
Therefore (strpos($name, "<")===true always returns false.
your code evaluates as:
if (strlen($name) < 5 || false) ||
(false) || (false)){
return false;
}else{
return true;
}
You need to use this format:
strpos($name, '<') !== false
so your code should look like:
if (strlen($name) < 5 || strpos($name, "<") !== false || strpos($name, ">") !== false || strpos($name, "\"") !== false) {
strpos never returns TRUE. It might return FALSE. Solution: change your comparisons to !== FALSE

Categories