Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function help!

Status
Not open for further replies.

fowlerlfc

MIS
Mar 20, 2002
136
US
I've written the following function:

function checkdata($infield){
if (!isset($infield) || empty($infield)) {
echo &quot;<br><font color='red'>You must enter a valid&quot; . &quot; &quot; . $infield . &quot; &quot; . &quot;to submit a workorder. Please click <a href= to return to the workorder submission form.</font>&quot;;
}
}

When I call it and the variable given for infield is empty, I get the following output:

You must enter a valid to submit a workorder. Please click here to return to the workorder submission form.

As you can see, the variable is not echoed. What am I doing wrong?

Any help is greatly appreciated!
 
If it is in a function, you need to globalise it first. Something like, if you submit a field called 'test', then you would need something like;

function test() {

global $test;

echo &quot;something here along with $test&quot;;

}
 
Why would it need to be globalised if it were passed in? The problem could be that, prior to the function call, the variable passed to the function as $infield had no value.

Globalising it would work though, and without any apparent side effects (it's not being modified in the function, so it should affect references outside of the function).
 
well ... you are echoing if the var is empty ... so it's normal. It is showing ... but empty.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
well ... you are echoing if the var is empty ... so it's normal. It is showing ... but empty.

Disord3r: I have an advise to you... Globalise is not the solution for problems. That's a two sided knife. A global var you NEVER now the real value, cause you can change it inside functions, etc.
Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
I'm with Anikin,
If the variable is empty then echoing it will display nothing. You should just write

function checkdata($infield)
{
if ((!isset($infield)) || empty($infield))
{
echo '<br /><font color=&quot;red&quot;>You must enter a valid entry to submit a workorder. Please click <a href=&quot; to return to the workorder submission form.</font>';
return FALSE;
}
else
{
return TRUE;
}
}

I prefer to use single quotes when echoing HTML so I can use double quotes and keep to XML standard. BE WARNED that you cannot include codes like \n and variables within this, but need to seperate them as in the original post.
 
attention with the single quotes cause if you do this:

$var=5;
echo &quot;$var&quot;;
is different than
$var=5;
echo '$var';

The output of the first will be 5 and the output of the second will be $var.
When you use single quotes the var is not changed to it's value. Anikin
Hugo Alexandre Dias
Web-Programmer
anikin_jedi@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top