Hello I have problem with PHP if else at echo part
where my echo didn't count my php result with * instead its calculate normally with variable $tot_gaji
<td align="right">
<?php
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000) {
$tot_gaji+0.05*$tot_gaji;
} elseif ($tot_gaji>=5000000) {
$tot_gaji+0.15*$tot_gaji;
}
echo number_format($tot_gaji,0,".",",");
?>
</td>
If you don't assign the calculated results to the variable, it be still unchanged. So you have to assign the calculated result to a variable
<td align="right">
<?php
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000) {
$tot_gaji = $tot_gaji+0.05*$tot_gaji;
// ^^^^^^^^^^^
} elseif ($tot_gaji>=5000000) {
$tot_gaji = $tot_gaji+0.15*$tot_gaji;
// ^^^^^^^^^^^
}
echo number_format($tot_gaji,0,".",",");
?>
</td>
You are not assigning the mathematics expression to any variable that's why the values of variable $tot_gai stay original
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji>=4500000)
{
$final = $tot_gaji+0.05*$tot_gaji;
}
elseif ($tot_gaji>=5000000)
{
$final = $tot_gaji+0.15*$tot_gaji;
}
echo number_format($final,0,".",",");
You have to assign the calculated values to $tot_gaji
$tot_gaji = $row['gajipokok']+$rows['jlhbonus'];
if ($tot_gaji >= 4500000) {
$tot_gaji = $tot_gaji+0.05*$tot_gaji;
} elseif ($tot_gaji>=5000000) {
$tot_gaji = $tot_gaji+0.15*$tot_gaji;
}
echo number_format($tot_gaji,0,".",",");
Other than not assigning your adjusted figure your second condition may not be met. i.e. a number such as 6000000 is greater than 4500000, it is also greater than 5000000, under your logic the first condition is met.
You likely want to switch your conditions or test for a range.
<?php
function adjustment($num)
{
if($num > 10) {
$num += 0.25 * $num;
} else if ($num > 5) {
$num += 0.5 * $num;
}
return $num;
}
foreach([1,6,12] as $num) {
echo adjustment($num), "\n";
}
Output:
1
9
15
Related
Here is my Code
<?php
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = get_rows($getvids);
if($offline == "Offline")
{
$apsys = "APSYS";
$offlined = "";
if ($getvids == $apsys || $getvids == $offlined )
{
echo "<tr>
<th>$x</th>
<th>$accntid[$x]</th>
<th>$fname[$x]</th>
<th>$lname[$x]</th>
<th>$vid[$x]</th>
<th>$vplatenum[$x]</th>
<th>$imei[$x]</th>
<th>$datas[1]</th>
<th>$datas[0]</th>
<th>$offline</th>";
echo "<th>$getvids</th>";
echo "<th><button type = 'button' class = 'btn btn-success viewbtn'> View Troubleshoot Report History</button></th>";
echo "</tr> ";
echo "total number of Rows:";
echo str_word_count($countrow);
}
else
{
}
}
else
{
}
}
?>
and here is my Function
<?php
function get_rows($getrowscount)
{
require ('db_connection.php');
$getrowscounts = $getrowscount;
//echo $getrowscounts;
return $getrowscounts;
}
//get_rows(); // call the function
?>
the table shows Two Rows with the value of = "APSYS"
I want to count the rows and show the output like this:
Total number of Rows: 2
but the output I can only get is like This:
Total number of Rows:1
Total number of Rows:1
Total number of Rows:0
can someone help me, I am a little bit confused about what will I use to count the rows?
Thanks.
How about this? Does this satifsfy your requirement?
<?php
$countrow = 0;
for ($x = 0; $x <= $n; $x++)
{
//Counting of rows
$countrow = $countrow + get_rows($getvids);
if($offline == "Offline")
{
//omitted
}
else
{
}
}
}
echo "total number of Rows:";
echo str_word_count($countrow);
?>
If you are trying to count them after fetching from the database, try to write a SQL query instead of your current approach.
I am trying to make a simple while loop using a class to get the factorial of a number. However, for some reason, the while loop is only returning the value after it has run once.
Here is my code:
<?php
class Factorial {
public function calculate($int){
$intStep = $int-1;
do {
$int*=$intStep;
$int--;
return $int;
} while($int>0);
if (!is_int($int)){
echo "entry is not a number";
}
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
I see a number of problems with your code:
return $int; is run inside the do while loop, which means it'll only ever run once.
You're decrementing $int instead of $intStep
You're checking if $int is below zero instead of $intStep
Here's your code with all of these problems fixed, it works and returns 15:
class Factorial {
public function calculate ($int) {
if (!is_int($int)) {
echo "entry is not a number";
}
$intStep = $int - 1;
do {
$int += $intStep;
$intStep--;
} while ($intStep > 0);
return $int;
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
3v4l.org demo
You shouldn't return from your function until your result is ready. Since you return early, you won't finish your calculation.
In general, your life will be easier if you learn how to debug. For PHP, the easiest way would be to echo stuff throughout your code. If you put echo $int inside of your loop it would be more obvious to you what the issue was.
try this
<?php
class Factorial {
public function calculate($num){
$Factorial = 1;
$i =1;
do{
$Factorial *= $i;
$i++;
}while($i<=$num);
return $Factorial;
}
}
$factorial = new Factorial;
echo $factorial->calculate(5);
?>
Factorial ? Maybe the following code is what you want:
Don't forget negative Numbers.
class Factorial {
public function calculate ($int) {
if (!is_int($int)) {
echo "entry is not a number";
}
if ($int == 0 || $int == 1) {
$result = 1;
} else if ($int < 0) {
$result = $this->calculate($int + 1) * $int;
} else {
$result = $this->calculate($int - 1) * $int;
}
return $result;
}
}
$factorial = new Factorial;
echo $factorial->calculate(-4);
so the task I'm trying to complete here is comparing a user input number against the fibonacci sequence and if it is a fibonacci number the program will echo true, if not it will echo false.
Can someone tell me where I'm going wrong?
this is my first file:
<?php
function print_fibonacci($n){
$first = 0;
$second = 1;
echo "Fibonacci Series: \n";
echo $first.' '.$second.' ';
for($i=2;$i<$n;$i++){
$third = $first + $second;
echo $third.' ';
$first = $second;
$second = $third;
}
}
/* Function call to print Fibonacci series upto 6 numbers. */
print_fibonacci(16);
?>
<form method="POST" action="fibonacci3.php"><br>
<label>Input a number to check if it is fibonacci or not.</label><br>
<input name="fib" type="text" placeholder="#" /><br>
<input type="submit" value="OK!" />
</form>
This outputs the fibonacci sequence until the 16th fibonacci number, and is followed by a form which allows a user to enter a number.
The next file is fibonacci3.php as referenced in the form.
<?php
include "fibonacci2.php";
$n = $_POST["fib"];
function fibonacci($n) {
//0, 1, 1, 2, 3, 5, 8, 13, 21
/*this is an error condition
returning -1 is arbitrary - we could
return anything we want for this
error condition:
*/
if((int)$n <0){
return -1;
echo "False";
}
if ((int)$n == 0){
return 0;
echo "0";
}
if((int)$n == 1 || $n == 2){
return 1;
}
$int1 = 1;
$int2 = 1;
$fib = 0;
for($i=1; $i<=$n-2; $i++ )
{
$fib = $int1 + $int2;
//swap the values out:
$int2 = $int1;
$int1 = $fib;
}
if ($fib = $int1 + $int2 && $n == $fib){
echo "True!";
} else {
echo "False!";
}
return $fib;
}
fibonacci((int)$n);
?>
I thought this might be correct but it's not outputting anything when the user inputs a number.
$n = 'Your number';
$dRes1 = sqrt((5*pow($n, 2))-4);
$nRes1 = (int)$dRes1;
$dDecPoint1 = $dRes1 - $nRes1;
$dRes2 = sqrt((5*pow($n, 2))+4);
$nRes2 = (int)$dRes2;
$dDecPoint2 = $dRes2 - $nRes2;
if( !$dDecPoint1 || !$dDecPoint2 )
{
echo 'True';
}
else {
echo 'False';
}
How to execute php if condition once time only and didn't check it again, i put if condition in for loop and i want to check it one time only:
<?php
for($x=0;$x<3;$x++){
if($x == $x){
echo "";
}
else{
echo "hidden";
}
}
I need to execute first if one time only in for loop
I need to execute first if one time only in for loop. Well that's not how you do it, you need to compare with a variable and upon success just use break to come out of the loop. Something like:
<?php
$y = 0;
for($x=0; $x<3; $x++){
if($x === $y){
echo "X is Equal to Y";
break;
}
else {echo "hidden";}
}
echo "<br>". "You're outside the loop!";
?>
Set a flag, if flag is true don't execute if condition.
<?php
$flag = 0;
for($x=0;$x<3;$x++) {
if($x == $x && $flag == 0){
echo "";
$flag = 1;
}
else{
echo "hidden";
}
}
Any php random function which gives number of random numbers specified and with in the boundaries defined.
For example numbers from 1 to 5000
And I need 5 random numbers.
I know there is function which work for array indexes array_rand
I need similar for numbers and boundaries defines. At least the upper boundry defined.
Here I used mt_rand instead rand (much faster):
function randomNumbers($min, $max, $count = 1) {
$randomFunc = function () use ($min, $max) { return mt_rand($min, $max); };
return array_map($randomFunc, range(1, $count));
}
Why not make a function?
function randomArray($count, $min, $max){
$randomArray = array();
for ($i = 0; $i < $count; $i++) {
$randomArray[] = rand($min, $max);
}
return $randomArray;
}
Your case would be:
$randoms = randomArray(5, 1, 5000);
You can try this
<?php
function random_generator($digits)
{
srand((double) microtime() * 10000000);
//Array of alphabets or numeric ,you can define
$input = array ("0", "1", "2", "3","4");
$random_generator="";// Initialize the string to store random numbers
for($i=1;$i<$digits+1;$i++)
{ // Loop the number of times of required digits
if(rand(1,2) == 1){// to decide the digit should be numeric or alphabet
// Add one random alphabet
$rand_index = array_rand($input);
$random_generator .=$input[$rand_index]; // One char is added
}
else
{
// Add one numeric digit between 1 and 10
$random_generator .=rand(1,10); // one number is added
}
// end of if else
}
// end of for loop
return $random_generator;
}
echo random_generator(5);
?>
Will return unique random number, you have pass 3 value to function: Min, Max and Count (number of random value)
function rand5($min,$max,$count){
$num = array();
for($i=0 ;i<$count;i++){
if(!in_array(mt_rand($min,$max),$num)) {
$num[]=mt_rand($min,$max);
}
else {
$i--;
}
}
return $num;
}
Hope will help!
use of define may be alternative in PHP
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid red;
}
</style>
</head>
<body bgcolor="gold">
<table style="width:25%">
<?php
// code by Bhupinder Deol
define('MIN', 1);
define('MAX', 49);
define('ROWS', 10);
define('NUMS', 6);
for($y=0;$y <ROWS;$y++){ // for loop started for $y
for($x=0;$x<NUMS;$x++) {
$a[$x]=rand(MIN,MAX);
}
asort($a);
$a=array_unique($a);
if (count($a) == NUMS)
{
// print_r($a);
echo "<tr>";
foreach ($a as $value) {
echo "<td>$value</td>";
// echo $value." ";
}
echo "</tr>";
`enter code here` $x=0;
}
else
{
--$y;
$x=0;
}
} // for loop ended for $y
?>
</table>
<h1> Total lottery lines created = <?php echo ROWS." for ". NUMS. "/" .MAX; ?>