Connecting Tech Pros Worldwide Help | Site Map

Help with this code?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 17th, 2005, 04:56 AM
kyote
Guest
 
Posts: n/a
Default Help with this code?

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>");
?>

  #2  
Old July 17th, 2005, 04:56 AM
Martman
Guest
 
Posts: n/a
Default Re: Help with this code?

remove the ampersand in front of the $count in the function declaration

Marty U

"kyote" <truthowl@NOSPAM.charter.net> wrote in message
news:napg805dvjg7l4iijkfsc9fpllq8uqm2s9@4ax.com...[color=blue]
> 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>");
> ?>[/color]


  #3  
Old July 17th, 2005, 04:56 AM
Tim Sheets
Guest
 
Posts: n/a
Default Re: Help with this code?

kyote wrote:
[color=blue]
> 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?[/color]

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.
[color=blue]
>
> <?php
> function printColor($text,
> $color="black", &$count=NULL)[/color]
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 "".

[color=blue]
> {
> //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");[/color]
printColor("This is black text","", $c);
[color=blue]
> print("<br>\n");
>
> //override default color
> printColor("This is blue text", "blue");[/color]
printColor("This is blue text", "blue", $c);
[color=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>");
> ?>[/color]

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

  #4  
Old July 17th, 2005, 04:56 AM
kyote
Guest
 
Posts: n/a
Default Re: Help with this code?

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?



  #5  
Old July 17th, 2005, 04:57 AM
Ashmodai
Guest
 
Posts: n/a
Default Re: Help with this code?

kyote scribbled something along the lines of:
[color=blue]
> 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>");
> ?>[/color]

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/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.