Error while providing the condition for if Statement - php

$h = 30;
$flipSum = $h - 1;
if (5 < print $flipSum) {
echo "Yes";
}
else {
echo "No";
}
The output of this file is "29No". Where is the error? I want the output to be "Yes".
Also, I'm getting errors when I remove $h and $flipSum and replace it using one variable like below. How do i go about with the bracketing?
$h = 30;
if (1 < print $h - 1) {
echo "s";
}
else {
echo "Sa";
}
This is giving the same output "29No".
Thank you,
Sai

The opening statement to your conditional should be:
if (5 < $flipSum) {
What you want is to evaluate 5 < $flipSum, where $flipSum is presumably some number. What you're actually doing is evaluating 5 < print $flipSum – where the return value of print $flipSum is always 1. Therefore, you are seeing No as the returned value.
More info: the reason the return value is 29No is because – in 5 < print $flipSum – two things are happening (in order):
The value of $flipSum is being printed – hence 29
The entire conditional is evaluated (to false) and the corresponding block is entered – hence No

print statement in PHP returns 1 always.. That's why you were getting No as the output
More about print:
http://www.php.net/print

Do not use print construct in your if logic as it always returns 1:
$h = 30;
$flipSum = $h - 1;
if (5 < $flipSum) {
echo "{$flipSum}Yes";
} else {
echo "{$flipSum}No";
}

Related

"break" doesn't work as expected

I want to check the string length of comment array.
Once any of them is equal or higher than 4, I want to echo the relevant value, and then stop.
I guessed that using while should be good,
but if I break the loop at 4 or more, nothing gets echoed.
If I will break it at 5 or more, the previous two 4-string values will be echoed, but I want only the first 4-string value to get echoed, and then stop.
$comment[1] = "abc"; // add comment below text button
$comment[2] = "xyz"; // add comment below text button
$comment[3] = "abcd"; // add comment below text button
$comment[4] = "xyza"; // add comment below text button
$comment[5] = "abcde"; // add comment below text button
$comment[6] = "xyzab"; // add comment below text button
$x = 1;
while ($x <= 10) {
if (strlen((string)$comment[$x]) >= 4 ) {
echo $comment[$x];
echo "<br/>";
}
$x = $x + 1;
if (strlen((string)$comment[$x]) >= 4) break; // Nothing get echoed
// if (strlen((string)$comment[$x]) >= 5) break; // two values get echoed
}
Also, is there maybe a better/shorter practice to check this thing, maybe some built in function like in_array?
The problem with your code is that your loop body checks/prints one element and breaks on different one because you increment the pointer between those two points. You could have moved your break statement above the increment, or even put it into that if statement (much like #A-2-A suggested). Then it should work as expected.
With break above the increment:
while ($x <= 10) {
if (strlen((string)$comment[$x]) >= 4 ) {
echo $comment[$x];
echo "<br/>";
}
if (strlen((string)$comment[$x]) >= 4) break;
$x = $x + 1;
}
With combined echo/break:
while ($x <= 10) {
if (strlen((string)$comment[$x]) >= 4 ) {
echo $comment[$x];
echo "<br/>";
break;
}
$x = $x + 1;
}
Also you may want to iterate the array up to it's length instead of hardcoded limit of 10:
$x = 0;
$length = count($comment);
while ($x < $length) {
// ...
}

Difficulties with is_int function in PHP

I'm writing a code to solve a problem.one of my functions is not working properly.
function check_factor($sqr,$num)
{
for($i = $sqr ; true ; $i++)
{
$n = pow($sqr,2) - $num;
$s = sqrt($n);
if(is_int($s))
{
return $i;
}
}
}
I know that $s is a "double" , but even when I limit my loop counter to 2,I'll get an endless loop.
What am I missing here? why the function doesnt simply return null? and why I get Infinite loop even when there are 2 iterations?
true is making it an infinite loop
you will always get an infinit loop because sqrt() returns float means
$s = sqrt($n);
$s is a float now
and your test is testing if $s is an integer so it will be an infinit loop even after $i=2 the loop will always stuck and the $i=2 in every loop but
if you change the code to this
<?php
function check_factor($sqr,$num)
{
echo "<br>";
for($i = 1 ; $i < 3 ; $i++)
{
echo " in the loop $i<br>";
$n = pow($sqr,2) - $num;
echo "$n<br>";
$s = sqrt($n);
echo "$s<br>";
if(is_int($s))
{
echo "in the if <br>";
return $i;
}
}
return 0;
}
$val=0;
$val = check_factor(5,2);
echo "<br>$val<br>";
?>
the out put should be like this
in the loop 1
23
4.7958315233127
in the loop 2
23
4.7958315233127
0
and that's it. i hope i helped.
According to the manual: PHP function sqrt() always returns a float and NEVER an integer.
Check http://php.net/manual/en/function.sqrt.php

Add An If statement to a value using php

how do i add an if statement to a Php value, this is what i tried
&n = 1;
$number = if(10 < 1)
{
$n = 0;
}
is there any way to fix this or something i didn't ad that made it not to work?
because it kept showing an error message that the code is not right
You have a typo and some syntax error.
&n = 1;
That should be:
$n = 1;
Also, you can't really assign the value of an if statement to your variable.
Try simply using a ternary instead.
$n = (10 < 1) ? 1 : 0;
In PHP, if is a statement, but the = operator expects an expression, so that is invalid syntax. If you want to assign something to $number when the condition is true, then you can use something like:
if (10 < 1)
{
$number = ...;
$n = 0;
}

PHP for loop : equal or smaller than - works, equal to - creates infinte loop

This code works
<?php
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
but if instead of " <= " I use simply " = " php gets into an infinite loop that timesout
<?php
for ($x = 0; $x = 10; $x++) {
echo "The number is: $x <br>";
}
?>
Is that expected behavior?
Yes, you're using an assignment operator instead of a comparison operator. Now there's no way for that loop to end.
Yes, it's expected.
You must use <= instead of = only
<= is a comparaison operator : http://php.net/manual/en/language.operators.comparison.php
= is an assignment operator : http://php.net/manual/en/language.operators.assignment.php

Can you help me get this PHP explode/array/numrows/etc block working?

Ok, so here's my code:
if ($_GET['send'] === "yes") {
$name = $_POST['msg-to'].", ";
$nameParts = explode(", ", $name);
$recipients = array();
for ($x = 0; $x >= 10; $x++) {
$name_query = mysql_query("SELECT * FROM users WHERE username='".$nameParts[$x]."'");
while($value = mysql_fetch_array($name_query)){ $name_numrows = mysql_num_rows($name_query); }
if ($name_numrows = 1) {
$recipients[$x] = $nameParts[$x];
$msgError .= '<span class="success">'.$nameParts[$x].' is a valid user.</span><br>';
} else {
$msgError .= '<span class="warning">'.$nameParts[$x].' is not a valid user, message did not send.</span><br>';
break;
}
}
}
But when a user enters a username for this message to be sent to, it doesn't seem to work AT ALL. It doesn't echo either of the two error messages, and doesn't return an error. It doesn't do anything.
Any feedback at all would be absolutely wonderful :D
I tried to help in the comments above but I think a more clear explanation is needed so I'm resorting to posting an answers. Your code:
for ($x = 0; $x >= 10; $x++) {
This code block declares $x = 0 as the first part of the statement, this is the initialisation.
The second part $x >= 10 is the condition. It states that while $x is greater than or equal to 10 you want to execute an iteration of the loop.
The final part $x++ is the afterthought. It states that on each successful iteration of the loop you want to increment the value of $x.
Because you initialise $x to be 0 and then set the condition that it has to be greater than or equal to 10 >= 10 the condition will fail first time, every time. 0 can't be great than or equal to 10. I imagine what you probably want for your condition is something like while $x is less than or equal to 10 $x <= 10.

Categories