Forum Moderators: coopster

Message Too Old, No Replies

create function()

         

michaelmuller

1:31 am on Dec 2, 2011 (gmt 0)

10+ Year Member



Hey there, I'm new to PHP (coming from the CFM world) and need to create the PHP equivalent of the CFM function yesNoFormat() to convert BIT values from the db into "Yes" or "No" in the html output.

Is this function syntax correct?...

$yesNoFormat = create_function('$a', 'if ($a == "1") echo "Yes"; else echo "No"; ');

$yesNoFormat(row[aBitField]);

I'm not getting an error on the page when I include that code, but when I use the function I get nothing in the output. I mean, if $a isn't equal to 1 or even "1" I should get "No" in the output all the time, right? I'm just getting nothing.

Any advice?

vortex

2:24 am on Dec 2, 2011 (gmt 0)

10+ Year Member



To create a function in PHP, you use the function() function
So in this case, you could do:

function yesNoFormat($a) {
if ($a == 1) {
return 'Yes';
} else {
return 'No';
}
}


Then to add it to a variable:

$yesNoFormat = yesNoFormat($row['abitField']);


Or to just echo it out:

 echo yesNoFormat($row['abitField']); 

Untested, but should work. :)

michaelmuller

5:11 am on Dec 2, 2011 (gmt 0)

10+ Year Member



Ok. So I have this in the code near the top of the page...

function yesNoFormat($a) { if ($a == 1) { return 'Yes'; } else { return 'No'; } }

... and then lower down the page I have this...

echo ("\n<tr><td>Full Time?</td><td>".$yesNoFormat($row[employmentFullTime])."</td></tr>");

... and the page stops rendering at that point, on the line above the echo code.

michaelmuller

5:12 am on Dec 2, 2011 (gmt 0)

10+ Year Member



Ah. Remove the $ from in front of the yesNoFormat(). Works.

Thanks!

Mik