Problems with using/calling functions....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Problems with using/calling functions....

Post 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???
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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?
User avatar
phpScott
DevNet Resident
Posts: 1206
Joined: Wed Oct 09, 2002 6:51 pm
Location: Keele, U.K.

missing brackets

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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......
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post by sinewave »

try

Code: Select all

&lt;?php echo $lotterynums ?&gt;
if this makes it work...you wont have them enabled :)
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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
sinewave
Forum Commoner
Posts: 41
Joined: Tue Sep 10, 2002 4:35 pm
Location: Canada

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply