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
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>";
Link to comment
Share on other sites

[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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.