PHP Multiple condition if statement returning nothing - php

I have been trying to figure this out for weeks, it is a simple statement yet it never executes the if statement. I'm echoing the variables to the screen so I know they exist and each condition is true, but the echo "something" never executes.
if ($db_a==$session_a && $db_b==$session_b && $dbfreq_b=="1")
{
echo "something";
}
I thought it was just the brackets as I had this originally:
if (($db_a==$session_a) && ($db_b==$session_b) && ($dbfreq_b=="1"))
I am comparing variables stored in a MYSQL database with session variables.
If I use Var_dump the db variables are null, yet they echo the expected string value to the screen.
$db_a="username";
$session_a="username";
$db_b=="keyword string"; -mostly one word but could be multiple
$session_b=="keyword string";
$dbfreq_b="1"; - This is the frequency that the keyword appears in the MYSQL database
using vardump $db_a and $db_b are NULL yet they echo what I am expecting to see to the browser.
Hopefully this explains things a bit more?
Thank you for the very prompt help!!

If as you say $db_a = $session_a AND $db_b = $session_b AND $dbfreq_b = 1 then it's impossible that condition returns false.
Please check your code again (all 5 variables) and make sure ALL of the conditions are met.
You could just split your single IF into three separate conditions so that you know which one returns false.
if ($db_a == $session_a) {
echo "first OK\n;"
}
if ($db_b == $session_b) {
echo "second OK\n";
}
if ($dbfreq_b == "1") {
echo "third OK";
}
Could you add the values of your variables to your question?

Related

storing something if the statement is true

This is a simple code I can't figure out
if(preg_match( "/where/","where's my backpack") == true){
echo "this is a match";
// this is the part i cant figure out
// how can I store it as == $match1
// I tried $match1 == "this is a match"; but it didn't work
}else{
//$match1= " "; << this what I tried and didn't work
//when I say echo match1 later, it displays this is a match even when its wrong
}
how can I store it as a variable or something for later. for example, if those two matches are true, store it as $match1 so that I can later say 'echo $match1' so that it displays "this is a match". if not just display a space.
the overall code is in php BTW
== operator checks for equality, to store something you need to use =
$match1 = '';
if(preg_match("/where/", "where's my backpack"))
$match1 = "this is a match";
else
$match1 = " ";
echo $match1;
You can assign it directly:
$match = preg_match("/where/","where's my backpack") === 1
Now you have a boolean value that you can use anywhere after this statement.
Note that I have replaced your true with 1 as that is what preg_match will return but either will work.
By the way, I realize that this is not exactly what you want but I find it easier to maintain and more reusable when I separate output texts from logic like this.

PHP: Random "1" outputted to the screen

I have the following code:
if ($currentStage == 7)
{
echo include("include/contentP7.php");
}
The content of "content7.php" exists however it is blank.
But when $currentStage is equal to 7 the page is displayed and a random "1" is outputted although "content7.php" is blank.
I assume it may be to do with returning "True" to the if statement. Why is this and how can I remove this "1".
include returns TRUE upon success, when echoed, it becomes 1.
Omit the echo statement:
if ($currentStage == 7) {
include("include/contentP7.php");
}
Include should be on its own.
include probably returns true(1) when includei successful. Remove the echo to get rid of the 1
if ($currentStage == 7)
{
include("include/contentP7.php");
}
I don't understand what the other parts were intended to do. If you wanted to echo a value from contentP7, place that content into a variable (perhaps a HEREDOC or something). Then include and echo like this:
if ($currentStage == 7)
{
include("include/contentP7.php");
echo $contentP7_variable;
}
The "1" or True value might be returned because you are echoing the returned status of the include() but I am not sure how since php.net's manual explains that it is a language construct. I can't test this right now, unfortunately.

Php test empty string

I have a bit of php code that I'm not understanding why it is acting as it is. I have a variable called contactId that I want to test to see if it is empty. However even if it is empty it evaluates to true. Code is below. Thanks in advance.
print "*".$contactId."*<br/>";
if($contactId != '')
{
//queryContact($contactId);
print "Contact Present<br/>";
}
result returned to screen is:
**
Contact Present
If you want to see exactly what your string is, simply use var_dump(), like this, for instance:
var_dump($contactId)
instead of
print "*".$contactId."*<br/>";
Couple of things you can try:
if (!empty($contactId)) {
// I have a contact Id
}
// Or
if (strlen($contactId) > 0) {
// I have a contact id
}
In my experience I have often used the latter of the two solutions because there have been instances where I would expect a variable to have the value of 0, which is valid in some contexts. For example, if I have a drink search site and want to indicate if an ingredient is non-alcoholic I would assign it a value of 0 (i.e. IngredientId = 7, Alcoholic = 0).
Do it with if (isset($contactId)) {}.
You likely want:
if (strlen($contactId))
You'll want to learn the difference between '' and null, and between == and ===. See here: http://php.net/manual/en/language.operators.comparison.php
and here: http://us3.php.net/manual/en/language.types.null.php
In future, use if(!empty($str)) { echo "string is not empty"}.

If first argument falls true in a condition with and operators will the next run?

Lets say I've got a pice of code that looks like this:
if( !isset($this->domainID) && !$this->getDomainID() ){
return false;
}
Will the 2nd statement run if the first one is true?
Because performance wise it would be stupid to get the ID from the database if I've already got it and there's a lot of other situation where the same apply. If it doesn't I'd have to nest them, am I right?
I don't know if there's an standard on how programming language work in these cases or if it's different in other languages then php. I tried googling it but I didn't really know what to search for in this case. As you can see I hade a quite hard time describing it in the title.
Yes. If the first is true, the second will be evaluated. Conversely, if the first is false, the second will not be evaluated. This can be a good place for micro optimizations and, as you noted, logical progression.
Per the comments, the inverse is true for OR conditions. So if the first expression is false the next will be evaluated and if the first expression is true the next will not.
No, if the first is false, the second will not be evaluated.
What you're really talking about here is "lazy evaluation". It's often used in functional programming languages and can vastly improve the run-time. See here for more info: http://en.wikipedia.org/wiki/Lazy_evaluation
PHP does not use lazy evaluation but in conditionals like this it does at least stop before the second argument if the result is already clear.
Answer is yes, you can see it by yourself by doing something like this:
#!/usr/bin/php
<?PHP
function test1() {
echo "inside test1\n";
return false;
}
function test2() {
echo "inside test2\n";
return true;
}
echo "test1 first AND test2 second\n";
if (test1() && test2()) {
echo "both passed\n";
}
echo "test2 first AND test1 second\n";
if (test2() && test1()) {
echo "both passed\n";
}
echo "test1 first OR test2 second\n";
if (test1() || test2()) {
echo "one passed\n";
}
echo "test2 first OR test1 second\n";
if (test2() || test1()) {
echo "one passed\n";
}
?>
Yes, you are using an AND so both conditions will be checked if the first is true (but not if the first is false).
If you had used an OR and the first condition was true, php wouldn't evaluate the second.

Strange Boolean Reaction

$alerter2="false";
for ( $counter = 0; $counter <= count($filter); $counter++) {
$questionsubmitted=strtolower($_POST[question]);
$currentcheck =$filter[$counter];
$foundvalue=stripos((string)$questionsubmitted,(string)$currentcheck);
echo $foundvalue;
if ($foundvalue==0) {
$alerter2="true";
} else { }
}
if (!($alerter2=="true")) {
$sql="INSERT INTO Persons (Name, Email, Question)
VALUES
('$_POST[name]','$_POST[email]','$_POST[question]')";
} else {
echo "Please only post appropriate questions";
}
For some reason, whenever I run this, stripos returns 0 every time for every iteration. It's supposed to be a filter, and using echo I found that stripos is 0 every time that it appears. However, when I use 0 in the if, it returns true for even those that don't have the word in them.
Where should I use mysql_real_escape_string? After the query? Note, I am making this a piece of code where I want user input to be saved to a database.
stripos return false if the value is not found, or 0 if it is the first character. Problem is, php automatically cast boolean to the 0 integer or the 0 integer to false. So I think a cast is happening here and thus the condition don't do what you want.
You can use === to also check the type of the variable :
if ($foundvalue === 0) {
$alerter2="true";
}
There's more details about this problem in the linked documentation for stripos.
You should also remove the empty else clause for a cleaner code and use mysql_real_escape_string to sanitize the values before putting them in your database.
You need to change
if ($foundvalue==0)
to
if ($foundvalue===0) // three equals signs
or something equivalent, depending on your logic (I didn't quite understand what's going on).
But as everyone says, THIS CODE IS OPEN TO SQL INJECTION ATTACKS (among other problems).
Also,
$questionsubmitted=strtolower($_POST[question]);
should probably be:
$questionsubmitted=strtolower($_POST['question']);

Categories