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
Related
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 (!=).
I´m using strpos two timer. In the first if/else it works well but in the second it doesn´t work. This is my code:
if (strpos($word, "mono") == true) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if ($word, "galva") == true) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") == true) {
$coating = "EPOXI 100%";
} elseif ($word, "electro") == true) {
$coating = "Electrozinced";
}
Example:
If the variable word has the value "galva-mono" $type should be "Monobloc" and $coating should be "Galvanized Rod". The problem is that it is assigning well the $type but in the coating it doen´t enter in the if clause.
As stated in the official documentation:
Warning
This function may return Boolean FALSE, but may also return a
non-Boolean value which evaluates to FALSE. Please read the section on
Booleans for more information. Use the === operator for testing the
return value of this function.
You are checking the result with == true instead of !== false.
So, try this code:
if (strpos($word, "mono") !== false) {
$type = "Monobloc";
} else {
$type = "Articulated";
}
if (strpos($word, "galva") !== false) {
$coating = "Galvanized Rod";
} elseif (strpos($word, "epoxi") !== false) {
$coating = "EPOXI 100%";
} elseif (strpos($word, "electro") !== false) {
$coating = "Electrozinced";
}
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;
}
I have this if statment here and its not working
if(
($division->id == 1 || $division->id == 2) &&
in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships) ||
($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships))
{
return FALSE;
} else {
return TRUE;
}
What I am trying to do is say if $division is 1 or 2 and if member1, member2, member3, member4 are in the array $memberships return false, if $division is not 1 or 2 and member5 is in the array return false, everything else return true.
This is not working because member5 is in the array and $division is 1, which should return true, but it returns false.
PS - member1-5 are just names I am using for here as they actually are personal information in my array.
What Am i doing wrong?
I'd do this as a series of different 'if' statements, to avoid you going around the bend trying to work it out.
I've created this piece of code according to your statement...
What I am trying to do is say if $division is 1 or 2 and if member1,
member2, member3, member4 are in the array $memberships return false,
if $division is not 1 or 2 and member5 is in the array return false,
everything else return true.
I think I've got the logic right, I'm about to double check it:
if ($division->id == 1 || $division->id == 2)
{
if (in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_3', $memberships) || in_array('member_4', $memberships))
{
return false;
}
else
{
return true;
}
}
else
{
if (in_array("member_5", $memberships))
{
return false;
}
else
{
return true;
}
}
These are the logical groupings, 1 per line. Use parenthesis to adjust how php evaluates it if this isnt what you want.
($division->id == 1 || $division->id == 2) && in_array('member1', $memberships)
|| in_array('member2', $memberships)
|| in_array('member3', $memberships)
|| in_array('member4', $memberships)
|| ($division->id != 1 || $division->id != 2) && in_array('member5', $memberships))
Try:
if(
(($division->id == 1 || $division->id == 2) &&
(in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships))) ||
(($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships)))
{
return FALSE;
} else {
return TRUE;
}
Less lines of code does not always mean it's better, break it down and make it readable, for example:
$output = true;
if($division->id == 1 || $division->id == 2)
{
if(in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_4', $memberships))
{
$output = false;
}
}
else
{
if (in_array("member_5", $memberships))
{
$output = false;
}
}
return $output;
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;
}