Jump to content
xisto Community
Sign in to follow this  
NilsC

Php Problem Error Message Im working from a book

Recommended Posts

I get this error message when I try to view a small webpage.

Parse error: parse error, unexpected T_STRING, expecting ',' or ';' in /home/nilsc/public_html/bobs/processorder.php on line 27

Line 27 is in this php part:

<?php	echo '<p>Order processed.</p>';	echo $tireqty. ' tires<br />';	echo $oilqty. ' bottles of oil<br />';	echo $sparkqty. ' spark plugs<br />';		$totalqty = 0;	$totalqty = $tireqty + $oilqty + $spakqty;	echo 'Items ordered: ' .$totalqty.'<br />;		$totalamount = 0.00;		define('TIREPRICE', 100);	define('OILPRICE', 10);	define('SPARKPRICE', 4);		$totalamount = $tireqty * TIREPRICE     + $oilqty * OILPRICE     + $sparkqty * SPARKPRICE;     	echo 'Subtotal: $'.number_format($totalamount,2).'<br />;		$taxrate = 0.10;	// local tax is 10%	$totalamount = $totalamount * (1 + $taxrate);		echo 'Total including tax: $'.number_format(stotalamount,2).'<br />	?>
Line 27 is define['TIREPRICE', 100); The book seems to be using PHP5 and mySQL5 is the "define" something that is new to PHP5?

 

Thanks

Nils

Share this post


Link to post
Share on other sites

whenever it says

expecting ',' or ';' in /home/nilsc/public_html/bobs/processorder.php

it means that you simply forgot a semicolon. And after a quick runthrough, i found that you forgot a semicolon here:
echo 'Total including tax: $'.number_format(stotalamount,2).'<br />
so add a (;) and you're done ^_^

Share this post


Link to post
Share on other sites

I changed the ' in the define statement to ". I think I was defining an array instead of a constant.

Now the error moved down to the echo 'Subtotal: $' line and I tried 2 different ways of formatting the $totalamount and I still get the same error different line:

 

define("TIREPRICE", 100);	define("OILPRICE", 10);	define("SPARKPRICE", 4);		$totalamount = $tireqty * TIREPRICE     + $oilqty * OILPRICE      + $sparkqty * SPARKPRICE;     	echo 'Subtotal: $' .number_format($totalamount, 2, '.', ',').'<br />;		$taxrate = 0.10;	// local tax is 10%	$totalamount = $totalamount * (1 + $taxrate);		echo 'Total including tax: $' .number_format($totalamount, 2).'<br />;

What am I overlooking here. I know I can pop the disk in that came with the book to get the code ready made but that will not teach me how to find errors in the code. I think I learn more by hacking it until I figure it out or....

 

Thanks for any pointers

Nils

Share this post


Link to post
Share on other sites

I found my error so anyone else who encounters the same problem... cross your t's and dot your i's :)

 

I had forgotten a ' after the <br />

echo 'Items ordered: $'.$totalqty.'<br />';		$totalamount = 0.00;		define('TIREPRICE', 100);	define('OILPRICE', 10);	define('SPARKPRICE', 4);		$totalamount = $tireqty * TIREPRICE     + $oilqty * OILPRICE      + $sparkqty * SPARKPRICE;     	echo 'Subtotal: $'.$totalamount.'<br />';		$taxrate = 0.10;	// local tax is 10%	$totalamount = $totalamount * (1 + $taxrate);		echo 'Total including tax: $'.number_format($totalamount,2).'<br />';
Here is the updated code that worked.

 

Nils

Share this post


Link to post
Share on other sites

lol, sorry i didn't catch that for ya. I just saw a missing semicolon and decided that it was the problem so i didn't look through the rest =P glad you fixed it up though ^_^

Share this post


Link to post
Share on other sites

I guess I have to be less sloppy while working on this. The biggest problem for me are missing the semicolons and use a capital S instead of $ :)It's killing me... but in order for me to learn I have to do the hack job... copying from the disc don't help me any.I'm learning!Nils

Share this post


Link to post
Share on other sites

Hey,define is not new at all, this is how you can create constants in PHP, variables that can not be altered by your code.Say you defined MAX_FILE_SIZE:<?phpif(!defined('MAX_FILE_SIZE')) // checks whether we've already set MAX_FILE_SIZE define('MAX_FILE_SIZE', 8192); // sets MAX_FILE_SIZE to 8192if(constant('MAX_FILE_SIZE')) // checks whether MAX_FILE_SIZE is a constant echo constant('MAX_FILE_SIZE'); // prints the constant MAX_FILE_SIZEif(defined('MAX_FILE_SIZE')) // checks whether MAX_FILE_SIZE is set MAX_FILE_SIZE++; // add 1 to MAX_FILE_SIZE (cannot do, error on this line)?>Since I try altering MAX_FILE_SIZE by increasing it by 1, I would get a parse error on this line. You will encounter parse errors quite a lot and usually the message will help you find the problem, missing semi-colons or commas, etc. Although this message doesn't really help understand the problem, you should have a style that constants are all in capital letters, you may also like to prefix it with an underscore _ so it won't conflict with reserved keywords, not likely PHP but C/C++ you would.Cheers,MC

Share this post


Link to post
Share on other sites

I find a good way to prevent missing apostrophes and other such things is to do sets together. When you implement your first ‘ implement your second straight away ‘’ then add the text inside ‘hello world’. As in SGML I build inwards. Doing so is just simple and prevents so many simple errors popping up later, though watch that insert button write over all your work lol.1) <name></name>2) <name>Matthew</name>

Share this post


Link to post
Share on other sites

The text editor you use can also help you prevent such problems with syntax hilighting, colour coding, etc. This will allow you to pick up the problems more easily. I use UltraEdit under Windows, but you'll find there's quite a lot of others out there. On Linux anjuta for C/C++, Perl, etc, can be configured for any other languages too, but sometimes it's easier just using the provided text editor like kwrite. Again it's really personal preference in what you choose. I usually do most things in console anyways.Cheers,MC

Share this post


Link to post
Share on other sites

I'm using first page 2000 for my php and html edits and EditPad Lite for the css code. Is there a css editor out there (that high lights syntax?)1'st page 2000 works good for me and my needs right now. Does UltraEdit highlight the syntax on css sheets? or is that not possible.Working inwards, I'm doing that but not on a regular bases yet. I'm still memorizing the syntaxes and learning the basics. When I can write one simple page without going into the book looking at the syntax, then I'll graduate myself to the next level :)I'm using 3 books to get the basics down, and web resources on w3schools and sitepoint and a few others. One of the books are CSS only, the second and third is php & mySQL (Introductory and intermediate).The basic template for the site is 80% done so I'm working on the css, html and php right now. Then when that is done I'll build the mySQL db for the dynamic data that is going onto the pages and the forms to use for populating the tables.Nils

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.