pdenlinger Posted January 18, 2007 Share Posted January 18, 2007 I'm having trouble with the following code; I get a Parse error: syntax error, unexpected T_ECHO, expecting ',' or ';' on line 14. Can you please tell me what the error is?Thank you.[code]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Assigning values to variables</title></head><body> <h1>Assigning values to variables</h1> <?php echo "Setting number of apples to 1.<br>"; $apples=1; echo "Number of apples: ", $apples, "<br>" echo "Adding 3 more apples.<BR>"; $apples = $apples + 3; echo "Number of apples now: ", $apples, "<BR>"; ?></body></html>[/code] Link to comment https://forums.phpfreaks.com/topic/34763-basic-php-coding-question/ Share on other sites More sharing options...
zhahaman2001 Posted January 18, 2007 Share Posted January 18, 2007 [code]<?php echo "Setting number of apples to 1.<br>";$apples=1;at the end of the line below you need a ;echo "Number of apples: ", $apples, "<br>"echo "Adding 3 more apples.<BR>";$apples = $apples + 3;echo "Number of apples now: ", $apples, "<BR>";?>[/code]and instead of saying this echo "Number of apples now: ", $apples, "<BR>";you can say this echo "Number of apples now: $apples <BR>"; Link to comment https://forums.phpfreaks.com/topic/34763-basic-php-coding-question/#findComment-163862 Share on other sites More sharing options...
Psycho Posted January 18, 2007 Share Posted January 18, 2007 You are missing a semi-colon at the end of line 13. And you need to use a period to concatenate strings - not a comma Link to comment https://forums.phpfreaks.com/topic/34763-basic-php-coding-question/#findComment-163864 Share on other sites More sharing options...
Nhoj Posted January 18, 2007 Share Posted January 18, 2007 [quote author=pdenlinger link=topic=123013.msg507989#msg507989 date=1169145496][code]<?php echo "Setting number of apples to 1.<br>";$apples=1;echo "Number of apples: ", $apples, "<br>";echo "Adding 3 more apples.<BR>";$apples = $apples + 3;echo "Number of apples now: ", $apples, "<BR>";?>[/code][/quote]If you're concatenating strings like that don't use double quotes, it wastes memory. It may not be a lot but in very very large scripts it can make a difference. Double quotes forces PHP to scan for variables within the quotes. Concatenating is nearly pointless when you use double quotes with it. (Not entirely, but nearly) Link to comment https://forums.phpfreaks.com/topic/34763-basic-php-coding-question/#findComment-163907 Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 Nhoj is right. A more effective way of writing all that is:[code]<?php echo 'Setting number of apples to 1.<br>';$apples=1;echo "Number of apples: $apples<br>Adding 3 more apples.<br>";$apples = $apples + 3;echo "Number of apples now: $apples<BR>";?>[/code]OR:[code]<?php echo 'Setting number of apples to 1.<br>';$apples=1;echo 'Number of apples: '.$apples.'<br>Adding 3 more apples.<br>';$apples = $apples + 3;echo 'Number of apples now: '.$apples.'<br>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/34763-basic-php-coding-question/#findComment-163916 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.