Jump to content
Truman

Why this code doesn't run?

Recommended Posts

I'm working on my php skills and I run .php files on XAMPP server but it signals parse error even when php code checker sees no problem.

This is one of the codes that I wrote:

<!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"
xml:lang="en" lang="en">
 <head>5 <meta http-equiv="content-type"
content="text/html; charset=utf-8" />
 <title>Quotes</title>
 </head>
 <body>
 <?php // Script 2.4 - pursue3.php
 $name = "M";
 $age = 29;
 $lastname = 'B';
 $nationality = "S";
 print "<h1>Basic Info</h1><p>My name is $name, I am $age years old.<br/>My nationality is $nationality. My lastname is $lastname</p>"
 ?>
 </body>
 </html>
 

When I execute this code on my localhost it says: "Parse error: syntax error, unexpected '?' in C:\xampp\htdocs\my-site\pursue3.php on line 18"

 

It happened with last 3 php files that I wrote. I know what a parse error is, I just don't see where I made any mistake.

Edited by Truman

Share this post


Link to post
Share on other sites

I corrected this line to:

 print "<h1>Basic Info</h1><p>My name is $name, I am $age years old.<br/>My nationality is $nationality. My lastname is $lastname</p>";

...but it still doesn't work and the message I receive is the same.

Share this post


Link to post
Share on other sites

Have you save it as encoded utf-8? cuz i tried it with and without, with error reporting on and i'm not getting any error using either option, but you should definitely finish with ';'.

Share this post


Link to post
Share on other sites

Yes, it's with utf-8. It's interesting that when I first wrote the code I made a mistake on line 18. When I corrected that mistake and tried to execute the code but the message was the same.

Share this post


Link to post
Share on other sites

I am sorry, I don't understand your previous comment.

 

And I have a similar problem with my next task. I created two files ( html form and php ) but it doesn't execute properly.

Check it if you wish:

<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Feedback Form</title>
</head>
<body>
<!-- Script 3.1 feedback.html -->
<div><p>Please, complete this form to submit a feedback:</p></div>
<form method="post" action="handle_form.php">
<p>Name: <select name="title">
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Ms.">Ms.</option>
</select><input type="text" name="name" size="20" /></p>
<p>Email Address: <input type="text" name="email" size="20" /></p>
<p>Response: This is...
<input type="radio" name="response" value="excellent" /> excellent
<input type="radio" name="response" value="okay" />Okay
<input type="radio" name="response" value="boring" />boring</p>
<p>Comments: <textarea name="comments" rows="3" cols="30"></textarea></p>
<input type="submit" name="submit" value="Send My Feedback" />
</form>
</div>
</body>
</html>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8"/>
<title>Your Feedback</title> 
</head>
<body>
<?php // Script 3.3  handle_form.php
// This page receives data from feedback.php
// It will receive: title, name, email, response, comments, and submit in $_POST.
$title = $_POST['title'];
$name = $_POST['name'];
$response = $_POST['response'];
$comments = $_POST['comments'];
print "<p>Thank you, $title $name, for your comments</p>
<p>You stated that you found this example to be '$response' and added:<br />$comments</p>";
?>
</body>
</html>
Edited by Truman

Share this post


Link to post
Share on other sites

what I get when I fulfill the html form is:

 

Thank you, $title $name, for your comments

You stated that you found this example to be '$response' and added:
$comments

"; ?> /body>

Share this post


Link to post
Share on other sites

Instead of using 'print' use 'echo' instead.

echo '<p>Thank you, '.$title.' '.$name.', for your comments</p>';
echo '<p>You stated that you found this example to be \''.$response.'\' and added:<br />'.$comments.'</p>';
Edited by dsonesuk
  • Like 1

Share this post


Link to post
Share on other sites

Now the response is:

Thank you, '.$title.' '.$name.', for your comments'; echo '

You stated that you found this example to be \''.$response.'\' and added:
.'$comments.'

'; ?>

 

Using print should be ok, I have a feeling that something is wrong with my server, XAMPP is in actions but I don't get why these kind of responses for different files that I tried to execute.

Edited by Truman

Share this post


Link to post
Share on other sites

I made error in last bit, (I edited the code to show this) 'print is old ans slower compared to 'echo', you should not see any of the php code? it is like you using it on html page rather than php page? you are not saving it as myfile.php.html by mistake are you? with hidden extensions you will see myfile.php, but hidden extension will be html, its been done before.

Share this post


Link to post
Share on other sites

No hidden extension, I follow the book "PHP for the web" by Larry Ullman. Everything is as in the book. After first few fails I even copy-pasted codes but nothing changes...

After rerunning your edited code it says:

 

Thank you, '.$title.' '.$name.', for your comments'; echo '

You stated that you found this example to be \''.$response.'\' and added:
'.$comments.'

'; ?>

Edited by Truman

Share this post


Link to post
Share on other sites

It looks like your server is not executing PHP at all.

Did you give the file a .php extension and is it running on a server with PHP installed?

Share this post


Link to post
Share on other sites

Yes, I gave a php extension and I use XAMPP that has php installed.

By the way, it did execute php code for some simple things such as phpinfo function. Whenever I made a mistake that needed to be fixed after I did it the server didn't respond well.

Edited by Truman

Share this post


Link to post
Share on other sites

I use notepad++. I will procceed with next task and see what will happen. Maybe I'll come back to these that don't work.

Share this post


Link to post
Share on other sites

A friend helped me with editing code, this is how it should look:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
    <title>Your Feedback</title>
</head>
<body>
<?php // Script 3.3  handle_form.php
// This page receives data from feedback.php
// It will receive: title, name, email, response, comments, and submit in $_POST.
$title = $_POST['title'];
$name = $_POST['name'];
$response = $_POST['response'];
$comments = $_POST['comments'];
print "<p>Thank you, {$title} {$name}, for your comments</p>
<p>You stated that you found this example to be '{$response}' and added:<br />{$comments}</p>";
?></body>
</html>


So basically, he only added curly braces in quotes to variables in paragraph...

Edited by Truman

Share this post


Link to post
Share on other sites

The curly braces are not required.

 

The real problem seems to be that the server is not running the PHP code which has nothing to do with PHP syntax.

Share this post


Link to post
Share on other sites

I minor problem this time. I'm practising sending data to server without html form. So, the first code is html with <a href=...> and the second is php that receives data from html.

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"lang="en">
<head>
<meta http-equiv="Content Type" content="text/txt" charset="utf-8"/>
<title>Greetings!</title>
</head>
<body>
<!-- Script 3.6 - hello.html --> 
<div><p>Click a link to say hello:</p>
<ul>
 <li><a href="hello.php?name=Michael">Michael</a></li> 
 <li><a href="hello.php?name=Celia">Celia</a></li>
 <li><a href="hello.php?name=Jude">Jude</a></li>
 <li><a href="hello.php?name=Sophie">Sophie</a></li>
 
</ul>
</div>
</body>
</html>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/
xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml"
xml:lang="en" lang="en">
4 <head>
5 <meta http-equiv="content-type"
content="text/html; charset=utf-8" />
6 <title>Greetings!</title>
7 </head>
8 <body>
9 <?php // Script 3.7 hello.php 
    ini_set ('display_errors', 1);
	error_reporting (E_ALL | E_STRICT);
	$name = $_GET['name'];
	print "<p>Hello,<span style=\"font-weight:bold;\">$name</span>!</p>";
	?>
22 </body>
23 </html>

This time I received:

3 4 5 6 7 8 9

Hello,Michael!

22 23

Why those numbers appeared? They are propably lines in notepad++.

Share this post


Link to post
Share on other sites

Thank you. Now I'm working on ecommerce task. I made a html form for entering data about purchasing and php to handle it and calculates things.

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTDxhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content Type" content="text/html; charset=utf-8"/>
  <title>Product Cost Calculator</title>
</head>
<body><!-- Script 4.1 - calculator.html -->
<div><p>Fill out this form to calculate the total cost:</p>
<form action="handle_calc.php" method="post">
<p>Price: <input type="text" name="price" size="5" /></p>
<p>Quantity: <input type="text" name="quantity" size="5" /></p>
<p>Discount: <input type="text" name="discount" size="5" /></p>
<p>Tax: <input type="text" name="tax" size="3" /> (%)</p>
<p>Shipping method: <select name="shipping">
<option value="5.00">Slow and steady</option>
<option value="8.95">Put a move on it.</option>
<option value="19.36">I need it yesterday!</option>
</select></p>
<p>Number of payments to make:<input type="text" name="payments" size="3" /></p>
<input type="submit" name="submit" value="Calculate!" />
</form>
</div>
</body>
</html>
<!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" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title>Product Cost Calculator</title>
 <style type="text/html" media="screen">
 .number{font-weigh="bold";}
 </style>
</head>
<body>
<?php // Script 4.2 - handle_calc.php
$price = $_POST['price'];
$quantity = $_POST['quantity'];
$discount = $_POST['discount']
$tax = $_POST['tax'];   
$shipping = $_POST['shipping'];
$payments = $_POST['payments'];
$total = $price * $quantity;
$total = $total + $shipping;
$total = $total - $discount;
$taxrate = $tax/100;
$taxrate = $taxrate + 1;
$tax = $total * $taxrate;
$monthly = $total / $payments;
Print "<p>You have selected to purchase: <br />
<span class=\"number\">quantity></span> widget(s) at: <br />
$<span class=\"number\">$price</span> price each plus a <br />
$<span class=\"number\">$shipping</span> shipping cost and a <br />
<span class=\"number\">$tax</span> percent tax rate. <br />
After your $<span=\"number\">$discount</span> discount, the total cost is 
$<span class=\"number\">$total</span>.<br />
Divided over<span class=\"number\">$payments</span> monthly payments that would be $<span class=\"number\">$monthly</span>each.</p>"
?>
</body>
</html> 

The message that I receive is:

Parse error: syntax error, unexpected '$tax' (T_VARIABLE) in C:\xampp\htdocs\my-site\handle_calc.php on line 16

 

I don't understand what is wrong in line 16: $tax = $_POST['tax'];

The php code checker signalizes the same error.

Share this post


Link to post
Share on other sites

I have a problem with this code:

<!DOCTYPE html PUBLIC "-//W3C//DTDXHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Forum Posting</title>
</head>
<body>
<?php // Script 5.2 - handle_post.php
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name];
$posting = $_POST['posting'];
$name = $first_name . ' ' . $last_name;
print "<div>Thank you, $name, for your posting":<p>$posting</p></div>";
?>
</body>
</html>

After I enter data to html form it says:
Parse error: syntax error, unexpected 'posting' (T_STRING), expecting ']' in C:\xampp\htdocs\my-site\handle_post.php on line 12

Edited by Truman

Share this post


Link to post
Share on other sites

I think the forum's syntax highlighting makes it obvious.

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...