Im learning PHP now and was given a task which I thought I could follow through, but I get an
'int(459)'
printed out on the website.
Here is the task and my attempt to solve it:
Multiply your age by the numbers of yours
you went to school and put it isnide of
variable named total.
Then minus the total by 3.
Then check, if total is greater or equal
to 12 and put the result inside of another variable.
Then use var_dump to see if its true, or false.
<?php
$age = 33;
$schoolyears = 14;
$total = $age * $schoolyears;
$total -= 3;
$total >= 12;
$newVar = $total;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
var_dump($newVar);
?>
</body>
</html>
Appreciate your answers!
Rob
UPDATE!
After editing it lloks like this and it works.
<?php
$age = 33;
$schoolyears = 14;
$total = $age * $schoolyears;
$total -= 3;
$total = $total >= 12;
$newVar = $total;
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
var_dump($newVar);
?>
</body>
</html>
It puts out :
bool(true)
This line doesn't do anything:
$total >= 12;
It produces a value, but you don't store that value anywhere. On the next line you just copy the value from $total (which is 459) to a new variable:
$newVar = $total;
It looks like you meant to combine these into this instead:
$newVar = $total >= 12;
In general, it looks like you're confusing these operators:
-=
>=
While they share the same second character, they are not related in any way. The first one subtracts the second value from the first and assigns it back to the first variable, kind of a double operation and a shorthand for:
$var1 = $var1 - $var2;
But the second one does no assigning. It literally semantically means "greater than or equal to". It performs a comparison between two values, but doesn't modify anything.
When you state $total >= 12; it makes the check but does nothing with the information.
You need to set $newVar = $total >= 12.
From your question, step by step;
$age = 33;
$schoolyears = 14;
$negative = 3;
$total = $age * $schoolyears - $negative;
if ($total >= 12) {$result = $total; $status = true;} else {$status = false;}
var_dump($status);
I got it. This has to be....
$total >= 12;
like this:
$total = $total >= 12;
Thanks, I leave this up for other people looking foir similar questions.
Related
i want to SUM number using loop in php. And here's my code
$point = 0,3;
for ($i=1;$i<=40;$i++){
$total = $point + $i;
}
echo $total;
But when I run the program, it print wrong result. It printed 40.3, and the right result is 12
The point is, I want to SUM each point until 40 times.
example : 0.3+0.3+0.3+...+0.3 (40 times)
It should be like this
$point = 0.3;
$total = 0;
for ($i=1;$i<=40;$i++){
$total = $total+$point;
}
I wrote a simple PHP scripts which should print 320 random generated ages. These ages should be generated in pairs, and the difference between the ages should be calculated. I wrote the following code, everything but the getDifference function works. If I leave the function out, 320 ages will be generated.
But if I leave the function in it:
1) Doesn't work right
2) Stays in a long loop
3) Sometimes prints out
4 ages instead of the 320.
I need some help.
<?php
//Minimum of the age range.
$min = 8;
//Maxium of the age range.
$max = 25;
//Generating the ages
for ($i=0;$i<160;$i++) {
$age1= random_float($min,$max);
$firstage= round($age1,1);
$age2= random_float($min,$max);
$secondage=round($age2,1);
$diff = getDifference ($age1, $age2);
print "Age 1: $firstage Age 2: $secondage Difference: $diff \n ";
}
//Function to generate a random float number
function random_float ($min,$max) {
return ($min+lcg_value()*(abs($max-$min)));
}
// Function to calculate the differences between the ages in years and months
function getDifference ($age1, $age2) {
$difference = abs($age2 - $age1);
$jaar = 0;
$newdifference = round($difference,1) * 12;
for ($newdifference; $newdifference >=12; $newdifference -12) {
$jaar ++; }
$month = $newdifference - 12*$jaar;
$newmonth = floor($month);
$test = "$jaar years and $newmonth months";
return $test;
}
?>
In this loop:
for ($newdifference; $newdifference >=12; $newdifference -12) {
$jaar ++;
}
Two things:
First, you don't need that first loop condition statement. The variable already exists, there's nothing to declare:
for (; $newdifference >=12; $newdifference -12) {
$jaar ++;
}
Second, you never modify $newdifference within the loop. Basically, that third loop condition statement ($newdifference -12) doesn't actually do anything. It subtracts 12, but doesn't do anything with the result of that subtraction. So if the loop begins (because the condition is true), then it will never end (because the condition will always be true). Perhaps you meant to modify the value in that last loop condition statement?:
for (; $newdifference >=12; $newdifference -= 12) {
$jaar ++;
}
It's stuck in a loop because you're not incrementing your $i inside your for loop:
Replace:
for ($newdifference; $newdifference >=12; $newdifference -12)
By:
for ($newdifference; $newdifference >=12; $newdifference -= 12)
You need to use the -= instead of the - because otherwise it's not updating the value of $newdifference, it's just returning the result of the equation.
You're not changing $newdifference at all. Your for loop should be
for($newdifference; $newdifference >= 12; $newdifference -= 12) {
This will decrement $newdifference by 12.
I'm writings a PHP script that calculates an average of a number.
EXAMPLE:
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
The return $AvgRating for this would be 2.3 (to 1 decimal place).
is it possible to say.....
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
I have tried but it does not work, I have tried google but don't know exactly what I need to look for.
This code evaluates correctly...
<?php
$rating = 7;
$votes = 3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3){
$display = 'between';
}
echo $display;
?>
Hi there is some mistakes $AvgRating retunrs 2.3 not 7.3
So you have to check with this
$rating =7; $votes=3;
$AvgRating = number_format($rating / $votes,1);
if ($AvgRating > 2 && $AvgRating < 3.0){
echo $AvgRating;
}
If your question is whether the condition inside the if statement is correct, yes it is.
However in your case, the if condition fails since the value of $AvgRating (7/3) is less than 7. So you probably might be checking whether $AvgRating is greater than 2.
I have parsed over the variable $museum from another page under the name 'm'. If a check box was ticked on the page I parsed it from then on this new receipt page it will add 5 to the cost, or add 0. Cost is then echoed. But either way it is returning 0. First time poster and PHP beginner so sorry if I got something wrong or was not specific enough. Here is the code from the receipt page.
<?php
$cost = 0;
if(isset($_GET['m']))
{
$museum = $_GET['m'];
if($museum==false){
$cost + 0;
}else{
$cost + 5;
}
}
echo $cost;
?>
You are not using the return value of the operation. Try
$cost += 5;
instead of
$cost + 5;
Try this:
if($museum==false){
$cost = $cost + 0;
}else{
$cost = $cost + 5;
}
Well, ths is a really newbie question, but couldn't find the proper keywords to get the answer.
I have a vector variable, $p.
I want to achieve this:
$n = 5;
$prev = $p[$n--];
$actual = $p[$n];
$next = $p[$n++];
The values i want should be:
$prev = 4;
$actual = 5;
$next = 6;
Instead of that, i get:
$prev = 5;
$actual = 4;
$next = 4;
I know i'm missing something, but i couldn't figure it out.
Thanks in advance
Just do the math yourself:
$prev = $p[$n - 1];
$actual = $p[$n];
$next = $p[$n + 1];
If you must use increment / decrement operators (for some strange reason), you can use:
$prev = $p[--$n];
$actual = $p[++$n];
$next = $p[++$n];
Note that your original code was failing, as jeremy points out, because increment and decrement operators modify the original value of the variable.
At the first search:
$prev = $p[$n--]
You are changing the value of n for the next operations, decreasing it by one. In
$next = $p[$n++];
You are increasing it again so n value is the same as the beginning. You should do what nickb told.
You are incrementing the same variable inline so:
$n doesn't stay as 5 when you do $n++ or $n--, you reassign the value to $n
$n-- = 4, so now $n is 4, not 5 so $n++ is 5, not 6