PHP syntax error, unexpected 'elseif' [duplicate] - php

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
I get this error:
Parse error: syntax error, unexpected 'elseif' (T_ELSEIF) in C:\...\functions.php on line 6
This is the whole content of functions.php:
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs") echo $cs;​
elseif ($lang=="​sk") echo $​sk;​
elseif ($lang=="​en") echo $​en;​​ // line 7
}
?>
What's wrong? This seams like such a basic thing!

You're just missing a space: it's else if, not elseif.

You need to use {'code here'} after an if/elseif/else statement. The correct way to go about this would be:
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs") {
echo $cs;​
} elseif ($lang=="​sk") {
echo $​sk;​
} elseif ($lang=="​en") {
echo $​en;​​
}
}
?>

And it is simple. You just need to correct syntax for if/elseif
<?php
// prints the suitable string based on the chosen language
function lecho ($cs,$sk,$en) {
global $lang;
if ($lang=="cs")
{
echo $cs;​
}
elseif ($lang=="​sk")
{
echo $​sk;​
}
elseif ($lang=="​en")
{
echo $​en;​​ // line 7
}
}
?>

Related

PHP giving same output for different inputs [duplicate]

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 ===).

PHP Error when check if URL Query = equals to [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I get an error when I try to check if a query taken from the url equals something.
Error: syntax error, unexpected T_IS_EQUAL, expecting ',' or ')'
if (isset($_GET['lang'] == 'eng')) {
echo 'ENG';
}else if (isset($_GET['lang'] == 'alb')) {
echo 'ALB';
}else {
echo 'MKD';
}
Am I doing something wrong?
Thanks
You can not use == and isset to gether:
if (isset($_GET['lang']) && $_GET['lang']=='eng') {
echo 'ENG';
}
else if (isset($_GET['lang']) && $_GET['lang'] == 'alb') {
echo 'ALB';
}else {
echo 'MKD';
}

simple php if statement generates parse error [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 7 years ago.
I've got this code
if ( !empty( $ImStoreCart->cart->items ) {
$count = $ImStoreCart->cart->items;
echo "Items in Basket: ". $count;
} else { echo "Your shopping basket is empty."; }
but I get this error: Parse error: syntax error, unexpected '{'
I've checked the code but cannot see why its failing to see the if statement that needs the {
Any ideas?
It was ")" missed in first line
if ( !empty( $ImStoreCart->cart->items )) {
$count = $ImStoreCart->cart->items;
echo "Items in Basket: ". $count;
} else { echo "Your shopping basket is empty."; }

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.

PHP Parse error: syntax error, unexpected '}' [duplicate]

This question already has answers here:
Parse error: syntax error, unexpected '}' but can not find another [closed]
(3 answers)
Closed 10 years ago.
OK I'm returning to PHP after not using it for a couple of years and I'm trying to do a simple check of a $_POST variable.
I have:
if(isset($_POST['partydate'])) { $partydate = $_POST['partydate'] } else { $partydate = "No Date Selected" };
It's the only thing on that line but I keep getting the following when the page runs:
Parse error: syntax error, unexpected '}' in C:\xampp\htdocs....... on line 3
What is the obviously VERY simple thing I am overlooking here?!
One-liners are bad for code readability. If displayed better, your code becomes:
if (isset($_POST['partydate'])) {
$partydate = $_POST['partydate']
}
else {
$partydate = "No Date Selected"
}
;
So as you can see, you are missing semi-colons in your if and else blocks. Proper code is:
if (isset($_POST['partydate'])) {
$partydate = $_POST['partydate'];
}
else {
$partydate = "No Date Selected";
}
Use the ternary if if you really want to use a one-liner:
$partydate = isset($_POST['partydate']) ? $_POST['partydate'] : "No Date Selected";
Try this for size, with proper placement of ";"
if(isset($_POST['partydate'])) {
$partydate = $_POST['partydate'];
} else {
$partydate = "No Date Selected";
}
You have miss two
;
after the assignment of the string try this code:
if(isset($_POST['partydate'])) {
$partydate = $_POST['partydate'];
} else {
$partydate = "No Date Selected";
};

Categories