i am getting unexpected 'function' (T_FUNCTION) error - php

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.

Related

Where is the error in my code? syntax error, unexpected token "," (PHP)

I am a total beginner on PHP so that is why I can't find what the error is... it seems very simple but I can't find it, this is the part of my code that is giving me trouble:
function invalidRUT($username) {
$result;
if (!preg_match("/^[0-9]*$/"), $username) {
$result = true;
}
else {
$result = false;
}
return $result;
}
And the error message is:
syntax error, unexpected token "," (lin 15, col.5)
you accidentally wrote "," in your code and it caused an error, please delete or replace the ","
Try this:
(fixed code)
function invalidRUT($username) {
$result;
if (!preg_match("/^[0-9]*$/", $username)) {
$result = true;
}
else {
$result = false;
}
return $result;
}

PHP5.4 code Runs on server but not on local

I got a php code (index.php) from a friend, something like this:
<?
if (!isset($_SESSION['stdtype']))
{
$datas = mysql_query($query);
$data = mysql_fetch_assoc($datas);
if ($data) {
?><li>Acc Letter</li><?
}
else {
?><li>Acc Letter</li><?
}
}
$datas = mysql_query($query2);
$data = mysql_fetch_assoc($datas);
if ($data)code
{
?>
<li>Upload</li>
<?php
}
$datas = mysql_query($query3);
$data = mysql_fetch_assoc($datas);
if ($data) {
if ($data['rank'] <> 6 && $data['rank'] <> 0) {
?>
the error is:
::1:56706 [500]: /index.php - syntax error, unexpected '}' in /home/asd/phpT_T/index.php on line 333
line 333 from above snippet is:
} <-- this one
$datas = mysql_query($query3);
is there anything on php.ini that I should add to make the exact copied code from server works on my computer? because my computer already has the same php version (5.4.45)
go up a couple of lines. You will find this:
if ($data)code
{
this should be
if ($data)
{
Also replace <? with <?php everywhere in your code
for your code to work

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

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
}

syntax error, unexpected $end in PHP, and I already checked '}'

<?php
require_once('Auth/Auth-1.6.4/Auth.php');
class MyAuth extends Auth
{
function custom_login( $username = null, $status = null )
{
if( $status == AUTH_WRONG_LOGIN )
$status_msg = 'nameORpasswordWRONG';
else
$status_msg = '';
echo <<<LOGIN_FORM
LOGIN_FORM;
}
}
?>
I get the following error:
syntax error, unexpected $end
I got this error when I added the following lines:
echo <<<LOGIN_FORM
LOGIN_FORM;
I checked }, but they seem to correct. Can somebody help me?
There must be no whitespace between the new line and the closing LOGIN_FORM;
See the big warning in the documentation
Heredoc style string enders cannot be indented.
When using HEREDOCs, the end tag must be on the line by itself. No white space before it.
<?php
require_once('Auth/Auth-1.6.4/Auth.php');
class MyAuth extends Auth
{
function custom_login( $username = null, $status = null )
{
if( $status == AUTH_WRONG_LOGIN )
$status_msg = 'nameORpasswordWRONG';
else
$status_msg = '';
echo <<<LOGIN_FORM
LOGIN_FORM;
}
}
?>
Make sure there's no whitespace before LOGIN_FORM;.
LOGIN_FORM has to be flush with the very left. See the PHP documentation on HEREDOCs

Categories