'IF' statement doesn't work right? [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
When I print out the integers (1 or 0), all the iterations (there are 3 of them in this case) print out the correct number. First and the second one return 1, the last one returns zero. BUT, 'if' statement seems to display everything in it anyway. What am I doing wrong?
All the code below is inside a bigger 'for' loop.
$yn = 0;
if(!in_array($pos, $blocks)){
$blocks[$x] = $pos;
$x++;
$yn = 1;
}
print_r($blocks);
print "YN: ".$yn; # this prints out 1, 1 and 0 on the last iteration
if(yn){
# show some stuff (is displayed in all three iterations, but it shouldn't be on the last)
}

Try:
if($yn){
PHP is interpreting yn as a string, rather than as the variable you should be using.

Related

SUM OF NUMBERS IN WHILE LOOP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 days ago.
Improve this question
I'm trying to find the sum of numbers by querying a database using PHP and MYSQL. I need a while loop or any other solution that can add numbers that are >= 40 from a sequence of numbers. I'm only able to make the query but don't know how to go about the rest.
$selectMARK="SELECT mark FROM resultstbl WHERE mark >= 40";
$queryMARK=mysqli_query($dbCon, $select);
$sum=0;
while($rowz=mysqli_fetch_assoc($queryMARK))
{
// need some code to add the numbers here
}
You maybe need an
$iterator = 0;
outside the loop, that you can increment inside of it.
$sum += $rowz[$iterator];
$iterator++:
Then do your condition around it by checking $rowz[$iterator] <= 40

How to sum two rows input file numbers to one row output file (PHP) [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 months ago.
Improve this question
Input data:
In the input file INPUT.TXT there are two non-negative integers are given by two rows and the numbers are less than 10 powered by 100.
Output data:
in the OUTPUT.TXT file is needed to return sum of the numbers to one row, without initial zeros
Example:
#
input.txt
output.txt
1
3
7
4
Check it , It will help you
$data = explode("\n", file_get_contents('INPUT.TXT'));
$sum = intval($data[0])+intval($data[1]);
echo $sum;
file_put_contents('OUTPUT.TXT', $sum);

PHP: What is the best way to get last even value in array? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed last year.
Improve this question
Suppose , I have a array with some integers value (odd and even both mixed) in php.and I want to get last even number.
A loop that works backwards through the array would work.
$number=false;
$a=array(2,4,5,4,6,7,13,11,95,88,16,17,107);
for($i=count($a)-1; $i >= 0; $i--){
if( $a[$i] % 2 == 0 ){
$number=$a[$i];
break;
}
}
echo $number; //16

How to get certain part of a string value in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I get one value like "13and45" and would like to know if there is a way so I can store these values separately like
$string="13and45";
$value1=13;
$value2=45;
Do you guys know how I can manipulate the string in order to get something like what I have above?
PHP - Explode () function
This will return an array with the two values.
Example:
$array = explode ("and", $string);
Returned array with strings:
$array[0] = 13
$array[1] = 45
You can use something like intval(substr($string, 0, 2) to get the first integer and intval(substr($string, 5, 2) for the second. intval will get the integer value from a string, and substr will get the portion of the string given by ($string, index, length).

PHP Loop a functions parameter from 1 to 10 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a function called calculate. And inside the calculate function I have a paramater next to it
function calculate($id){
..
}
What I'm trying to figure out how to do is make a loop which will start the loop and the function calculate will start with the ($id) next to it from 1 to 10. So it does the same thing 10 times with the id set to 1 then 2 then 3 and so on to 10.
Try using range
function calculate($id){
//other statements
// return ;
}
foreach (range(0, 10) as $id) {
calculate($id);
}

Categories