PHP giving same output for different inputs [duplicate] - php

This question already has answers here:
The 3 different equals
(5 answers)
PHP if single or double equals
(3 answers)
Closed 2 years ago.
I am trying to create a simple demonstration of web services where I retrieve price of book. But here when I try different book names it gives price of 1st book entered in array.
<?php
function get_price($find){
$books=array(
"c"=>20,
"java"=>30,
"php"=>40
);
foreach ($books as $book=>$price) {
if ($book=$find) {
echo $book;
echo $price;
return $price;
break;
}
}
}
?>
Here, if I enter 'java' it gives price of 'c'.
<?php
//process client req
header("Content-Type:application/json");
include("functions.php");
if (!empty($_GET['name'])) {
# code...
$name=$_GET['name'];
$price=get_price($name);
if(empty($price))
deliver_response(200,"Book not Found",NULL);
else
deliver_response(200,"Book Found",$price);
}
else
{
//throw error
deliver_response(400,"Invalid",NULL);
}
function deliver_response($status,$status_message,$data){
header("HTTP/1.1 $status $status_message");
$response['status']=$status;
$response['status_message']=$status_message;
$response['data']=$data;
$json_response=json_encode($response);
echo $json_response;
}
?>
Can anyone help??

= is the assignment operator. If you want to check for equality, you should use the == operator (or better yet, === to avoid type juggling shenanigans):
foreach ($books as $book=>$price) {
if ($book == $find) {
// Here^
echo $book;
echo $price;
return $price;
break;
}
}

please change the line
if ($book=$find) {
to
if ($book==$find) {

Change if ($book=$find) to if ($book == $find). You're using the assignment operator (=) instead of a comparison operator (== or ===).

Related

PHP - IF doing all the time only first condition, why? [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 5 years ago.
why the IF(the lastest one with else if and else) is doing all the time only first condition and only the first part ($filtry_1value[$key] = 'min_cena'), even if the condition shouldnt be true. I have another solution (less dynamic), if I will not fix this one, but I would like to know, why it is not working... I think it will be a trivial thing, but I cannot see it.
PS: I am working with laravel.
$filtry_1value = ['stat', 'lokalita', 'patro', 'min_cena', 'max_cena', 'min_uzitna_plocha', 'max_uzitna_plocha'];
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] = 'min_cena' OR $filtry_1value[$key] = 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] = 'max_cena' OR $filtry_1value[$key] = 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
may be-
foreach ($filtry_1value as $key => $filtr_1value) {
$filtr_1value = \Request::has($filtr_1value) ? \Request::get($filtr_1value) : null;
if(!empty($filtr_1value)){
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'>=',$filtr_1value);
}
elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
$query->where(substr($filtry_1value[$key], 4),'<=',$filtr_1value);
}
else {
$query->where($filtry_1value[$key],'=', $filtr_1value);
}
}
}
You need to use the double equal sign for comparisons. == not a single =
Your if's should look like:-
if ($filtry_1value[$key] == 'min_cena' OR $filtry_1value[$key] == 'min_uzitna_plocha') {
// ...
} elseif ($filtry_1value[$key] == 'max_cena' OR $filtry_1value[$key] == 'max_uzitna_plocha') {
// ...
}

If statement working only until 1st else if [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I am trying to show a different navigation bar depending on a users authority. Only problem is that when i log on to the system it shows the first else if, regardless of the authority of the user. To ensure that the problem is in the loop i have tried switching the else ifs and the same happened. the code is in an external php file and i call the function in the top of each page. any suggestions ?
function checkAuth() {
session_start();
if(!isset($_SESSION['role'])) {
require_once('menu.php');
} else if ($_SESSION['role'] = "registered") {
require_once('regnav.php');
} else if ($_SESSION['role'] = "admin") {
echo "FDGFGFD";
require_once('adminnav.php');
}
}
Your issue is with this part: $_SESSION['role'] = "registered". The single = means you are assigning the value "registered" to variable $_SESSION['role'].
If you are evaluating to check something, you need to use == i.e. $_SESSION['role'] == "registered"
You'll have the same issue with the second elseif
You need to use a double = sign for any condition check. For any condition check in if or else if, you have to use == in the middle of the variables.
If you use only = that means it assigning the value in the $_SESSION['role']. Also you can use === for checking the value as well as the type of the variable.
Valid function is:
function checkAuth()
{
session_start();
if(!isset($_SESSION['role']))
{
require_once('menu.php');
}
else if ($_SESSION['role'] == "registered"){
require_once('regnav.php');
}
else if ($_SESSION['role'] == "admin"){
echo "FDGFGFD";
require_once('adminnav.php');
}
}
?>

PHP - $x=0 -- Why is it passing off as being equal to 1? [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
I have a few lines of code
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case = 1)
{
$message['a']="co";
}
if ($case = 0)
{
$message['a']="to";
}
echo $message['a'];
It will echo "co". Why is this? The file_put contents puts "0". However the if statement thinks it is 1 for some reason...
You are doing wrong in if condition. You doing assign instead of comparison. So here is the solution.
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case == 1)
{
$message['a']="co";
}
if ($case == 0)
{
$message['a']="to";
}
echo $message['a'];
you have to use the comparison operator "==" when comparing values: otherwise you are assigning values (in this case you were assigning $case to be 1 and then the message was "co".
$case=0;
file_put_contents("text.txt", $case, FILE_APPEND);
if ($case == 1)
{
$message['a']="co";
}
if ($case == 0)
{
$message['a']="to";
}
echo $message['a'];

How can I use if else if in this type of if else($var==2)? '' : '' [duplicate]

This question already has answers here:
Using nested ternary operators [duplicate]
(7 answers)
Closed 7 years ago.
Can I use multiple if else in this format?
$data->frm_status==1? "Incomplete" :"other"
I have tried with different ways . like this
$data->frm_status==1? "Incomplete" $data->frm_status==2 ?"Analysis Done":"other"
$data->frm_status==1? "Incomplete" ? $data->frm_status==2 ?"Analysis Done":"other"
This is the error. I am unable to search this on google because I don't know the name for this syntax.
although i know i can use this
if()
{
//
}else if()
{
//
}else
{
//
}
Ternary operator isn't good choice for multiple (else)ifs:
echo $data->frm_status == 1 ? "Incomplete" : ($data->frm_status == 2 ? "Analysis Done" : "other");
The better approach is:
if ($data->frm_status == 1) {
echo 'Incomplete';
} elseif ($data->frm_status == 2) {
echo 'Analysis Done';
} else {
echo 'other';
}
OR switch:
switch ($data->frm_status) {
case 1:
'Incomplete';
break;
case 2:
'Analysis Done';
break;
default:
'Other';
break;
}

IF statement newbie [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 8 years ago.
<?php
if ($user->getProfile()->get('title')="Canon"); {
echo "Test1"; }
else {
echo "Test2"; }
?>
This is causing my site to break, is there an obvious mistake?
Thank you.
= is an assignment, you want == for a comparison.
You also shouldn't have a ; between ) and {.
This will work:
<?php
if ( ($user->getProfile()->get('title')) == "Canon" ){
echo "Test1";
}
else {
echo "Test2";
}
?>
First of all You need a comparison '==' and not an assigning '='
And then You have a syntax error with a ';' after the condition
You need to change the = sign to == as the first one assigns a values while the latter one compares it.
And also, you don't need to terminate the if statement with a semicolon
if ($user->getProfile()->get('title') == "Canon") /* note that there is no semicolon here */
{ echo "Test1"; }
else
{ echo "Test2"; }
<?php
if ($user->getProfile()->get('title')=="Canon") {
echo "Test1";
}
else {
echo "Test2";
}
?>
Try this.

Categories