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

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";
};

Related

PHP method returning an error of encapsed and white space [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 4 years ago.
I have the following PHP method:
public function addIndividual($conn, $hhid,
$marital_status,
$family_relation,
$ind_id,
$ind_id,
$head_of_hh,
$ind_first_name_ar,
$ind_last_name_ar,
$ind_first_name_en,
$ind_last_name_en,
$date_added,
$gender,
$dob,
$ind_status,
$arrayOfLegalProtection,
$user_id
)
{
$res = $this->checkNumberOfIndividual($conn, $household_id);
if($res=="enableAddInd")
{
try
{
$add = "INSERT INTO individual(hhid,
family_relation_id,
marital_status_id,
ind_id, ind_id,
head_of_hh, ind_first_name_ar, ind_last_name_ar,
ind_first_name_en, ind_last_name_en,
dob, ind_status,
ind_date_added, user_id)
VALUES (:hhid,
:family_relation_id,
:marital_status_id,
:ind_id, :ind_id,
:head_of_hh, :ind_first_name_ar, :ind_last_name_ar,
:ind_first_name_en, :ind_last_name_en,
:dob, :ind_status,
:ind_date_added, :user_id);
$execAdd = $this->conn->prepare($add);
$execAdd->bindValue('hhid', $hhid);
$execAdd->bindValue('family_relation_id', $family_relation);
$execAdd->bindValue('marital_status_id', $marital_status);
$execAdd->bindValue('ind_id', $ind_id);
$execAdd->bindValue('ind_id', $ind_id);
$execAdd->bindValue('head_of_hh', $head_of_household);
$execAdd->bindValue('ind_first_name_ar', $ind_first_name_ar);
$execAdd->bindValue('ind_last_name_ar', $ind_last_name_ar);
$execAdd->bindValue('ind_first_name_en', $ind_first_name_en);
$execAdd->bindValue('ind_last_name_en', $ind_last_name_en);
$execAdd->bindValue('dob', $dob);
$execAdd->bindValue('ind_status', $ind_status);
$execAdd->bindValue('user_id', $user_id);
$execAdd->execute();
$id = $conn->lastInsertId();
$ind_action = 'HH First Time added';
//Add into HH history
$this->addHHHistory($conn, $unit_id, $id, $hh_id, $last_name_en,
$last_name_ar, $un_id, $num_ind_of_hh,
$gov_id_type, $gov_id_number, $phone_number,
$address_in_origin_country, $hh_status, $date_added,
$hh_action,
$user_id);
$arr = json_decode($arrayOfLegalProtection, true);
//Add Array of protection, situation and legals
if(sizeof($arr)>0)
{
foreach($arr as $array)
{
if($array['situationId']=='')
{
$situation_id=null;
}
else{
$situation_id = $array['situationId'];
}
if($array['legalId']==='')
{
$legal_id=null;
}
else{
$legal_id = $array['legalId'];
}
if($array['protectionId']==='')
{
$protection_id=null;
}
else{
$protection_id = $array['protectionId'];
}
if($array['willing_to_go_back']==='')
{
$willing_to_go_back='';
}
else{
$willing_to_go_back = $array['willing_to_go_back'];
}
$status = 'Active';
try
{
$sqlActualities = 'INSERT into household_protection_legal(
hhid,
unit_id,
protection_id,
legal_id,
situation_id,
willing_to_go_back, unit_household_protection_status, unit_household_protection_date_added, user_id)
VALUES(:hhid,
:unit_id,
:protection_id,
:legal_id,
:situation_id,
:willing_to_go_back,
:unit_hh_protection_status,
:unit_hh_protection_date_added,
:userId)';
$sqlActExec = $this->conn->prepare($sqlActualities);
$sqlActExec->bindValue('household_id', $id);
$sqlActExec->bindValue('unit_id', $unit_id);
$sqlActExec->bindValue('protection_id', $protection_id);
$sqlActExec->bindValue('legal_id', $legal_id);
$sqlActExec->bindValue('situation_id', $situation_id);
$sqlActExec->bindValue('willing_to_go_back', $willing_to_go_back);
$sqlActExec->bindValue('unit_household_protection_status', $status);
$sqlActExec->bindValue('unit_household_protection_date_added', $date_added);
$sqlActExec->bindValue('userId', $user_id);
$sqlActExec->execute();
//return $res;
}
catch(PDOException $e)
{
return $e->getMessage();
}
}
}
}
}
catch(PDOException $e)
{
return $e->getMessage();
}
return $id;
}
else{
return $res;
}
}
I am getting the following error on load of the component:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE),
expecting identifier (T_STRING) or variable (T_VARIABLE) or number
(T_NUM_STRING) in C:\wamp64\www\dev\api.php on line 1005
The line 1005 is:
if($array['situationId']=='')
I read about the problem in this documentation but I have never been able to solve my problem.
I tried to search whites spaces or even missing colons or commas, but I didn't find anything wrong.
Error is clearly visible even in your SO question I am now answering. Thanks to syntax highlighting you can see there's too much lines highlighted as string, which usually is because of non-terminated strings. This type of error usually are reported imprecisely by lexer, as it cannot really tell for sure if you really wanted to put your code in string on purpose or not. If you did that on purpose then it's pretty valid, so it will complain on first line that no makes syntax error. I.e. when text no longer could be considered string content, as there would be " from other code line that, from lexer perspective terminates your "culprit"-string. But then the remaining part of that code starts to make no sense and that's why it complains pointing to that line instead of string declaration/variable assignemtn.
You need to close your SQL query string ($add) as it lacks closing ". Also get yourself a editor or IDE with working code highlighting.

PHP syntax error, unexpected 'elseif' [duplicate]

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
}
}
?>

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."; }

i am getting unexpected 'function' (T_FUNCTION) error

it is very frustrating , i am getting error - >
Parse error: syntax error, unexpected 'function' (T_FUNCTION) in
C:\xampp\htdocs\chat\inc\chat.func.php on line 6
in the following code and unable to find any solution. and my php version is 5.5.1 so i don't any point of 'older' version.
here is the code
<?php
include( 'connect.inc.php')
function get_msg() {
$que = "SELECT sender , message FROM chat ";
$run = mysqli_query($conncetion,$que)
$messages = array();
while ($message = mysql_fetch_assoc($run)) {
$messages[] = array{'sender'=>$message['sender'],'message'=>$message['message']};
}
return $messages;
}
function send_msg($sender,$message) {
if( !empty($sender) && !empty($message)) {
$sender = mysql_real_escape_string($sender);
$message = mysql_real_escape_string($message);
$query = "INSERT INTO chat VALUES('','$sender','$message') ";
if($run = mysqli_query($con,$query)) {
retur n true;
} else {
return flase;
}
} else {
return flase;
}
}
?>
You're missing a semicolon from the end of your include line at the top. I saw this when I tried to edit your post and saw all of the code. Tried formatting it for you but someone else has submitted an edit.
#user4141363, There are couple of errors in your code.Please check below code & replace with your original code.
//Below Semi-column is not there in your code.
include_once( 'connect.inc.php');
// Semicolon is missing in below line so please add semicolon as below.
$run = mysqli_query($conncetion,$que);
//Array braces wrong in your code so replace this line in your code.
$messages[] = array('sender'=>$message['sender'],'message'=>$message['message']);
//Space between return true code so it's cause syntax error.
return true;
//Return false spell mistake in your code so replace below line.
return false;
I have edited your code in your question, please copy that code & check it your end.

whats wrong with this format?

something is wrong with my code formatting i believe
i am still unsure of what is happening that gives this error,
i am getting the error Parse error: syntax error, unexpected T_VARIABLE, expecting '('
here is my code
<?php
$runamazonapi = false;
if $runamazonapi = true
{
"run this code"
else
}
//do nothing
{
?>
i am getting the following error on line 3 or at this part
if $runamazonapi = true
thanks for your help in advance!!
<?php
$runamazonapi = false;
if ($runamazonapi == true)
{
"run this code"
}
else
//do nothing
{
}
?>
There are a number of syntactical errors with your code, but the error means that the php parser expected to find an ( but instead found a variable. You need () around the if statement condition and you need a closing } on the first if condition. Also, you need to use the proper {} to open and close the else clause:
<?php
$runamazonapi = false;
if ($runamazonapi = true)
{
"run this code"
}
else
{
//do nothing
}
?>
Also, what you have won't work. You're assigning $runamazonapi to true, not checking if it is true. You need to use == not =:
<?php
$runamazonapi = false;
if ($runamazonapi == true)
{
"run this code"
}
else
{
//do nothing
}
?>
try
if($runamazonapi){
//run code
}else{
//do something
}

Categories