By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,047 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,047 IT Pros & Developers. It's quick & easy.

Help with this code?

P: n/a
This is straight out of a book I'm reading to learn PHP. I'm thinking
maybe I set either apache or PHP up incorrectly on my computer.
Simpler PHP's snippets work fine so I just don't know. Could someone
look this over and see if I have a problem?

<?php
function printColor($text,
$color="black", &$count=NULL)
{
//print the text with style
print("<span style=\"color: $color\">" .
"$text</span>");

//if given a count, increment it
if(isset($count))
{
$count++;
}
}

//call with one argument
printColor("This is black text");
print("<br>\n");

//override default color
printColor("This is blue text", "blue");
print("<br>\n");

//pass in count reference
$c = 0;
printColor("This is red text", "red", $c);
print("<br>\n");
printColor("This is green text", "green", $c);
print("<br>\n");
print("Count: $c<br>");
?>
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
remove the ampersand in front of the $count in the function declaration

Marty U

"kyote" <tr******@NOSPAM.charter.net> wrote in message
news:na********************************@4ax.com...
This is straight out of a book I'm reading to learn PHP. I'm thinking
maybe I set either apache or PHP up incorrectly on my computer.
Simpler PHP's snippets work fine so I just don't know. Could someone
look this over and see if I have a problem?

<?php
function printColor($text,
$color="black", &$count=NULL)
{
//print the text with style
print("<span style=\"color: $color\">" .
"$text</span>");

//if given a count, increment it
if(isset($count))
{
$count++;
}
}

//call with one argument
printColor("This is black text");
print("<br>\n");

//override default color
printColor("This is blue text", "blue");
print("<br>\n");

//pass in count reference
$c = 0;
printColor("This is red text", "red", $c);
print("<br>\n");
printColor("This is green text", "green", $c);
print("<br>\n");
print("Count: $c<br>");
?>

Jul 17 '05 #2

P: n/a
kyote wrote:
This is straight out of a book I'm reading to learn PHP. I'm thinking
maybe I set either apache or PHP up incorrectly on my computer.
Simpler PHP's snippets work fine so I just don't know. Could someone
look this over and see if I have a problem?
I am just starting to play with php myself and thought this looked ok,
but wondered why it didn't work. As someone else mentioned, you can
remove the ampersand, but then, you won't keep the value of $count.

<?php
function printColor($text,
$color="black", &$count=NULL) function printColor($text, $color="black", &$count)
If you remove the default setting of =NULL, it works fine, but really
expects something to be passed (it works, but you get warnings if you
don't pass something). I just added an argument to the first two calls,
and since $c hasn't been defined yet, it worked. But, it sure is
ugly/risky to rely on that. Also added an argument to the first call to
printColor for the color, I just passed "".

{
//print the text with style
print("<span style=\"color: $color\">" .
"$text</span>");

//if given a count, increment it
if(isset($count))
{
$count++;
}
}

//call with one argument
printColor("This is black text"); printColor("This is black text","", $c);
print("<br>\n");

//override default color
printColor("This is blue text", "blue"); printColor("This is blue text", "blue", $c);
print("<br>\n");

//pass in count reference
$c = 0;
printColor("This is red text", "red", $c);
print("<br>\n");
printColor("This is green text", "green", $c);
print("<br>\n");
print("Count: $c<br>");
?>


Like I said, I am just trying to learn a bit about php myself, hopefully
someone will have a better explanation / solution.

Have Fun!!

Tim

Jul 17 '05 #3

P: n/a
Thanks guy's. I appreciate you trying to help me out with this.

I looked at my book I'm trying to learn from and it's about php5. I
only had php4.something installed. I've now updated to PHP5 and it
seems to work just fine.

I'd still like to know what the difference is if someone happens to
know?

Jul 17 '05 #4

P: n/a
kyote scribbled something along the lines of:
This is straight out of a book I'm reading to learn PHP. I'm thinking
maybe I set either apache or PHP up incorrectly on my computer.
Simpler PHP's snippets work fine so I just don't know. Could someone
look this over and see if I have a problem?

<?php
function printColor($text,
$color="black", &$count=NULL)
{
//print the text with style
print("<span style=\"color: $color\">" .
"$text</span>");

//if given a count, increment it
if(isset($count))
{
$count++;
}
}

//call with one argument
printColor("This is black text");
print("<br>\n");

//override default color
printColor("This is blue text", "blue");
print("<br>\n");

//pass in count reference
$c = 0;
printColor("This is red text", "red", $c);
print("<br>\n");
printColor("This is green text", "green", $c);
print("<br>\n");
print("Count: $c<br>");
?>


You do not want that functionality. You want to manipulate a global
variable instead. Look up globals and how to manipulate them from within
a function at http://www.php.net/manual please.

Instead of passing the count variable you'll more likely want to pass a
boolean (whether you want the call to count or not: TRUE or FALSE
respectively with the default in the function being FALSE).

--
Alan Plum, WAD/WD, Mushroom Cloud Productions
http://www.mushroom-cloud.com/
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.