Jump to content

Basic PHP coding question


pdenlinger

Recommended Posts

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

[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>";
[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)
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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

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.