I'm trying to build up a PHP if statement with or ("||") operators, but it doesn't seem to work.
$country_code = "example_country_code";
if ($country_code != 'example_country_code' || !clientIscrawler()) {
echo 'the script can be executed';
}
else {
echo 'skipping';
}
With the given example, it should be echoed skipping, but it doesn't happen like that. What am I doing wrong?
Perhaps the double negatives are giving you problems. Let's rewrite it to:
!($country_code == 'example_country_code') || !clientIscrawler()
This can be turned into an equivalent condition with &&:
!($country_code == 'example_country_code' && clientIscrawler())
By reversing the if you would get this:
if ($country_code == 'example_country_code' && clientIscrawler()) {
echo 'skipping';
} else {
echo 'the script can be executed';
}
Therefore, in your code, it will only print skipping if clientIscrawler() is truthy.
If you have multiple conditions with the OR operator, in which case you don't want the if statement to evaluate as true, the syntax is:
if(!($something == "something" || $something == 'somethingelse')){
do stuff...
}
Here is an example:
$apples = array (
1 => "Pink Lady",
2 => "Granny Smith",
3 => "Macintosh",
4 => "Breaburn"
);
foreach($apples as $apple){
// You don't want to echo out if the apple name is "Pink Lady" or "Macintosh"
if(!($apple == "Pink Lady" || $apple == "Macintosh")){
echo $apple."<br />";
}
}
// Output is:
Granny Smith
Breaburn
In your given code, it all depends on your function call
!clientIscrawler()
You will be getting the script can be executed output only when your function call returns FALSE. I think it is returning TRUE right now, which is why you are not getting the desired output.
Maybe this can help you:
if ( ($country_code != 'example_country_code') || clientIscrawler() == false) {
Try this way:
if ( ($country_code != 'example_country_code') || !clientIscrawler()) { ...
Related
I am using the following code to pass a variable. if variable = a, do nothing.
I then want to check if variable = a, do nothing, if b, do nothing, else do something
<?
if($_GET['pageid'] == 'a'){
} else {
include('header_image.php');
}
?>
Above is the code I have working correctly for one vartiable.
How do I add an if / else?
if($_GET['pageid'] != 'a' && $_GET['pageid'] != 'b'){
//do smth
}
This is a comment - i want the formatting...
To do what you want:
if ($_GET['pageid'] == 'a') {
// do nothing for now
}
elseif ($_GET['pageid'] == 'b') {
// do some more nothing...
}
else { // we do something...
include('header_image.php');
}
You could combine the 'do nothing' tests as:
if ( $_GET['pageid'] == 'a'
|| $_GET['pageid'] == 'b') {
// do nothing for now
}
else { // we do something...
include('header_image.php');
}
I agree it reads better than the 'not equal and' tests. However, that is what 'programmers' use so it is worthwhile getting used to it.
I want to check the GET variables are not empty, I tried ways but they didn't work.
So I had the code like this:
$u = isset($_GET["u"]);
$p = isset($_GET["p"]);
if ($u !== "" && $p !== "") {
//something
} else {
//do something
}
The I checked the code by sending create.php?u=&p=, but the code didn't work. It kept running the //do something part. The I tried:
echo $u;
echo $p;
It returned 1 and 1. Then I changed it to:
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "") {
//something
} else {
//do something
}
But it continued to run //do something.
Please help.
You can just use empty which is a PHP function. It will automatically check if it exists and whether there is any data in it:
if(empty($var))
{
// This variable is either not set or has nothing in it.
}
In your case, as you want to check AGAINST it being empty you can use:
if (!empty($u) && !empty($p))
{
// You can continue...
}
Edit: Additionally the comparison !== will check for not equal to AND of the same type. While in this case GET/POST data are strings, so the use is correct (comparing to an empty string), be careful when using this. The normal PHP comparison for not equal to is !=.
Additional Edit: Actually, (amusingly) it is. Had you used a != to do the comparison, it would have worked. As the == and != operators perform a loose comparison, false == "" returns true - hence your if statement code of ($u != "" && $p != "") would have worked the way you expected.
<?php
$var1=false;
$var2="";
$var3=0;
echo ($var1!=$var2)? "Not Equal" : "Equal";
echo ($var1!==$var2)? "Not Equal" : "Equal";
echo ($var1!=$var3)? "Not Equal" : "Equal";
echo ($var1!==$var3)? "Not Equal" : "Equal";
print_r($var1);
print_r($var2);
?>
// Output: Equal
// Output: Not Equal
// Output: Equal
// Output: Not Equal
Final edit: Change your condition in your if statement to:
if ($u != "" && $p != "")
It will work as you expected, it won't be the best way of doing it (nor the shortest) but it will work the way you intended.
Really the Final Edit:
Consider the following:
$u = isset($_GET["u"]); // Assuming GET is set, $u == TRUE
$p = isset($_GET["p"]); // Assuming GET is not set, $p == FALSE
Strict Comparisons:
if ($u !== "")
// (TRUE !== "" - is not met. Strict Comparison used - As expected)
if ($p !== "")
// (FALSE !== "" - is not met. Strict Comparison used - Not as expected)
While the Loose Comparisons:
if ($u != "")
// (TRUE != "" - is not met. Loose Comparison used - As expected)
if ($p != "")
// (FALSE != "" - is met. Loose Comparison used)
You need !empty()
if (!empty($_GET["p"]) && !empty($_GET["u"])) {
//something
} else {
//do something
}
Helpful Link
if ($u !== 1 && $p !== 1 && $u !== "" && $p !== "")
why are you using "!==" and not "!=".
to always simplify your problem solve the logic on paper once using the runtime $u and $p value.
To check if $_GET value is blank or not you can use 2 methods.
since $_GET is an array you can use if(count($_GET)) if you have only u and p to check or check all incoming $_GET parameters.
empty function #Fluffeh referred to.
if($_GET['u']!=""&&$_GET['p']!="")
Hope it helps thx
In you code you should correctly check the variable existence like
if ($u != NULL && $p != NULL && $u != 0 && $p != 0) {
//something
} else {
//do something
}
Wow! I was so dumb... isset returns a boolean. I fixed my problem now. Thank you for answering anyway :)
This fixes:
$u = $_GET["u"];
$p = $_GET["p"];
I have if statement with multiple condition,
what is the differece between this two conditon:
1.
if($province=="AB" || "NT" || "NU" || "YT")
{
$GST=5;
}
else if($province=="BC" || "MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || "NF" || "ON")
{
$HST=13;
}
and second is:
2.
if($province=="AB" || $province=="NT" || $province=="NU" || $province=="YT")
{
$GST=5;
}
else if($province=="BC" || $province=="MB")
{
$GST=5;
$PST=7;
}
else if($province=="NB" || $province=="NF" || $province=="ON")
{
$HST=13;
}
The difference between the two is that the first one won't work as expected, and the second is technically correct.
The code:
if($province=="AB" || "NT" || "NU" || "YT")
will always evaluate to true and execute the code in that conditional block.
The reason is because you are only checking if $province == "AB" and then you are checking if "NT" == true which will evaluate to true.
To check province against all of those values (AB, NT, NU, YT) you need to explicitly check $province against each value, not just the first, which is what you are correctly doing in the second example.
I add to drew010 answer that you can do this too (wich is simpler) :
if(in_array($province,array("AB","NT","NU","YT"))
{
$GST=5;
}
else if(in_array($province,array("BC","MB"))
{
$GST=5;
$PST=7;
}
else if(in_array($province,array("NB","NF","ON"))
{
$HST=13;
}
The first one will always evaluate to true. In the second, third, and forth OR clauses in the first example you are basically asking PHP to convert the strings to Boolean values, and non-empty strings will always evaluate as true.
Just for fun, my favorite way to handle conditionals like this is with a switch statement. It's a lot easier for me to read, but it's really a personal preference thing.
switch ( $province ) {
case 'AB' :
case 'NT' :
case 'NU' :
case 'YT' :
$GST = 5;
break;
case 'BC' :
case 'MB' :
$GST = 5;
$PST = 7;
break;
case 'NB' :
case 'NF' :
case 'ON' :
$HST = 13;
break;
}
I am running into a funny problem with a mischievous "if" condition :
$condition1="53==56";
$condition2="53==57";
$condition3="53==58";
$condition=$condition1."||".$condition2."||".$condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Why does the if condition pass?
Why does php echo "blah"? What do I do to make php evaluate the "if" statement and print "foo"?
The problem here is that you're putting your expressions in strings!
Your $condition1, $condition2, and $condition3 variables contain strings, and not the result of an expression, and the same goes for your $condition variable which will be a string that looks like 53==56||53==57||53==58. When PHP evaluates a string it considers it true if it is not empty and not equal to 0, so your script will output blah.
To fix this you just need to take your expressions out of the strings. It should look like this:
$condition1 = 53 == 56; // false
$condition2 = 53 == 57; // false
$condition3 = 53 == 58; // false
$condition = $condition1 || $condition2 || $condition3; // false || false || false = false
if ($condition) {
echo 'blah';
} else {
echo 'foo'; // This will be output
}
You're evaluating strings as booleans; they'll aways be true (except the strings "" and "0". Get rid of almost all of the quotes in your program.
Those aren't conditions, they're strings.
$condition1=53==56;
$condition2=53==57;
$condition3=53==58;
$condition=$condition1 || $condition2 || $condition3;
if($condition)
{
echo "blah";
}
else
{
echo "foo";
}
Because you're not checking those variables, it's saying if (String) will always return true. (unless "")
You should be doing:
if(53==56 || 53==57 || 53==58)
{
echo "blah";
}
else
{
echo "foo";
}
All $condition* variables will evaluate to true. This is how PHP sees it:
if("53==56" || "53==57" || "53==58")
What you want is this:
$condition1 = 53==56;
$condition2 = 53==57;
$condition3 = 53==58;
It's because you're evaluating a string, and strings other than empty strings evaluate to true.
You are concatting a string together, a non-empty string equals TRUE in php.
Because when the if passes, $condition is a string (a concatenation of) containing the text of your conditions. Try using if(eval($condition)).
String always evaluate to true if its not empty
And btw php make implicit conversion to boolean
I am new to php and this little bugger has been eating up my day, perhaps it's due to some property of php I am unaware of?
As part of some code for getting some data out of an xml file (using the event based Expat parser), I have the following code
$xmlFields;
$fieldName = "";
............... some other code ............
function char($parser,$data)
{
global $xmlFields, $fieldName;
if($fieldName) {
if($fieldName == "brandName" || "oeNumber" || "articleId" || "quantityPerPackingUnit" || "attrName") {
$xmlFields[$fieldName] = $data;
echo $data;
}
}
}
I try to echo $xmlFields["brandName"] for example, and nothing is printed.
1) I know that $xmlFields["brandName"] is non-empty because echo $data actually returns something.
2) If I change to $xmlFields[$fieldName] = 'some string';
then echo $xmlFields["brandName"] will print 'some string'
so why won't it print $xmlFields["brandName"]?
Thanks in advance,
Yazan
You cannot link ORs like that.
try
if($fieldName == "brandName" || $fieldName =="oeNumber" || $fieldName =="articleId" || $fieldName =="quantityPerPackingUnit" || $fieldName == "attrName") {
As Deceze said a much better option when you are searching in an array is to use
if (in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName")))
I know some languages allow such construct but php is not one of them.
The following expression
$fieldName == "brandName" || "oeNumber" || "articleId" || "quantityPerPackingUnit" || "attrName"
is parsed as
(
(
(
($fieldName == "brandName") || ("oeNumber")
) || ("articleId")
) || ("quantityPerPackingUnit")
) || ("attrName")
Notice that your equality check is separated from the other checks. In this case, the expression always evaluates to true.
You can use an array for this case:
in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName"));
Try this as a shorter version of Iznogood's answer:
if (in_array($fieldName, array("brandName", "oeNumber", "articleId", "quantityPerPackingUnit", "attrName")))