multiply number by itself PHP [duplicate] - php

This question already has answers here:
PHP: pass a variable to a function, do something to the variable, return it back
(5 answers)
Closed 3 years ago.
How come the output of these two functions are not the same? Please enlighten me on this one.
first code:
function multiply_itself(&$number) {
$number *= $number;
return $number;
}
$my_num = 10;
echo "$my_num" . "<br>"; //Output 10
multiply_itself($my_num);
echo "$my_num"; //Outputs 100
second code:
function doubled($integer) {
$integer *= $integer;
return $integer;
}
$integer = 20;
doubled($integer);
echo "$integer"; //Outputs 20, why not 400?

The second example outputs 20 because the parameter is not passed by reference like in the first example.
Change the function signature to function doubled(&$integer) or use its return value $integer = doubled($integer); and it prints 400.

Related

Unable to understand the working of PHP recursive function [duplicate]

This question already has answers here:
Understanding recursion [closed]
(20 answers)
Closed 4 years ago.
I find it difficult to understand the working of this recursive function in PHP. I am not able to follow the code written at return statement. How the total of numbers from 1 to 10 is added? I eagerly want to understand this line return $count + sum($count + 1);
My whole code is:
<?php
function sum($count)
{
if($count <= 10)
{
echo $count;
echo "<br />";
return $count + sum($count + 1);
}
}
$result = sum(1);
echo "The total is $result";
?>
Output:
1
2
3
4
5
6
7
8
9
10
The total is 55
How the total 55 is received in my code? I want to learn it step by step.
Define the function
function sum($count)
{
Check if $count <=10 so the function will not excuted if
$count > 10
if($count <= 10)
{
Print $count followed by a line break
echo $count;
echo "<br />";
Return $count + the result of the same function for $count+1 this which make the function work until reach the if condition $count<=10
return $count + sum($count + 1);
}
}
Note that while you pass 1 as a parameter and the function call itself within it so it will be continues until it reaches 10
$result = sum(1);
echo "The total is $result";
?>

after calculating percentage in php i got 87.555555 i wanna fixed it at 87.55 [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 5 years ago.
<?php
var $a = 85;
var $b = 7;
var $c = $a/$b;
echo $c;
?>
Answer is 12.14285714285714
I want to fixed it as 12.14. How can we set two digits after decimal in PHP?
PHP doc : PHP Doc to round numbers
In your example :
var $b = 7;
var $c = $a/$b;
echo round($c, 2);
Hope it helps
You can use number_format:
number_format($c, 2)

How to calculate (+ or - or * or /) using php variable? [duplicate]

This question already has answers here:
Shunting Yard implementation in PHP needed, interpret and parse a string perform a mathematical comparison and return a boolean result
(4 answers)
Closed 7 years ago.
Why this 2 code not echo same result ?
On first code echo *57, But i want to echo 35 like on second code.
How can i do that ?
<?PHP
$fomular = "*5";
$x = "7";
$res = $fomular."".$x;
echo $res;
?>
<?PHP
echo 5*7;
?>
There are a couple of issues.
In your example
...
$res = $fomular."".$x;
...
Is the same as saying
...
$res = "*5".""."3";
...
So that explains the "*53" been echoed/
...
$res = $fomular + $x;
// or
$res = $x + $fomular;
...
Will return 12 as it will not evaluate the "*" but rather add the numbers.
You could use eval( ... ) but it is evil and would really advise against it.
...
echo eval( "5*" . "7" . ";");
...
So your best bet, if you really want to echo the result, would be to write your own function to parse the 2 strings and do the operation.
That is because when you use .(dot) operator it appends the two variables.
You have to use code like below to get the desired output
$formular = 5;
$x=7;
$res=$formular * $x;
echo $res;
$formular = 5;
$x = 7;
$res = $formular * $x;
echo $res;
. (dot) in php is a string concatenating operator! if you try to use it with any type of variables, it converts they to string and put together. Sample: $x = 5 . 7 -> $x = '57'; but, if you use any kind of mathematicals operators (*, /, +, -), php will convert both operands to numeric form (float or int), then execute your formula and give a result $x = 5 * 7; -> $x = 35;
In short: use only mathematicals operators for executing formulas, not a dot. (sory for my bad english)

PHP - Call a function with no return that does actually change values [duplicate]

This question already has answers here:
Changing a global variable from inside a function PHP
(4 answers)
Closed 7 years ago.
I want to call a function in PHP, that changes existing variables without returning a specific one.
Here an example:
<?php
$number1 = 5;
$number2 = 3;
echo $number1;
echo $number2; //shows the unmodified numbers
modifyNumbers($number1, $number2); // Modifies the Numbers
echo $number1;
echo $number2; //shows the modified numbers
?>
<!-- Stuff -->
<?php
function modifyNumbers($number1, $number2) {
/* Doing math stuff with the numbers */
/* No return because many numbers were changed / overwritten
}
?>
Basically, I want to make a function that is just overwriting variables instead of giving a specific value back to a specific variable where the function is called.
Thanks in advance!
pass by reference with &
<?php
$number1 = 5;
$number2 = 3;
echo $number1;
echo $number2; //shows the unmodified numbers
modifyNumbers($number1, $number2); // Modifies the Numbers
echo $number1;
echo $number2; //shows the modified numbers
?>
<!-- Stuff -->
<?php
function modifyNumbers(&$number1, &$number2) {
$number1++;
$number2--;
}
?>
Precede the arguments with an ampersand.
function modifyNumbers(&$number1, &$number2) {
/* Doing math stuff with the numbers */
/* No return because many numbers were changed / overwritten
}
This way, a reference is created and everything you do with the variable will affect the variable 'outside' the function.

variables float no equals in php why? [duplicate]

This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same

Categories