Page 1 of 1

Problems with using/calling functions....

Posted: Mon Oct 28, 2002 2:03 pm
by infolock
ok, so i have a small problem. Anytime/Everytime that I try and cretae a function, the page to be displayed goes all haywire. In other words, it just posts a blank page with no data.

I remove the funtion, and it works fine.

here is the code ( Note: this was created by lan3y on evilwalrus.com )

Code: Select all

function lottery($maxn = "49",$maxb="6") { 
srand((double) microtime() * 1000000); 

while (1>0) {  
$lotteryї] = rand(1,$maxn); 
$lottery = array_unique($lottery); 
if (sizeof($lottery) == $maxb) break; 
}  
sort($lottery); 
return implode(", ",$lottery); 
} 
$lotterynums = lottery(); 
?> 
<html> 
<head> 
<title>Lottery Numbers</title> 
</head> 
<body> 
<h1>And your lottery numbers for this week are...</h1><br> 
<h3><?=$lotterynums?></h3><br> 
</body> 
</html>
Anyone have any clue why this won't work???

Posted: Mon Oct 28, 2002 2:24 pm
by infolock
I guess I should also state, that if I use ANY type of funtion, or even attempt to declare a funtion within the php page, I get a blank page, so it's not just with this one script. Is there something I'm doing wrong?

missing brackets

Posted: Mon Oct 28, 2002 3:41 pm
by phpScott
If that is the exact code you are using there seems to be a couple of things missing.
1st is the open declaration of php ie. <?php at the top of the code.
2nd you are missing a couple of closing brackets ie }. One for the actual function and one for the while loop.

try putting those in place to see what happens.

phpScott

Posted: Wed Oct 30, 2002 4:10 pm
by infolock
well, this is the exact same thing I thought. However, when I put the missing }'s in the function, the page will pop up but the function never works. Not to mention, every function that I have seen written by somone, has the exact same layout, and usually close the script with a final <? } ?> at the end. I didn't put the <?php at the top of this example, as this is a pretty much a given to have to be there......

Posted: Thu Oct 31, 2002 2:39 am
by twigletmac
When you say "However, when I put the missing }'s in the function, the page will pop up but the function never works. " what do you mean? Do you get a parse error / other type of error / no error but the function doesn't do what it's supposed to ...?

This code appeared to work fine for me:

Code: Select all

&lt;?php

function lottery($maxn = "49",$maxb="6") { 
	srand((double) microtime() * 1000000); 
	while (1 &gt; 0) {  
		$lottery&#1111;] = rand(1,$maxn); 
		$lottery = array_unique($lottery); 
		if (sizeof($lottery) == $maxb) { break; } 
	}  
	sort($lottery); 
	return implode(", ",$lottery); 
}

$lotterynums = lottery(); 
?&gt; 
&lt;html&gt; 
&lt;head&gt; 
&lt;title&gt;Lottery Numbers&lt;/title&gt; 
&lt;/head&gt; 
&lt;body&gt; 
&lt;h1&gt;And your lottery numbers for this week are...&lt;/h1&gt;&lt;br&gt; 
&lt;h3&gt;&lt;?php echo $lotterynums; ?&gt;&lt;/h3&gt;&lt;br&gt; 
&lt;/body&gt; 
&lt;/html&gt;
The code you have uses short tags - <?=$lotterynums?> - is this enabled on your server?

Mac

Posted: Thu Oct 31, 2002 2:34 pm
by infolock
When you say "However, when I put the missing }'s in the function, the page will pop up but the function never works. " what do you mean? Do you get a parse error / other type of error / no error but the function doesn't do what it's supposed to ...?
Well, when I say it works, I mean that the page will load, but the function does not take into effect.

I other words, the lottery #'s do not pop up, or anything. The page takes a while to load, so I know it's generating the #'s, but no output is given for the above function.

The code you have uses short tags - <?=$lotterynums?> - is this enabled on your server?
No, I don't have short tags enabled on my server ( at least I don't think I do, if this needed to be manually entered before it takes into effect ). In other words, I don't really know if it's enabled or not. If it is enabled by default, then yes. If it's not, then no. If it's not, then is there anyway ya could explain to me how to enable it? Thanks! 8O

Posted: Thu Oct 31, 2002 2:42 pm
by sinewave
try

Code: Select all

&lt;?php echo $lotterynums ?&gt;
if this makes it work...you wont have them enabled :)

Posted: Thu Oct 31, 2002 2:58 pm
by infolock
lol, i didn't even think about trying that ;) I'll try it definately.

In the meantime, could anyone answer the last questions I gave? hehe, thanks ;)

8O

Posted: Thu Oct 31, 2002 3:03 pm
by sinewave
This is taken from the php.ini


; Allow the <? tag. Otherwise, only <?php and <script> tags are recognized.
; NOTE: Using short tags should be avoided when developing applications or
; libraries that are meant for redistribution, or deployment on PHP
; servers which are not under your control, because short tags may not
; be supported on the target server. For portable, redistributable code,
; be sure not to use short tags.
short_open_tag = O

Posted: Fri Nov 01, 2002 1:49 am
by twigletmac
sinewave wrote:try

Code: Select all

&lt;?php echo $lotterynums ?&gt;
That'd be like in the modified code snippet I posted then... should've made that more explicit I 'spose.

Mac