PHP code to calculate if three sides equal a triangle [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How would i go about writing the php code that would take the inputs of three form fields and calculate whether or not they result in a triangle?
Would the best way to go about be an if/else statement?

That is right, you would have to use if/ else, or at last I can not think of something else at the moment.
Here is a sample PHP code:
<?php
$a = $_GET['a'];
$b = $_GET['b'];
$c = $_GET['c'];
if (($a + $b > $c) && ($a + $c > $b) && ($b + $c > $a)) {
echo "Yes, this is a triangle!";
} else {
echo "No, this is not a triangle!";
}
To test use something like: http://localhost/TriangleChecker.php?a=4&b=6&c=8

3 side lengths a, b, and c are a triangle if a+b>c, a+c>b, and b+c>a. Here's the function you need:
/**
* Determine If Three Side Lengths Are a Triangle
* #param integer $a first length
* #param integer $b second length
* #param integer $c third lenght
* #return boolean true|false
*/
function isTriangle($a, $b, $c)
{
return (($a + $b > $c) && ($a + $c > $b) && ($c + $b > $a));
}
Example:
$a = 10;
$b = 12;
$c = 34;
$ret = isTriangle($a, $b, $c);
//var_dump(isTriangle($a, $b, $c));
if ($ret)
echo 'That\'s definitely a triangle';
else
echo 'That is not a triangle';

Related

How to multiply by 5% [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
how to make multiplication by 5%, for example like this.
200000 x 5% = 10000
So that's how it is?
<?php
$a = 200000;
$b = 5;
$x = $a % $b;
echo $x;
?>
Why not convert the percentage value to a decimal and multiple?
$a = 200000;
$b = 5;
$x = $a * ($b / 100);
echo $x;
The return value will be a float.
Although, I'm still somewhat unclear as to what you are actually asking.
Use normal calculation to multiplication by %
Code:
<?php
$a = 1000;
$b = 2000;
$x = $a * 100 / 100 * $b;
echo $x;
?>

How to use a sign plus in a PHP variable and get result? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to store a plus sign (or any other sign) in a PHP variable and make arithmetic operation.
I tried to store sign plus in variable $c. But PHP it's not accepting it.
$a = 3;
$b = 5;
$с = "+";
But when I echo it,
echo $a. $c. $b;
it gives me the result 35.
I tried enothoer one way, but it doesn't work:
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
The result is -2.
How get result $a + $b?
You're using a combination of the Cyrillic and latin letter c in your code.
If I paste your code on this site to show each unicode char, the first c is an \u0441 (Cyrillic), how ever, the c inside the if shows 0x63 (latin), so PHP is unable to match those and trows:
PHP Notice: Undefined variable: c
Fixed:
<?php
$a = 3;
$b = 5;
$c = "+";
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
Try it online!

how to make a series of number jumps based on the numbers of variables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I want to make row of number jumps, example I have $a = 1 and $b = 2. then I want loop $a and $b and the result is $a = '1', '5', '9', '13', '17' and $b = '2', '6', '10', '14', '18' . How to make it?
You might want to add some context as to me it looks like a generic PHP question, but you also added the codeigniter tag which looks like there is more to it than you wrote.
If I understand you correctly, you start with two numbers and want to generate an list (array) for each of those numbers. Reusing $a and $b for the results might not be a good idea. So I suggest you save the result to a different variable:
// initialize A
$a=1;
$a_result=[];
// initialize B
$b=2;
$b_result=[];
// set the jump size
$jump_size=4;
// loop 5 jumps
for($i=0; $i<5; $i++) {
// add new elements to the result arrays
$a_result[] = $a + $jump_size * $i;
$b_result[] = $b + $jump_size * $i;
}
// print the results
print_r($a_result);
print_r($b_result);
If you have more than just two numbers to start from, you might want to put those in an array too (as well as the result arrays).
You can check odd or even number using modulo, and skip next number.
See below example.
See Running Example
$a = 1;
$b = 2;
for($i = $a; $i <= 20; $i++){
if($i > 1 && $i % 2){
$i = $i + 2;
}
if($i % 2 == 1) {
echo $i;
}
}
echo " b=> ";
for($i = $b; $i <= 20; $i++){
if($i > 1 && $i % 2){
$i = $i + 2;
}
if($i % 2 == 0) {
echo $i;
}
}

PHP code is returning "x=NAN" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My code is:
$a = $_GET["valA"];
$b = $_GET["valB"];
$c = $_GET["valC"];
$d = pow($b,2) - 4 * $a * $c;
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
echo "x = $h";
And it is returning:
x = NAN
Please point out my mistake.
Issue is with $e = sqrt($d);
Whenever $d gives a NEGATIVE value sqrt($d) returns NAN.
EXAMPLE
echo $e = sqrt(-4);
returns NAN
AND
echo $e = sqrt(4);
returns 2
NAN simply means Not A Number. The values you are pulling from the URL may be strings, which would cause this issue.
Also I think the problem may occur if you are taking the square root of a negative number.
You could use something like this to solve it
<?php
$a = 10;
$b = 20;
$c = 30;
$d = pow($b,2) - 4 * $a * $c;
if($d < 0){
$d *= -1;
}
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
?>

PHP Output (function swap) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone try to explain me why the output of this code is 2 , 12 ; 5 , 25 ?
<?php
function swap($x, $y){
$x = $x + 1;
$y = $y + 2;
return $x * $y;
}
$a = 2;
$b = swap($a, $a);
print "$a, $b";
$b = swap(&$a, &$a);
print "$a, $b";
?>
Output:
2, 12
5, 25
Here is some explanation. The first call I think you have understood. I will explain the result of the second call in the code. Here you are passing the variable by reference.
when you call by reference the reference to the variable is passed so any change gets reflected in the original variable.
<?php
function swap($x, $y){
//here your variable $a is 2
$x = $x + 1;
// added 1 to $a so $a is now 3
$y = $y + 2;
//again added 2 to variable $a becomes 5
return $x * $y; //returns 5*5=25
}
$a = 2;
$b = swap(&$a, &$a);
print "$a, $b";
?>
Hope this explanation helps.

Categories