Array == double equal, - php

I got an array implode variable, $varString, that is setup to return 3 separate values listed below depending on the condition.
1
2
1,2
If ($varString == 1)
{
echo 'APPLE';}
ElseIf ($varString == 2)
{
echo 'BANANA';}
ElseIf ($varString == 1,2) //throws an error.
{
echo 'APPLE and BANANA';}
How do I do this for the case of 1,2?
I tried
ElseIf ($varString == '1,2') //throws an error.
{
echo 'APPLE and BANANA';}
ElseIf ($varString == "1,2") //throws an error.
{
echo 'APPLE and BANANA';}

As 1,2 could only be understood by PHP as a string, you should change your script to this:
If ($varString == '1')
{
echo 'APPLE';
}
ElseIf ($varString == '2')
{
echo 'BANANA';
}
ElseIf ($varString == '1,2') //no it doesn't
{
echo 'APPLE and BANANA';
}
and also, string should always be in ''

ElseIf ($varString == '1,2') { echo 'APPLE and BANANA';}

Related

How to Exit If Statement and Run Another IF Statement?

$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}

multiple IF statements in a function - OOP

im wanting to check whether a inputted triangle is either a isosceles, equal etc.. the first if statement works, but once i introduce the second one it says unexpected t_else, here is the code...
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
any ideas?
Maybe this is even better? ( Based on the example from Vlad Preda:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
switch ($uniqueSides) {
case 1:
return "Equilateral";
break;
case 2:
return "Isosceles";
break;
default:
return "Normal triangle";
break;
}
}
You have a syntax error:
public function typeOfTriangle()
{
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
}
Need a brace }
if ($this->sideOneLength == $this->sideTwoLength && $this->sideTwoLength == $this->baseLength) {
echo "<h1>Equilateral Triangle</h1>";
}
else if ($this->sideOneLength == $this->sideTwoLength OR $this->sideOneLength == $this->baseLength OR $this->sideTwoLength == $this->baseLength) {
echo "<h1>Isosceles Triangle</h1>";
}
I would suggest a slightly different, more readable approach:
public function getSides()
{
return array($this->sideOneLength, $this->sideTwoLength, $this->sideThreeLength);
}
public function typeOfTriangle()
{
$uniqueSides = count(array_unique($this->getSides()));
if ($uniqueSides == 1) {
return "Equilateral";
} elseif ($uniqueSides == 2) {
return "Isosceles";
} else {
return "Normal triangle";
}
}

PHP If else if not working correctly

When I click y link,it is going to x.Why is that ?
x
y
<?php
if(isset($_REQUEST['hello']) == 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) == 'y'){
echo 'y';
}
else
{
echo "else";
}
try
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) == 'x') )
The isset function returns either true or false and you are comparing that return value with strings 'x' and 'y'.
Since you are using == and not ===, true == 'x' will return ture.
To fix this first you need to check if the variable is set and only then compare it.
if(isset($_REQUEST['hello']) && ($_REQUEST['hello']) === 'x'))
isset returns true or false, in both of these examples hello is set to something, so isset would return true (which does not equate to either x or y)
Hopefully this helps.
<?php
if(isset($_REQUEST['hello']) && $_REQUEST['hello']== 'x')
{
echo 'x';
}
else if(isset($_REQUEST['hello']) && $_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
?>
isset will check whether the request is set or not returns 0 or 1
x
y
<?php
if($_REQUEST['hello'] == 'x')
{
echo 'x';
}
else if($_REQUEST['hello'] == 'y'){
echo 'y';
}
else
{
echo "else";
}
<?php $myvar = $_REQUEST['hello'];
if($myvar == 'x')
{
echo 'x';
}
else if($myvar == 'y')
{
echo 'y';
}
else
{
echo 'else';
}
?>
Try this one

PHP if/else statement issue

I'm having some issues with this statement,
<?php
$cert_mess = $vehicle['make'];
if ($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif (!in_array($vehicle['make'], array('Cadillac','Honda') )) {
$cert_mess = "DFWCertAutos";
}
?>
<div style="font-size:10px; padding:10px; padding-top: 0px;">*This car is <?php
echo $cert_mess ?> certified.</div>
Any suggestions? currently it just displays $cert_mess as 'make' and ignores the if / else if statements.
A simpler code can be the following:
$cert_mess = $vehicle['make'];
if (($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")
|| ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0")
|| !in_array($vehicle['make'], array('Cadillac','Honda'))
) {
$cert_mess = "DFWCertAutos";
}
Simpler still:
$cert_mess = $vehicle['make'];
if (!in_array($vehicle['make'], array('Cadillac', 'Honda')) || $vehicle['certified'] == '0')
{
$cert_mess = 'DFWCertAutos';
}

PHP quiz script answer highlight

I have a PHP script and MSSQL tables, I got the answer key in a variable stored in $right_answer and user selected answers in $user_answer_select THe format is something like this
5+10?
A) 10
B) 15
C) 20
D) 25
E) 50
Answer key: B
what I want to do is put a check mark next to B if its correct and a X if it is wrong, how would I make the if else statements here?
This is the code I currently have
if(($user_answer_select == $right_answer) && $user_answer_select == 'a') $a_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'b') $b_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'c') $c_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'd') $d_sel = "<img src=\"tick_icon.gif\">";
else if(($user_answer_select == $right_answer) && $user_answer_select == 'e') $e_sel = "<img src=\"tick_icon.gif\">";
This is wrong because some of the questions that don't have answers for are highlighted as true. What's the way to do this?
$answers = array ( "A"=>10, "B"=>15, "C"=>20, "D"=>25, "E"=>50 );
$right_answer = "B";
$user_selected_answer = "A";
echo "5+10?<br/>";
foreach ($answers as $key => $value) {
echo $key.") ".$value;
if ($value === $user_selected_answer) {
if ($value === $right_answer){ echo "check!"; }
else { echo "X"; }
}
echo "<br/>";
}
echo "Answer key: $right_answer";
if ( $user_answer_select == $right_answer ) {
$correct = true;
} else {
$correct = false;
}
Then in the correct answer on the form:
<?php echo $correct == true ? 'x' : ''; ?>

Categories