PHP variable is not shown as an integer - php

if($koltukk%4 == 2){
if($koltukk > 1000){
$koltukH = "B";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
else{
$koltukH = "E";
(int)$koltukR = ($koltukk / 4) + 1;//Doesnt work (int)
}
}
$koltukR = ($koltukk / 4) + 1;
I want to get the $koltukR variable as an integer but i couldn't do it (int) did not work

You need to use the (int) casting on the other side of the assignment operator:
$koltukR = (int)(($koltukk / 4) + 1);
Or, use intval() like this:
$kolturR = intval(($koltukk / 4) + 1);

$koltukR = intval(($koltukk / 4) + 1);
You should use better variable names, move the math out of the if/else since both are the same, and you shouldn't even need to cast this as an int manually.

PHP has an intval() method that turns a variable into an integer. Pass in your variable as a parameter.
intval()

<?php
if($koltukk%4 == 2)
{
if($koltukk > 1000)
{
$koltukH = "B";
$koltukR = (int)(($koltukk / 4) + 1);
} else{
$koltukH = "E";
$koltukR = (int)(($koltukk / 4) + 1);
}
}
echo $koltukR;
?>

One important note here: intval() is NOT round(). intval() is similar to floor().
I think what you really want is round():
Here's the real answer: K.I.S.S.
if($k%4 == 2){
if($k > 1000){
$H = "B"
}
else{
$H = "E";
}
}
$R = round($k / 4) + 1;
Stealing an example from http://us2.php.net/intval to illustrate:
echo number_format(8.20*100, 20), "<br />";
echo intval(8.20*100), "<br />";
echo floor(8.20*100), "<br />";
echo round(8.20*100), "<br />";
819.99999999999988631316
819
819
820

Related

Check if a variable is in POWER base + PHP

I want to check if an input variable is in POWER base.
For example :
Input : 25 ;//yes in power pow(5,2)
Input : 26 ;//no not in power
I made a code but its time taking
$var = 25;
$half = round($var / 2);
for($x = 0; $x <= $half; $x++){
for($y = 0; $y <= $half; $y++){
if(pow($x, $y) === $var){
$output = "1";
}
}
}
if(!$output){
echo "0";
}else{
echo "1";
}
You can use sqrt which will return square root of given number, then just check if the returned value is in decimal or not.
$var = 25;
$sqrt = sqrt($var);
if($sqrt !== floor( $sqrt )) {
echo 'no power';
} else {
echo 'power';
}

How I can create my own pow function using PHP?

I want to create a function in which I put two values (value and its power - Example function: multiply(3, 3) result 27). I have tried so far but failed, I have searched using Google but I have been unable to find any result because I don't know the name of this function.
What I want exactly:
3,3 => 3 x 3 x 3 = 27
4,4 => 4 x 4 x 4 x 4 = 256
What I tried:
function multiply($value,$power){
for($x = 1; $x <= $value; $x++ ){
return $c = $value * $power;
}
}
echo multiply(3,3);
The answer has already been accepted, but I had to come here and say that all answers here use a bad algorithm. There are better ones. Including very simple ones, like exponentiation by squaring that reduces the complexity from O(power) to O(log(power)).
The idea is to square the base while dividing the exponent by 2. For example
3^8 = 9^4 = 81^2 = 6561
There is a special case when the exponent is odd. In this case, you must store a separate variable to represent this factor:
2^10 = 4^5 = 16^2 * 4 = 256 * 4 = 1024
PHP isn't one of my strong skills, but the final algorithm is as simple as:
function multiply($value, $power){
$free = 1;
while ($power > 1) {
if ($power % 2 == 1)
$free *= $value;
$value *= $value;
$power >>= 1; //integer divison by 2
}
return $value*$free;
}
echo multiply(3, 3) . "\n";
echo multiply(2, 10) . "\n";
echo multiply(3, 8) . "\n";
Oopsika, couldn't have asked a more obvious question. Use the built-in function named pow (as in a lot of languages)
echo pow(3, 3);
Edit
Let's create our own function.
function raiseToPower($base,$exponent)
{
// multiply the base to itself exponent number of times
$result=1;
for($i=1;$i<=$exponent;$i++)
{
$result = $result * $base;
}
return $result;
}
function exponent($value,$power)
{
$c=1;
for($x = 1; $x <= $power; $x++ )
{
$c = $value * $c;
}
return $c;
}
If you have PHP >= 5.6 you can use the ** operator
$a ** $b Exponentiation Result of raising $a to the $b'th power.
echo 2 ** 3;
If you have PHP < 5.6 you can use pow:
number pow ( number $base , number $exp )
echo pow(2, 3);
Your own function is:
function multiply($value, $power) {
$result = 1;
for($x = 1; $x <= $power; $x++){
$result *= $value;
}
return $result;
}
echo multiply(3,3);
Read more at:
http://php.net/manual/en/language.operators.arithmetic.php
http://php.net/manual/en/function.pow.php
Just try to run this code I hope your problem will be solved.
If you defining any function then you have to call it return value.
<?php
function multiply($value,$exp)
{ $temp=1;
if($exp==0)
return $temp;
else
{
for($i=1;$i<=$exp;$i++)
$temp=$temp*$value;
return $temp;
}
}
echo multiply(5,6);
?>
echo "Enter number (will be mutiplied):".PHP_EOL;
$value = (int) readline("> ");
echo "Enter number for multiplier:".PHP_EOL;
$multiplier = (int) readline("> ");
function power(int $i, int $n):int {
$result =1;
for ($int = 1; $int < $n; $int++){
$result *= $i;
}
return $result;
}
echo power($value,$multiplier);

Undefined offset on multiple lines

I have written a small program to solve a mathematical problem. But when I run, it gives an undefined offset error on line number 9,11,13,15.
I have searched various questions, but didn't find anything useful.
What might be causing this. ?
<?php
$arr = [1,3,5,7,9,11,13,15];
$tries=0;
$answer=0;
while(($answer!=30) && ($tries!=1000))
{
$tries = $tries+1;
$num1=getRandomNumber();
$num2=getRandomNumber();
$num3=getRandomNumber();
$num4=getRandomNumber();
$num5=getRandomNumber();
if($num5 + $num4 + $num3 + $num2 + $num1 == 30)
{
$answer = 30;
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
break;
}
}
if($tries==1000)
{
echo "1000 tries completed";
}
function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
?>
In your getRandomNumber() function, you're generating an array index between 1 and 15, but your array is only 8 elements long.
To fix this, update the call to mt_rand() to support your actual array size:
$r = mt_rand(0, count($arr) - 1);
Side-note (not answer specific), string concatenation in PHP is done with the period, . and not the +:
echo $num1 + "+" + $num2 + "+" + $num3 + "+" + $num4 + "+" + $num5 + " = 30";
// should be:
echo $num1 . "+" . $num2 . "+" . $num3 . "+" . $num4 . "+" . $num5 . " = 30";
You should change line:
$r = mt_rand(1,15);
into
$r = mt_rand(0,count($arr)-1);
because your $arr in your getRandomNumber function has only 8 elements (not 16)
function getRandomNumber()
{
$arr = [1,3,5,7,9,11,13,15];
$r = mt_rand(1,15);
if(($r%2)!=0)
{
return $arr[$r];
}
}
The mt_rand function returns a number higher then the array index witch is 7. You can either extend the array and make it have 16 index or reduce the range in mt_rand function to 0-7.

PHP generate a random minus or plus percentage of a given value

I have a value, lets say its 1000.
Now I have to generate a random minus or plus percentage of 1000.
In particular I have to generate or a -20% of 1000 or a +20% of 1000 randomly.
I tried using rand() and abs() but with no success..
Is there a way in PHP to achieve the above?
A bit of basic mathematics
$number = 1000;
$below = -20;
$above = 20;
$random = mt_rand(
(integer) $number - ($number * (abs($below) / 100)),
(integer) $number + ($number * ($above / 100))
);
rand(0, 1) seems to work fine for me. Maybe you should make sure your percentage is in decimal format.
<?php
$val = 10000;
$pc = 0.2;
$result = $val * $pc;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
if(rand(0, 1)) echo $result; else echo -$result;
?>
$number = 10000;
$percent = $number*0.20;
$result = (rand(0,$percent)*(rand(0,1)*2-1));
echo $result;
Or if you want some sort of running balance type thing....
function plusminus($bank){
$percent = $bank*0.20;
$random = (rand(0,$percent)*(rand(0,1)*2-1));
return $bank + $random;
}
$new = plusminus(10000);
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
echo $new."<br>";
$new = plusminus($new);
I know this is really old now but stumbled across it looking for something similar where I needed a random sign (+ or -) so opted for a random boolean:
<?php $sign = (rand(0,1) == 1) ? '+' : '-'; ?>
Thanks to this this answer.
So I would opt for a solution like this:
<?php
// Alter these as needed
$number = 1000;
$percentage = 20;
// Calculate the change
$change_by = $number * ($percentage / 100);
// Set a boolean at random
$random_boolean = rand(0,1) == 1;
// Calculate the result where we are using plus if true or minus if false
$result = ($random_boolean) ? $number + $change_by : $number - $change_by;
// Will output either 1200 or 800 using these numbers as an example
echo $result;
?>

Store plus and minus in a variable PHP

How can I solve this problem:
if ($variable == 1) {
$math = "-";
} else {
$math = "+";
}
$numberOne = 10;
$numberTwo = 10;
$result = $numberOne $math $numberTwo;
This doesn´t work, is there any way to solve this?
This will work for your example. A subtraction is the same as adding a negative. This will be far safer than the alternative of using eval.
if ($variable == 1) {
$modifier = -1;
} else {
$modifier = 1;
}
$numberOne = 10;
$numberTwo = 10;
$result = $numberOne + ($numberTwo * $modifier);
I suppose you could use eval() -- but that would be quite a bad idea (it would not be good for performances, it's not "clean", ...)
In this kind of situation, I would generally go with a switch on the operator, and one case per possible operator.
Here, it would mean using something like this :
switch ($math) {
case '+':
$result = $numberOne + $numberTwo;
break;
case '-':
$result = $numberOne - $numberTwo;
break;
}
Which can easily be extends to other operators.
(In your specific situation, if you only have + and -, though, some calculation based on a multiplication by +1 or -1 would be faster to write)
if ($variable == 1) {
$math = -1; // subtraction
} else {
$math = 1; // addition
}
$numberOne = 10;
$numberTwo = 10;
$result = $numberOne + ($math * $numberTwo);
No love for the ternary operator?
To minify Gazler's answer a bit further:
$modifier = ($variable == 1) : -1 ? 1;
$numberOne = 10;
$numberTwo = 10;
$result = $numberOne + ($numberTwo*$modifier);
If you plan to use more complex mathematics, you can use the eval() function.

Categories