Connecting Tech Pros Worldwide Help | Site Map

Help with this code?

  #1  
Old July 17th, 2005, 05:56 AM
kyote
Guest
 
Posts: 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>");
?>
  #2  
Old July 17th, 2005, 05:56 AM
Martman
Guest
 
Posts: n/a

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, 05:56 AM
Tim Sheets
Guest
 
Posts: n/a

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, 05:56 AM
kyote
Guest
 
Posts: n/a

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, 05:57 AM
Ashmodai
Guest
 
Posts: n/a

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/
Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with this code - parse insert into Mysql Steve answers 4 July 28th, 2008 01:15 AM
I need help with this code. basestring answers 1 March 25th, 2007 12:05 PM
Need some help with this code... EMW answers 5 November 20th, 2005 07:46 AM
Bit of help with this code please? Rob Meade answers 1 November 18th, 2005 09:56 AM
Can anybody help with this code? Catalin Porancea answers 3 November 17th, 2005 11:16 PM