PHP if statement and ob_flush - php

If they are same do nothing, if they are different do the bottom and make them same. Whole script is in a loop but the loop is only doing else. Like it is not checking for conditions. What am I doing wrong?
for ($int = 0; $int < 10, $int+1){
$array = $getarray();
foreach ($array as $array){
$var1 = $array->id;
$var2 = null;
if ($var1 == $var2){
echo ('skip');
}else{
echo '<br>'.$var1.'<br>';
ob_flush();
$var2 = $var1;
}
}
}
Initially var2 is null and var1 gets string value. So it performs else and var2 gets the var1 string.
Next Loop if var1 has not changed, it will equal var2. So it does nothing and skips

I don't understand the logic of these loops but this is how you should do it:
for ($int = 0; $int < 10, $int++) {
/*
* I suppose getarray() is a function.
* If you have a variable $getarray containing the name of another function
* then you should change getarray() to $getarray()
*/
foreach (getarray() as $array) {
if (is_null($array->id)) {
echo ('skip');
} else {
echo "<br />{$array->id}<br />";
ob_flush();
}
}
}

Your code does not work properly, as how many times your loop will execute, your $var2 will become null, so not a single time you will be reached at the point where your $var1 & $var2 values are same.
So instead please try the following code.
for ($int = 0; $int = 10; $int+1){
$array = $getarray();
$var2 = null;
foreach ($array as $array){
$var1 = $array->id;
if ($var1 == var2){
echo ('skip');
}else{
echo '<br>'.$var1.'<br>';
ob_flush();
$var2 = $var1;
}
}
}

Related

Get key after inserting into array PHP

I'm looking for something like this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1);
$j = array_push($a, $var2);
echo $i;
echo $j;
The expected output would be:
0
1
I want to know the index of the object I just inserted, to be able to find it quickly afterwards. I think array_push gives me the size of the resulting array, not the index for the recently inserted element
array_push return new number of elements in the array, so decrement the return value by 1
Try this:
$a = array();
$var1 = 'var1';
$var2 = 'var2';
$i = array_push($a, $var1) - 1;
$j = array_push($a, $var2) - 1;
echo $i;
echo $j;
function my_push_array(&$array, $value){
$array[] = $value;
end($array);
return key($array);
}
$a = ['h','e','l','l'];
echo my_push_array($a, 'o'); //returns 4

Putting Incremental values in array

I have the following code
<?php
for ($i=1; $i<=4; $i++)
{
echo $i . "<br>";
}
?>
which Results 1 2 3 4
I have another code which have a variable $number = 1 and coding is as follows
if ($number = $i)
{
echo "Message" ;
}
MY issue is i need to check the variable $number(1) is available in the list of values displayed by variable $i.
Can anybody find a solution for that? Thanks in advance .
You should use == (comparison operator) instead of = to compare two values within an if statement, as follows:
if ($number == $i)
{
echo "Message" ;
}
However, assuming that it's just a typo, here is my suggestion:
You can store the numbers to be printed in an array first.
$i = array(1,2,3,4);
foreach($i as $num)
{
echo $num."<br/>" ;
}
Then, you can use the in_array() function of php to check if $number is present in any of the numbers printed above.
if(in_array($number, $i))
{
echo "Message" ;
}
try this this will put your incremental values in array and after that you can validate with any number
$inc=array();
for ($i=1; $i<=4; $i++)
{
array_push($inc,$i);
}
$number=1;
if(in_array($number, $inc))
{
echo "your Message" ;
}
= is the assigment operator, to compare you have to use ==
if ($number == 1) {
//...
}
Furthermore there are situatons where using
if ($number = $value) {
//...
}
is of use. this code would assign $value to $number and 'return' its value, therefore if ($number = 1) echo "hi"; will echo "hi" - while `if ($number = 0) echo "hi"; will output nothing. A assign like the above is quite useless, it is mostly used when you call a function, you want to restore its return value and compare it in the same step like
if ($number = getValue()) {
//$number is set to getValue() and is not 0, null or false
}

php - use variable variable in while loop

I'm a bit confused about variable variables.
What I like to do is print the value of $var1, $var2 and $var3:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo $var.$i;
$i++;
}
I know $var.$i; is not correct, but I think it shows what I would like to achieve; the while-loop should change it to $var1, $var2 and $var3;
I've tried the following:
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
$current_var = 'var'.$i;
$current_var = $$current_var;
echo $current_var;
$i++;
}
But that doesn't work. I think because $var1, $var2 and $var3 are recreated in the while-loop instead of using the actual value. Not sure if that's correct, but that the only thing I can think of.
Try this instead:
echo ${"var".$i};
Curly braces can resolve to variable names without having to use the dollar-dollar approach.
See: Variable Variables in PHP
Try this one.
<?php
$var1 = 'a';
$var2 = 'b';
$var3 = 'c';
$i = 1;
while ( $i <=3 ) {
echo ${'var'.$i};
$i++;
}
?>
Are you trying to do something like this. Then use array
$my_data = array();
$my_data[1] = 'a';
$my_data[2] = 'b';
$my_data[3] = 'c';
// Method 1
$i = 1;
while ($i <= 3) {
echo $my_data[$i];
$i++;
}
// Method 2
foreach ($my_data as $data) {
echo $data;
}
// Output
abc
abc

Check how many variables equal zero with php

I have numerous variables and I need to print a numeric value for how many of these variables are equal to zero:
$var1 = '2';
$var2 = '0';
$var3 = '4';
//check how many variables = 0
$zeros = ?
//should be a numeric value this example should print '1'
echo $zeros
Thanks for any suggestions!
This gets a list of all defined variables, and counts how many are === 0 and excludes the $_GET, $_POST, and $_COOKIE globals.
If you define this code in a function, then only the variables in that scope are counted.
<?php
$value = 0;
$var = 1;
$test = false;
$nine = 0;
$zero = 9;
$zeroes = 1; // set to 1 so we don't count this
$vars = get_defined_vars();
foreach($vars as $var) {
if (is_array($var) && (
isset($var['_GET']) || isset($var['_POST']) ||
isset($var['_COOKIE']))
) {
continue; // don't count superglobal arrays
}
if ($var === 0) $zeroes++;
}
$zeroes -= 1; // subtract the initial value
echo "There are $zeroes zero values."; // There are 2 zero values.
EDIT: It could be modified to be a function that would work recursively if you needed to check the values of arrays for example. You could call it from the global scope like this:
$zeroes = countZero(get_defined_vars());
And then the function could detect arrays and call itself until it has searched all vars.
if($var1 == 0) $zeros++; repeat for var2, 3, etc.
Use variable variables to generate variables from a string if they all follow the varX pattern where X is a number.
$var1 = '2';
$var2 = '0';
$var3 = '4';
$zeros = 0;
for($i = 1; $i <= 3; $i++) {
$var = ${'var' . $i};
if($var == 0) $zeros ++;
}
echo $zeros;
I do not understand the purpose of what you are trying to achieve here and maybe I am misunderstanding the need but the following yet simple code does it:
$var1 = '2';
$var2 = '0';
$var3 = '4';
$zeros = ($var1 =='0')?1:0 + ($var2 =='0')?1:0 + ($var3 =='0')?1:0;
echo $zeros
Basically I nested ternaly conditional expressions instead of using regular if then elses.
I put all of my variables into an array, looped with foreach and added to the $zeros variable:
$values = array('0' => $var1, '1' => $var2, '2' => $var3);
$zeros = 0;
foreach($values as $v) {
if($v === '0')
$zeros++;
}
echo $zeros;
I found this was an easy solution, thanks for the input though!

assignment operator (=) within a function's parameter list?

I'm using the following code from PHPBuilder.com to handle user privileges on my site:
/**
* Correct the variables stored in array.
* #param integer $mask Integer of the bit
* #return array
*/
function bitMask($mask = 0) {
if(!is_numeric($mask)) {
return array();
}
$return = array();
while ($mask > 0) {
for($i = 0, $n = 0; $i <= $mask; $i = 1 * pow(2, $n), $n++) {
$end = $i;
}
$return[] = $end;
$mask = $mask - $end;
}
sort($return);
return $return;
}
and I'm a bit baffled by the "= 0" part of ($mask = 0) in the function parameter list. What does that do?
It means that if you call the function like this:
$a = bitMask();
Then $mask will be set to 0.
It is how you set default values to parameter in functions.
Example:
function example($a=0){
echo "a = $a";
}
example(10);
example();
Output:
a = 10
a = 0
If $a did not have a default value set, then calling the function like example() would give a warning.
reference: http://php.net/manual/en/functions.arguments.php (Default argument values)
That's the default value of $mask if no arguments are passed. This also prevents a warning from being generated when the parameter is omitted.
Michael's response is correct. To add to it, take note that the assignment will not have an affect on the original variable modified. Here is his code with a few more assignments / echos to illustrate this:
function example($a=0){
echo "Entering function: a = $a\n";
$a = 3;
echo "End of function: a = $a\n";
}
$a = 7;
example(10);
echo "Outside of Function: a = $a\n";
Outputs
Entering function: a = 10
End of function: a = 3
Outside of Function: a = 7

Categories