What is the best solution for filling limited variables in PHP - php

Let's say I have 3 variables
$a;
$b;
$c = 30;
What I wish to do here is that I have to divide $c and put it in the first 2 variables which can be easily done by doing.
$a = $c / 2;
$b = $c / 2;
However what if in $b there is a maximum value limit of 10 and $a is limitless.
In this case the values have to be.
$a = 20;
$b = 10;
What would be the best solution for this?

If B has a limit, then you would need to calculate B first. Then work out the difference between C & B and set A as the answer.
<?php
$a;
$b;
$c = 100;
$b = min(10, $c / 2);
$a = ($c - $b);
echo "A: " . $a;
echo "B: " . $b;
?>

Try using min
$a = $c / 2;
$b = min(10, $c / 2);

Related

number_format multiple vars at once in php

I have the following script
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
print $a;
print $b;
print $c;
I want all three variables to be returned using the number_format($var) syntax. The three vars are being printed in various parts of an html template. What is the best way to do this for all three vars at once? Should I add these vars to an array and number_format the array?
The best that I can come up with is the following:
$a = 434343434343;
$b = $a *3;
$c = $a * 6;
$a = number_format($a);
$b = number_format($b);
$c = number_format($c);
print $a;
print $b;
print $c;
Is that preferred?
Put those numbers inside an array and format the array, it's faster.
$numbers = array();
$numbers['a'] = 434343434343;
$numbers['b'] = $numbers['a'] * 3;
$numbers['c'] = $numbers['a'] * 6;
foreach($numbers as $key => $val)
{
$numbers[$key] = number_format($val);
}
by the way, if you NEED the values as variables, you can extract them:
extract($numbers); //creates the variables $a, $b, $c
echo $a;
echo $b;
echo $c;
You can see it in action right here.
Seems I found a much better solution.
$a = number_format(434343434343);
$b = number_format($a *3);
$c = number_format($a * 6);
//$a = number_format($a);
//$b = number_format($b);
//$c = number_format($c);
//output
print $a;
print $b;
print $c;

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;

if a variable is referenced inside a function then what happens

function a(&$c, &$d){
$c = &$d;
}
$a = 1;
$b = 2;
a($a, $b);
echo $a;
output is 1,but shouldn't it be outputting 2 as $c is referencing $d. $c and $a reference to the same value,then $c refer to the value of $d which refer to $b so ultimately $a should refer to $b, isn't it correct?
For the operation you seek you must remove the ampersand reference used within the function a.
Example
function swap (&$one, &$two) {
$tmp = $one; // One is stored temporarily
$one = $two; // Two is stored in One
$two = $tmp; // Temporary data retrieved and stored in two
unset($tmp); // Temporary variable destroyed
}
// Set the variables
$a = 1;
$b = 2;
echo $a . " - " . $b . "<br />"; // See output as: 1 - 2
swap($a, $b);
echo $a . " - " . $b; // See output as: 2 - 1
in line
$c = &$d;
$a is referring to the address of $b but not to $b, then you can try this:
<?php
function foo(&$c, &$d){
$c = $d;
}
$a = 1;
$b = 2;
foo($a, $b);
echo $a;
?>

Loop not working correctly (floats and integer issue?) -- PHP [duplicate]

This question already has answers here:
Double? Integer? -- PHP
(2 answers)
Closed 8 years ago.
The code below generates two random decimal values, then subtracts them to get $c.
The do-while loop is trying to ensure that $c will not be a whole number. But I keep getting times where $c actually is a whole number.
do{
unset($a);
unset($b);
unset($c);
unset($adjuster);
unset($c_is_int);
$a = mt_rand(5, 75);
$b = mt_rand(5, 75);
$adjuster = mt_rand(2, 20);
$decimal_selector = mt_rand(1, 6);
if ($decimal_selector == 1){
$a = $a / 10;
$b = $b / 10;
}
if ($decimal_selector == 2){
$a = $a / 10;
$b = $b / 100;
}
if ($decimal_selector == 3){
$a = $a / 100;
$b = $b / 10;
}
if ($decimal_selector == 4){
$a = $a / 100;
$b = $b / 100;
}
if ($decimal_selector == 5){
$a = $a / 1000;
$b = $b / 1000;
}
if ($decimal_selector == 6){
$a = $a / 1000;
$b = $b / 100;
}
if($b < $a){
$b = $b + ($a - $b) + $adjuster;
}
$c = $b - $a;
if(intval($c) == $c) {
$c_is_int = 1;
} else {
$c_is_int = 0;
}
echo $a . '<br><br>';
echo $b . '<br><br>';
echo intval($c) . '<br>';
echo $c_is_int . '<br>';
echo $c . '<br><br>';
} while($c_is_int == 1);
The attached image shows the results of one of these failing times. Any ideas on where this is going wrong?
Why not check to see if the number has a decimal?
$c = 123.456;
if(strpos((string) $c, '.') !== FALSE) {
// is decimal/float/double
}
You can also just check to see if it's an int
if(is_int($c))
You want to keep in mind that the integer 3 and float 3.0 will compare as equal when using the ==.
Consider:
$c_is_int = $c == floor( $c );
...
while( $c_is_int )

What is the difference between “=” and “=&” when assigning variables?

I am trying to figure out the difference between $a=&$b and $a=$b.
I know & make the variable to be a reference variable. But the following test gave me the same result. Can anyone explain the difference? Thanks.
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$a=5;
$b=6;
$a=$b;
echo $a; //6
First of all: you'll hardly ever need references, avoid the confusion of using them if you can.
$a=5; //assign value to a
$b=&$a; //make $b a reference to $a
$b=6; //assigning a value to $b assigns the same value to $a (as they point to the same location
echo $a; //6
$a=5; //assign a value to a
$b=$a; //set $b to the value of $a
$b=6; //set $b to another value leaves $a at it's original value
echo $a; //5
It matters more in a function when you send it in as a parameter.
For example:
<?php
function changeVariableWithReference(&$var)
{
$var += 1;
}
function changeVariableWithoutReference($var)
{
$var += 1;
}
$a = 5;
$b = 5;
changeVariableWithReference($a);
changeVariableWithoutReference($b);
print $a . ' ' . $b;
?>
The difference between $a = $b and $a =& $b is that with the former assignment operator the value is copied while with the latter reference operator the variable $a refers to the same value as $b.
You don’t see any difference when reading the value but you see it when writing the value:
$var = 123;
$copyOfVar = $var;
$referenceToVar =& $var;
$copyOfVar = 456;
echo 'var='.$var.'; copyOfVar='.$copyOfVar;
// "var=123; copyOfVar=456"
$referenceToVar = 456;
echo 'var='.$var.'; referenceToVar='.$referenceToVar;
// "var=456; referenceToVar=456"
When you use the & in an assignment, you can think of the new variable being a 'short-cut' to the original. If you don't use the &, it will be a 'copy' of the original.
$a = 5;
$b =& $a; // this $b is like a shortcut to $a. Change $b and $a will change too
$a = 5;
$b = $a; // this time, $b will be 5 - but not a shortcut to $a. Change $b and $a will still be 5.
A reference can be easily explained by simple graph. When you use copy by value ($a = $b) then something like that happens:
$a = 1000;
# $a -----> 1000
$b = $a;
# $a -----> 1000
# $b -----> 1000
# (two "pieces of memory" has been used)
But when you create a new reference to $a named $b then something like that happens:
$a = 1000;
$b =& $a;
# $a --\
# --> 1000
# $b --/
# (one "piece of memory" has been used but two different names ($a, $b) point on it)
You will get the ans, if you follow the following code (i have add some lines)
$a=5;
$b=6;
$a=&$b;
echo $a; //6
$b = 8;
echo $a; //8
$a=5;
$b=6;
$a=$b;
echo $a; //6
$b = 20;
echo $a; //6
with & simple, variable $a points the variable $b
without &, $b just copy into $a

Categories