Home > Archive > PHP Language > May 2006 > Leaning php
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
|
| I purchased a "Learn PHP" book and am trying to teach myself. Here is a
function from the book which is not working.
Could someone tell me what I am doing incorrectly.
Here is the error message:
Parse error: syntax error, unexpected '=' in
/home/dandgra/public_html/phptest/chp7.php on line 18
Here is the code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Chp 7</title>
</head>
<body>
<h1>Arrays Chapter 7 </h1>
<?php
// this is not working
function addNums ( ) {
$ret = "<table border=\"1\">";
for ( $x=0; $x<func_num_arg(); $x++){
$arg = func_get_arg($x);
$result += $arg;
$ret . = "<tr><td>number ".($x+1).":</td><td>$arg</td></tr>";
}
$ret .= "<tr><td>result: </td><td>$result</td></tr>";
$ret .="</table>";
return $ret;
}
print addNums (3, 4, 6, 99);
?>
</body>
</html>
| |
| Dave Kelly 2006-05-10, 6:58 pm |
| Jimbo wrote:
> I purchased a "Learn PHP" book and am trying to teach myself. Here is a
> function from the book which is not working.
>
> Could someone tell me what I am doing incorrectly.
>
> Here is the error message:
> Parse error: syntax error, unexpected '=' in
> /home/dandgra/public_html/phptest/chp7.php on line 18
>
> Here is the code:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
> "http://www.w3.org/TR/html4/loose.dtd">
> <html>
> <head>
> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
> <title>Chp 7</title>
> </head>
>
> <body>
> <h1>Arrays Chapter 7 </h1>
> <?php
>
> // this is not working
> function addNums ( ) {
> $ret = "<table border=\"1\">";
> for ( $x=0; $x<func_num_arg(); $x++){
> $arg = func_get_arg($x);
> $result += $arg;
> $ret . = "<tr><td>number
> ".($x+1).":</td><td>$arg</td></tr>";
> }
> $ret .= "<tr><td>result: </td><td>$result</td></tr>";
> $ret .="</table>";
> return $ret;
> }
> print addNums (3, 4, 6, 99);
>
>
> ?>
> </body>
> </html>
Being a new coder myself, I don't have an answer. But I do have a question.
$ret = "<table border=\"1\">";
$ret . = "<tr><td>number>".($x+1).":</td><td>$arg</td></tr>";
$ret .= "<tr><td>result: </td><td>$result</td></tr>";
Are the first 7 charecter of the 3 lines above suppose to be the same?
Or correct as is?
| |
| Cédric Olmanst 2006-05-10, 6:58 pm |
| (sorry for my English)
Jimbo a écrit :
> function addNums ( ) {
> $ret = "<table border=\"1\">";
> for ( $x=0; $x<func_num_arg(); $x++){
> $arg = func_get_arg($x);
> $result += $arg;
It's the first line I see $result. Where does it come from ?
> $ret . = "<tr><td>number ".($x+1).":</td><td>$arg</td></tr>";
> }
> $ret .= "<tr><td>result: </td><td>$result</td></tr>";
> $ret .="</table>";
> return $ret;
> }
> print addNums (3, 4, 6, 99);
| |
|
| Jimbo wrote:
> $ret . = "<tr><td>number ".($x+1).":</td><td>$arg</td></tr>";
Change this to
$ret .= "<tr><td>number ".($x+1).":</td><td>$arg</td></tr>";
'.=' is an operator, '. =' isn't,
Your code would translate like:
$ret combined with '=', which isn't a function, a variable or an allowed
constantname.
http://www.php.net/manual/en/language.operators.php
Grtz,
--
Rik Wasmus
| |
|
| Thanks for the help and the php manual link.
Rik wrote:
> Jimbo wrote:
>
>
> Change this to
> $ret .= "<tr><td>number ".($x+1).":</td><td>$arg</td></tr>";
>
> '.=' is an operator, '. =' isn't,
>
> Your code would translate like:
> $ret combined with '=', which isn't a function, a variable or an allowed
> constantname.
>
> http://www.php.net/manual/en/language.operators.php
>
> Grtz,
|
|
|
|
|