If else with same code, but different condition - php

I have got an if/elseif statement, both with the exact same code, but I want to trigger them depending on the condition. I have got the exact same code duplicated in the if statement aswell as the elseif statement.
$x = 10;
$y = 20;
if ($x <= $y) {
// some code
}
elseif ($x >= $y) {
$oldX = $x;
$x = $y;
$y = $oldX;
// same code as if
}
I am curious if there is a way that I do not have to duplicate my code in both the if statement aswell as the elseif statement.

The way I see it, you have two ways of going about this.
Alternative one: Create a function
$x = 10;
$y = 20;
function myCodeBlock($x, $y) {
// some code
}
if ($x <= $y) {
myCodeBlock($x, $y);
} else {
myCodeBlock($y, $x);
}
Alternative two (probably the most preferable): Put the codeblock after the check where you mix up the values of the variables.
$x = 10;
$y = 20;
if($x >= $y){
$oldX = $x;
$x = $y;
$y = $oldX;
}
// some code
Keep in mind, that by doing
elseif($x >= $y){
$x = $y;
$y = $x;
...then $y and $x become equal - as you set $x to the value of $y, but then set $y back to $x, which was $y.

Related

How to out of while and for loop combined

I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}

How to add element into array on php recursive loop?

function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
2#
How to make the output become :
2#,3#,4#
if($y < 5)
{
test_loop($x_values, $x, $y);
}
You're not doing anything with the return value of the recursive function call.
You need to add the array returned from test_loop() to the existing array:
$x_values += test_loop($x_values, $x, $y);
Your code now prints: 2#3#4#5#6#
If you want the output to be 2#, 3#, 4#, 5#, 6#, you can use the implode() function instead of a loop:
echo implode (', ', $bababa);
You'll want to utilize pass by reference:
https://www.php.net/manual/en/language.references.pass.php
I haven't looked too closely at your code, you may need to make more changes than this... but this might work:
function test_loop(&$x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
You should use array_push() function to add an element in the array
Documentation
if($x < 10)
{
array_push($x_values,$x.'#');
}

PHP select variable by name with other variable

So I have a list with variables (auto-generated), something like:
$won3 = 1;
$time3 = 4;
$won6 = 0;
$time6 = 5;
$won4 = 0;
$time4 = 5;
(...)
but with many more variables. Now I want to make a table with all the variables, so I used a for-loop, but $won1 has to be the first in the table, then $won2 etc...
But how can I recall this $won1 in a for-loop? I tried:
for ($X = 0, $X < $Y, $X++){
echo '$won'.$X;
}
but this does not do the job. Anyone knows how I can solve this?
Thanks in advance.
First... In for loops you can't use ,
for ($X = 0, $X < $Y, $X++){
Try this:
for ($X = 0; $X < $Y; $X++){
.
And you have to choices...
for ($X = 0; $X < $Y; $X++){
$var = 'won' . $X;
echo $$var;
echo ${'won' . $X};
}

Why do these nested while loops not work?

I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.

Find greatest of three values in PHP

With three numbers, $x, $y, and $z, I use the following code to find the greatest and place it in $c. Is there a more efficient way to do this?
$a = $x;
$b = $y;
$c = $z;
if ($x > $z && $y <= $x) {
$c = $x;
$a = $z;
} elseif ($y > $z) {
$c = $y;
$b = $z;
}
Probably the easiest way is $c = max($x, $y, $z). See the documentation on maxDocs for more information, it compares by the integer value of each parameter but will return the original parameter value.
You can also use an array with max.
max(array($a, $b, $c));
if you need to
<?php
$a=20;
$b=10;
$c=1;
if($a>$b && $a>$c)
{
echo "Greater value is a=".$a;
}
else if($b>$a && $b>$c)
{
echo "Greater value is b=".$b;
}
else if($c>$a && $c>$b)
{
echo "Greater value is c=".$c;
}
else
{
echo"Dont Enter Equal Values";
}
?>
Output:
Greater value is a=20
If you want to Compare Three Variables. Compare two integers and get maximum of them by using max() function. Then compare the maximum with the third variable!
$x = 1; $y = 2; $z = 3;
$maximum = max($x, $y);
$c = max($maximum, $z);
echo $c; //3
Also you could do it just in one line max(max($x, $y), $z).

Categories