Find greatest of three values in PHP - 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).

Related

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.'#');
}

If else with same code, but different condition

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.

How to find the common divisors of two numbers in PHP?

I use the following to find out the common divisors.
But in some case the count of divisors are not satisfied.
My Code :
$x = 66928;
$y = 66992;
$c_a = [];
$c_b = [];
$d = 1;
while ($d_a <= $x) {
if (is_int($x / $d)) $c_a[] = $d;
$d++;
}
$d = 1;
while ($d_b <= $y) {
if (is_int($y / $d)) $c_b[] = $d;
$d++;
}
echo count($c_a);
echo count($c_b);
// Output
$c_a = 20;
$c_b = 20;
Because, in some cases, it won't work.
Is this type of calculation is right ?
or any suggestions ?
As per asked in comment, to count the common factors of the two no. will be as like this.
<?php
$a = 66928;
$b = 66992;
$min = ($a < $b ) ? $a : $b;
$commomn_factors_count = 0;
for ($i = 1; $i < $min/2; $i++) {
if (($a%$i==0) && ($b%$i==0)) {
$commomn_factors_count++;
}
}
var_dump($commomn_factors_count);
You can you this code to get the fastest result to find the number of common divisors between two numbers:
// Function to calculate gcd of two numbers
function gcd($a, $b)
{
if ($a == 0)
return $b;
return gcd($b % $a, $a);
}
/* Function to calculate all common
* divisors of two given numbers
* a, b --> input integer numbers
*/
function commDiv($a, $b)
{
// find gcd of a, b
$n = gcd($a, $b);
// Count divisors of n.
$result = 0;
for ($i = 1; $i <= sqrt($n);
$i++)
{
// if 'i' is factor of n
if ($n % $i == 0)
{
// check if divisors
// are equal
if ($n / $i == $i)
$result += 1;
else
$result += 2;
}
}
return $result;
}
// Driver Code
$a = 10; $b = 15;
echo(commDiv($a, $b));

For loop not changing value

I need to subtract 10 from a value until it's below ten and then use it outside of the loop but the value doesn't seem to change.
I'm not sure how many wrongs I'm making but I bet it's many!
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b - 10){
echo $b; //This is supposed to be echo:ed when the loop is done
}
Thanks in advance!
You are not actually modifying $b:
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b = $b - 10) { // <- this line
echo $b;
}
Also, there is no need for the initial $b here:
for(; $b > 10; $b = $b - 10) {
Or you could get rid of:
$b = $y - $x;
And just use:
for($b = $y - $x; $b > 10; $b = $b - 10) {
Or you could just do
$x = 1987;
$y = 2015;
$b = ($y - $x) % 10;
Which is basically what you're doing, only you chose the hard way with the for loop :)
$x = 1987;
$y = 2015;
$b = $y - $x;
for($b; $b > 10; $b -= 10) {
echo $b;
}
$x = 1987;
$y = 2015;
$b = $y - $x;
for(; $b > 10; $b -= 10);
echo $b;
echo will happen only after completing loop. This will reduce the $b value by multiples of 10. $b will be less than 10 after loop.
A while loop is more readable than a for loop. Also, put the echo statement after the loop has completed.
$x = 1987;
$y = 2015;
$b = $y - $x;
while($b > 10) {
$b -= 10;
}
echo $b;

PHP: Return a value from a Function and change the variable for the while Loop that it is in

Is it possible to execute a function, then return a new value for Var and therefore when it tries to loop again it checks the condition and detects the changes?
Let's say I have this function:
function changeZ(){
$z=1
return $z;
}
and this is my while loop:
$a = 0;
$z = 10;
while($a<$z){
changeZ();
$a++;
}
How should I modify the codes such that
the function changeZ() returns a new value for the variable $z
and therefore when the loop checks for condition (1<1), it returns a false and stop looping.
You can pass by reference or return the value
$z = 100;
change($z);
function change(&$var) {
$var = 1;
}
$z = 100;
$z = change($z);
function change($var) {
return $var * 100 - 50;// Logic with $z obviously can be whatever
}
The $z in your function and the $z you use in the loop are not the same guys. So you have to set the value of the loop-z to the return value of your function...
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
$z = changeZ();
but as commented, you appear to definitely going about this wrong.
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ();
$a++;
}
Since your function returns a value you should set your variable to contain the returned value. This will do what you asked resulting in the loop running only once.
Another thing you can do is pass the variable into a function like so
function changeZ($in){
$out = $in-1;
return $out;
}
$a = 0;
$z = 10;
while($a<$z){
$z = changeZ($z);
$a++;
}
This will result in the loop running 5 times as one number goes up the other goes down
0<10
1<9
2<8
3<7
4<6
you can use this for your purpose for storing the returned data from function into an array.
function changeZ(){
$z = $row['result']; //your data from database
return $z;
}
$a = 0;
$z = 10;
$returned_value=new array();
while($a<$z){
$returned_value[] = changeZ();
$a++;
}
You have to put this
$a = 0;
$z = 10;
while($a<$z) {
$z = changeZ();
$a++;
}

Categories