Hangwire Posted September 10, 2010 Share Posted September 10, 2010 Hello all, I finally started learning php and with the little knowledge I have from CPP it's far simpler than it seemed years ago. I need a little bit of help with this function I followed in a tutorial and it's just not working. <?php function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> It just gives me a blank screen. Now, I figured #fname isnt set, so I did this: <?php $fname = "Refsnes"; function writeName($fname) { echo $fname . " Refsnes.<br />"; } echo "My name is "; writeName("Kai Jim"); echo "My sister's name is "; writeName("Hege"); echo "My brother's name is "; writeName("Stale"); ?> Still, no go, which leads me to think I'm probably mistaking. Help? I don't want to skip out things in a tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/ Share on other sites More sharing options...
Adam Posted September 10, 2010 Share Posted September 10, 2010 That code works for me no problem. My guess is that the problem is elsewhere; possibly the web server. Try the code below in a file of it's own: <?php phpinfo(); ?> If you still see a blank screen you don't have the server configured correctly. Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109553 Share on other sites More sharing options...
kickstart Posted September 10, 2010 Share Posted September 10, 2010 Hi I just tried your code and it worked fine. $fname is a parameter to the function so is set by the call to the function. You do not need to give it a value at the start. Indeed inside the function $fname is referring to the parameter and the one you have defined specifically is a separate variable (if you want to access a variable that is outside the function from within the function you either need to pass it as a parameter or use the global statement to specify that you want to refer to it). All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109554 Share on other sites More sharing options...
wildteen88 Posted September 10, 2010 Share Posted September 10, 2010 Make sure the code is within a .php file. Also make sure you are running the PHP script from a server environment, either locally (eg http://localhost) or on your website. Passing the php file directly to your browser will not work as web browsers do not understand PHP code. You can install amp packages such wampserver (windows only) or xampp (windows, mac, linux) to run your PHP code locally on your PC. Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109562 Share on other sites More sharing options...
Hangwire Posted September 10, 2010 Author Share Posted September 10, 2010 Thank you for all the replies, then I'm afraid something is wrong with how i set up my webserver. I'm using apache and PHP 5, so far I've had no problems with any of the functions that I've had from that tutorial (tried them, no problem). Also, I execute all of them under firefox as php.php (it opens them without any problem) This is what I've done so far. <?php $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota"; echo $cars[0] . " and " . $cars[1] . " are Swedish cars." . $cars[2] . " and " . $cars[3] . " are not." ?> <br /> <?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?> <br /> <?php $families = array ( "Griffins"=>array ( "Peter", "Lois", "Megan" ), "Quagmires"=>array ( "Glenn" ), "Browns"=>array ( "Cleveland", "Loretta", "Junior" ) ); echo "Is " . $families['Griffins'][2] . " a part of the Griffin family?" . "Is " . $families['Quagmires'][0] . " a pervert?"; ?> <br /> <?php $i=1; while($i<=5) { echo "Chisloto e " . $i . "<br />"; $i++; } ?> <br /> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> <br /> <?php $i=1; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<=5); ?> <br /> <?php for ($i=1; $i<6; $i++) { echo "The number is " . $i . "<br />"; } ?> <br /> <?php $g=array("edno","dve","tri"); foreach ($g as $value) { echo $value . "<br />"; } ?> <br /> <?php function writeName() { echo "Smurt Ivanova"; } echo "My name is "; writeName(); ?> <br /> On and On and On and before that - all simple tutorial ones. I use that code just below all others and it gives me a blank page. When I remove it, all the above display normal results. Help? Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109592 Share on other sites More sharing options...
kickstart Posted September 10, 2010 Share Posted September 10, 2010 Hi If the code you just posted works fine, but adding the previous code at the end causes it to fail then I suspect that it is because you have 2 functions called writeName(). Although I would expect this to raise an error, not sure if some setting of error_reporting could suppress it. All the best Keith Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109600 Share on other sites More sharing options...
Hangwire Posted September 10, 2010 Author Share Posted September 10, 2010 Thank you, I'll try in a seperate file. I just saved my data from htdocs and I'm installing xampp - I don't know why I didn't in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/213046-an-extremely-simple-problem-with-functions/#findComment-1109605 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.