473,395 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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>");
?>
Jul 17 '05 #1
4 1527
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Edward King | last post by:
Hi! I am trying to achieve the following: I have a number of help pages (in the format help_nn.php where nn=helpid). I want to be able to open a particular help page by calling the function...
2
by: sunfox | last post by:
Please help!! I have a difficulty in writing an assignment which is related to Visual C++ V6.0. Can anybody here assist me to write a program which is able to run under DOS? The program will be...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
1
by: Paul Rubin | last post by:
In Windows if you click the Help dropdown, IDLE launches a help window as it should. The help contents are included in the installation. In Linux, clicking Help launches a web browser, which is...
1
by: glenn123 | last post by:
Hi, i am just about out of time to produce a working jukebox which has to perform these functions: to play music files when a track is chosen from a list which when the user presses the change genre...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
45
by: davy.zou | last post by:
I have started learning c++ and I need help. I need to write a program, the question is as follows. At a post office, there are a certain number of 2, 7, and 9cents stamps, now, given a total...
1
by: dsoutherland | last post by:
I am so new to ASP I'm not sure I am breathing. A company one of my clients deals with provides information over the web. They provided the ASP code for a default web site that is used to...
1
by: menyki | last post by:
help 'e debug -------------------------------------------------------------------------------- I wrote the below code to input new data from name and address textbox of a form to microsoft...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.