PHP error on a line of code I don't understand - php

I have an error on this code:
code:
$sessionsAry[] .= array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark']);
error:
Parse error: syntax error, unexpected T_VARIABLE in /web/stud/u0867587/Mobile_app/student_overall_grade.php on line 122
What does this error mean and where is the error on this particular code?

First, remove the '.'
$sessionsAry[] = array('SessionId'=>$row['SessionId'], 'Mark'=>$row['Mark']);
Now, if you're still getting the error, make sure that $sessionsAry is an array and that $row is an array too. Try:
var_dump($sessionsAry, $row);
Also, make sure that you haven't missed off a ';' on the line before.

On line 121 you definetly forgot to put a semicolon, that is #1 explanation for this error usually.

You don't need to write .= since $array[] add a new entry to $array. So, use simply =.

When you see a "Parse error" spit out by PHP, it usually means you forgot a character (;) or formatted an expression incorrectly (.= has an extra period) . Consider getting a new PHP IDE that can point the error before you run the code. It will drastically help speed up your ability to code in PHP.

Related

Unexpected PHP Parse error: (T_CONSTANT_ENCAPSED_STRING), after I moved to new server

I was trying to move my website to my new server. But after I moved the code and wanted to run the website, I got this php error:
PHP Parse error: syntax error, unexpected ''] = "'' (T_CONSTANT_ENCAPSED_STRING), expecting ']' in /home/mywebsite/public_html/includes/functions/functions_general.php on line 1230
Here is the code on line 1230:
$trans_table['''] = "'";
The strange thing is that this error was never reported on my old server. Could someone help?
Thank you.
This is one of most common parsing PHP errors, I have encountered it mostly due to version conflict.
The PHP version on new server may be different from old one. Most probably lower than old one. Upgrade/ Match PHP version on both servers to get rid of error.
There are three single quotes characters inside the [], you should use double quotes for array index, instead of single quotes.
use " " instead of ' '
$trans_table["'"] = "'";
You are using [singlequote singlequote singlequote] instead use [doublequote singlequote doublequote]
I suppose that you probably have had different strictness settings for your PHP on old server. In there the error isn't reported and execution has continued. The error seems to be quite obvious, though:
$trans_table['''] = "'";
becomes
$trans_table['''] = "'";
The part between [] has three ' in it after the ' is interpret as '. Use "" instead around it and should work.
You have three single-quote characters between the square brackets: [''']. It looks like PHP is interpreting the first two single-quote characters as a zero-length string, and then getting confused how to interpret the third single-quote.
I expect that the version of PHP, or the error-reporting configuration, differs between the old server and the new. The old server let the error slide, the new one is telling you about it.
Similar to existing questions, Syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in PHP and Error Parse error: syntax error, unexpected T_STRING, expecting T_CONSTANT_ENCAPSED_STRING

(PHP,xampp) syntax error in every code I test

I get a " Parse error: syntax error, unexpected" error for whatever I print.
The error "Parse error: syntax error, unexpected '"test"' (T_CONSTANT_ENCAPSED_STRING) in C:\xampp\htdocs\firstfile\test.php on line 3"
The code I tested
<?php
 echo 'test';
 
?>
A basic code, but the error persist.
How to solve this problem?
Could it be that what you posted is not the actual file that is executed? Certainly that file is fine syntactically, so there must be some stupid mistake somewhere. I see that the faulty file is inside a folder firstfile. You are really sure this posted file is that file inside the folder?
What is especially outstanding here are the double quote chars in the error message which simply are not present in the file you posted.
When an error like this remains unchanged whatever you do, then typically you edit the wrong file.

Sprintf in php what am I doing wrong

I am new to PHP and I am somewhat understanding it. Now I am working on something but I keep getting a error message saying Parse error: parse error, unexpected T_ECHO in C:\wamp\www\mymoney.php on line 11. Now I am looking at line 11 and I don't see anything i am doing wrong. So I was woundering if someone can help me understand what I might be doing wrong thanks.
<html>
<head>
<title>money $</title>
</head>
<body>
<?php
$balance=55.75;
$newShirtCost=15.75;
$earns=20.00;
$buyscandybar=.55
echo "<p> Starting balance:" .sprintf("$%.2f",$balance)."</p>";
$balance= $balance -$newShirtCost;
echo "<p>Purchase: Clothing Store:".sprintf("$%.2f",$newShirtCost)."</p>";
$balance=$balance/2;
echo "<p>ATM Deposit:".sprintf("$%.2f",$earns)."</p>";
$balance=$balance-$buyscandybar;
echo "<p>Purchase: Gas Station:".sprintf("$%.2f",$buyscandybar)."</p>";
$balance=$balance
echo "<p>Ending Balance:".sprintf("$%.2f",$balance)."</p>";
?>
$buyscandybar=.55
Semicolon missed. When that error appears always check the line before too.
Also this makes no sense:
$balance=$balance
and it's without semicolon
Also you should separate your presentation by your logic
Use single-quotes strings or escape the $ with a \. Single-quotes strings are preferred of course since escaping = ugly.
"Parse error" means exactly parse error. It usually locates before the place, where interpreter points you. You've missed ";" at line 10.

Parse error: syntax error, unexpected T_SL on line 23

I am getting this error:
Parse error: syntax error, unexpected
T_SL on line 23
Here is line 23:
$selectorder = <<<ORDER
Here it is in context:
$grid->setUrl('myfirstgrid.php');
$selectorder = <<<ORDER
function(rowid, selected)
{
if(rowid != null) {
alert("selected: "+rowid);
}
}
ORDER;
$grid->setGridEvent('onSelectRow', $selectorder);
What is causing this error?
I personally don't know what <<< does and have never used it, I got it from a tutorial. I tried to google it, but you can't google characters like that :(
Check for whitespace after <<<ORDER. There should be no blank characters.
<<< is for heredoc: See manual
Make sure that there is no SPACE/INDENTATION before ending ORDER;
PHP Heredoc does not get on well with the % symbol, and the following also causes Parse error: syntax error, unexpected T_SL:
<?php
$var=<<<%%SHRUBBERY%%
Nih!
%%SHRUBBERY%%;
?>
Also make sure that you have 3 '<<<'. Omitting one will throw this error. Also if your using NOWDOCs, make sure your hosting provider has php 5.3 installed. Plus if your php environment is below 5.3, do not use double quotes or single quotes.
It's called "Heredoc syntax", and it lets you specify large strings without using quotes. In this case, it looks like you're using it to put JavaScript code into a variable. Since you started the string with <<<ORDER, you should be able to finish it with ORDER;, as you have — but you need to make sure that ORDER; occurs at the start of a line, with no whitespace before it.

T_STRING error on line that just says <?php

So I'm writing a script in codeigniter, and I get the following error message:
Parse error: syntax error, unexpected T_STRING in /home/globalar/public_html/givinghusband.com/system/application/controllers/sizes.php on line 1
the only problem: the only thing on that line is this:
<?php
So I'm quite mysterified as to what's going on here? Have I typed PHP wrong or what?
There could be a problem with your editor when it updated the file. I just had this problem and the editor removed all line breaks.
If you are uncertain try opening your php file in another editor or use the Cpanel file manager to take a peak.
Or you may have an unterminated quote or statement on some previous line without an ending semicolon (;) . Check all files that are included before this one.
a bare <?php in the file leads to a parse error in php (don't ask). Try adding a whitespace or a newline after it
Sorry.A bit late but might helpful for others.Just looked at your question.Just use
<?
?>
instead of :
<?php
?>
and remove whitespaces/line breaks between your php open tag and class name .It will resolve this conflict.Ta

Categories