Sprintf in php what am I doing wrong - php

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.

Related

Can I add a double quote inside a variable in PHP when it starts with "\n"?

UPDATE !!!
It seems that the website I used to test my code has issues and therefore I had problem with executing my thing. I still leave this post up, so someone else, with similar problem can learn something.
website to avoid: http://phptester.net
ORIGINAL POST !!!
I want to add a string to my variable that uses a double qouted word. Is that possible? I only get error.
FATAL ERROR syntax error, unexpected 'Old' (T_STRING) on line number 4
Here is the code:
<?php
$var = "name: ";
$biography = "\n I'm Robert and I came from \"Old\" Europe.";
echo $biography;
?>
I was checking it on this website:
http://phptester.net
It must be smg to do with the new line character in the beginning, or perhaps its not even possible to add double qouted words to a variable. I am not sure.
Your help is greatly appreciated !

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

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.

Issue with dynamically generated javascript link with PHP

example link
returns PHP error:
syntax error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
Any ideas? (Hope this question makes sense. I'm trying to fix someone else's code.)
Check that there's not a "<?" or "<?php" at the beginning of the file. This code assumes you're not already within a PHP block. If you are, then you'll have to adjust (remove it). Or, you'll need to add close ?> somewhere before this html, and a <?php after it...
Sorry about that guys, the problem was <?php echo val;?> instead of <?php echo $val;?>. Sometimes the eyes betray the mind...

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