RowCount() = 0 error php mysql - php

echo "<h2 style='margin:0; padding:0;'>Recent Comments</h2>";
if ($sth7->rowCount()) {
while($row7 = $sth7->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='comment'>{$row7['usr']} said";
}
}
else($sth7->rowCount() = 0)
echo "User";
Can't use method return value in write context
Why doesnt that rowcount() = 0 logic work?

= is the assignment operator in PHP.
You're basically trying to assign 0 to $sth7->rowCount().
Perhaps you mean $sth7->rowCount() == 0?
Also, you really don't need the if else if. It could be just an if else:
if($sth7->rowCount()) {
} else {
}
rowCount() returns an integer, and any integer except for 0 will cast to true.

Try rowcount() == 0 to compare with 0, your code (rowcount() = 0) tries to assign 0. Also, it's may be useful to put constant on the left side while comparing : (0 == rowcount()) to make such errors easier to detect.

else doesn't take any logic, it just runs if it's assosciated if (and any elseifs) didn't evaluate true.
Also, = is an assignment operator - read it as "becomes equal to"
$var = 1; // Var becomes equal to 1
== is an equality operator, it tests if two expressions are equal
$var == 1 // Var is equal to 1
You probably want
else if ($sth7->rowCount() == 0)
echo "User";

Related

How many = signs are required to equal a value in php?

I have following code which is not working with three equal signs (===). Hence it is working with single equal sign (=). I always use === in such cases and that works fine. I am confused why it is not working in this case?
$vendor_name = "Stock Returned";
if($vendor_name = "Stock Returned")
{
$stock_return === "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;
Result of above code is showing "No". But in my understanding it should be "Yes"
The 1 equal sign (=) is used for assigning values, it is not used for comparisons.
Comparisons are made with == or ===, that makes your $stock_return === "Yes"; line incorrect as well because it looks like you want to assign a value to $stock_return
The difference between == and === is that
== is for testing if both values are equal, with php, a string '20' and an integer 20 are equal
=== is more strict and will test the type as well so a string '20' is no longer equal to an integer 20
Your code should look something like this to be correct
$vendor_name = "Stock Returned";
if($vendor_name == "Stock Returned")
{
$stock_return = "Yes";
}
else
{
$stock_return = "No";
}
echo $stock_return;

Why does this PHP assignment in a loop always print the first value?

I'm very confused as to the output of this small piece of test code:
<?php
$count = 0;
while ($fn_retval = do_the_thing($count) !== false) {
print $fn_retval."\n";
$count++;
}
function do_the_thing($count) {
if ($count > 3) {
return false;
} else {
return $count;
}
}
?>
The output is:
$ php ./test.php
1
1
1
1
So it is correctly performing 4 iterations but always printing the return value of the first iteration. I feel like I must have missed something really obvious because this makes no sense.
Can somebody explain what's going on here because it seems like I can't use that assign-and-check construct in the way I thought I could.
The problem is that the order in which
$fn_retval = do_the_thing($count) !== false
is evaluated. If you check the Operator Precedence, you will see that !== is a higher precedence than =, so it's evaluated as
do_the_thing($count) !== false
and the result is then assigned to $fn_retval.
To force the order you are after, use brackets to explicitly do the assignment first...
while (($fn_retval = do_the_thing($count)) !== false) {

How can i detect if (float)0 == 0 or null in PHP

If variable value is 0 (float) it will pass all these tests:
$test = round(0, 2); //$test=(float)0
if($test == null)
echo "var is null";
if($test == 0)
echo "var is 0";
if($test == false)
echo "var is false";
if($test==false && $test == 0 && $test==null)
echo "var is mixture";
I assumed that it will pass only if($test == 0)
Only solution I found is detect if $test is number using function is_number(), but can I detect if float variable equal zero?
Using === checks also for the datatype:
$test = round(0, 2); // float(0.00)
if($test === null) // false
if($test === 0) // false
if($test === 0.0) // true
if($test === false) // false
Use 3 equal signs rather than two to test the type as well:
if($test === 0)
If you use=== instead of == it will compare as well as get the data types errors manage...Can you post your answer while using === ? Please check the difference between this two here
When comparing values in PHP for equality you can use either the == operator or the === operator. What’s the difference between the 2? Well, it’s quite simple. The == operator just checks to see if the left and right values are equal. But, the === operator (note the extra “=”) actually checks to see if the left and right values are equal, and also checks to see if they are of the same variable type (like whether they are both booleans, ints, etc.).

Variable assignment in a conditional?

What would this evaluate to? I know it looks funny but I was looking at a practice exam and saw this:
if (number = 1) { echo "C1 is true"; }
Whenever you are assigning variables it always returns true when the assigned variable is not causing false.So it will go to the if and echo the output.And consider that it mainly depends on the value that you are assigning.
Suppose if you do like
if (number = 0) { // if(number = false)
echo "C1 is true";
} else {
echo "C1 is false";
}
It will prints C1 is false.Bec it will indirectly indicate like
if(0) // if(false)
which is a false.
The assignment operator = returns the assigned value. What does that mean? For example, the + operator in 1 + 2 returns the sum of two numbers; the value of the expression 1 + 2 is 3. In the same vein, the value of the expression number = 1 is 1. That's why this works:
a = b = c = 1;
So you're assigning 1 to number, the resulting value of which is 1, which is evaluated by if, which equals true.

What is my error in the very simple php?

i make a very simple php to check a data, but it not works.
<?php
$ngl="G";
if ($parsed[0][4]="0") {
$ngl="NG";
}
if ($parsed[0][5]="0") {
$ngl="NG";
}
?>
and the output of the
<?php echo $ngl; ?>
is always
G
But I know that $parsed[0][4] and $parsed[0][5] is 0. The problem is that the output is G and not NG! I also tried to remove $ngl="G"; but then the output is nothing.
What do I have to repair?
i just use = but == its the correct. thx all.
You're assigning (=) a value instead of comparing (== or ===)
<?php
$ngl = "G";
if($parsed[0][4] == "0")
{
$ngl = "NG";
}
if($parsed[0][5] == "0")
{
$ngl = "NG";
}
?>
Explanation:
When you put an assignment like $parsed[0][4]="0" in an if statement, the if will evaluate the "0" to false. The reason it evaluates just the "0" is because the line $parsed[0][4]="0" (any assignment) returns the right hand side of the operation (the "0")
== vs === : php.net - Comparison Operators
== is a loose comparison which doesn't compare the type. ie "2" == 2 is true (even though one is a string and the other is an integer)
=== is a strict comparison, comparing types as well as values. ie "2" === 2 is false
The if statement uses the former (loose) comparison on your "0", and of course 0 is the false value in binary (0 and 1), so 0 == false and "0" == false both evaluate to true - however, 0 === false would return false as 0 is an integer, whereas false is a boolean.
Use == not = when checking
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
You use = to set variables. You use == to compare.
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
You should use == rather than =:
<?php
$ngl="G";
if ($parsed[0][4]=="0") {
$ngl="NG";
}
if ($parsed[0][5]=="0") {
$ngl="NG";
}
?>
The reason is that the = is the assignment operator, while the == is the comparison operator (which is the one you want).
$parsed[0][4]=="0" will evaluate to 0, which is false, so $ngl will not be changed. The same thing happens with $parsed[0][5]=="0"
This line is not checking if $parsed[0][4] is zero. It makes it zero.
if ($parsed[0][4]="0") {
Change it to
if ($parsed[0][4]=="0") {

Categories